OSDN Git Service

modify redundant boolean formula.
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / config / CmdOption.java
1 /*
2  * command line options
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.config;
9
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.EnumSet;
13 import java.util.List;
14 import jp.sfjp.jindolf.ResourceManager;
15
16 /**
17  * コマンドラインオプションの列挙。
18  *
19  * <p>1オプションは複数の別名を持ちうる。
20  *
21  * <p>1引数を持つオプションと持たないオプションは区別される。
22  */
23 public enum CmdOption {
24
25     /** ヘルプ。 */
26     OPT_HELP("-help", "-h", "--help", "-?"),
27     /** 版数表示。 */
28     OPT_VERSION("-version"),
29     /** UI文字制御。 */
30     OPT_BOLDMETAL("-boldMetal"),
31     /** ウィンドウ位置指定。 */
32     OPT_GEOMETRY("-geometry"),
33     /** 実行環境出力。 */
34     OPT_VMINFO("-vminfo"),
35     /** コンソールログ。 */
36     OPT_CONSOLELOG("-consolelog"),
37     /** フォント指定。 */
38     OPT_INITFONT("-initfont"),
39     /** アンチエイリアス。 */
40     OPT_ANTIALIAS("-antialias"),
41     /** サブピクセル制御。 */
42     OPT_FRACTIONAL("-fractional"),
43     /** 設定格納ディレクトリ指定。 */
44     OPT_CONFDIR("-confdir"),
45     /** 設定格納ディレクトリ不使用。 */
46     OPT_NOCONF("-noconfdir"),
47     ;
48
49
50     private static final Collection<CmdOption> OPTS_INDEPENDENT =
51             EnumSet.of(
52             OPT_HELP,
53             OPT_VERSION,
54             OPT_VMINFO,
55             OPT_BOLDMETAL,
56             OPT_CONSOLELOG,
57             OPT_NOCONF
58             );
59     private static final Collection<CmdOption> OPTS_BOOLEAN =
60             EnumSet.of(
61             OPT_ANTIALIAS,
62             OPT_FRACTIONAL
63             );
64
65     private static final String RES_DIR = "resources";
66     private static final String RES_HELPTEXT = RES_DIR + "/help.txt";
67
68
69     private final List<String> nameList;
70
71
72     /**
73      * コンストラクタ。
74      *
75      * @param names オプション名の一覧
76      */
77     private CmdOption(String ... names){
78         assert names.length > 0;
79         this.nameList = Arrays.asList(names);
80         return;
81     }
82
83
84     /**
85      * ヘルプメッセージ(オプションの説明)を返す。
86      *
87      * @return ヘルプメッセージ
88      */
89     public static String getHelpText(){
90         String helpText = ResourceManager.getTextFile(RES_HELPTEXT);
91         return helpText;
92     }
93
94     /**
95      * オプション名に合致するEnumを返す。
96      *
97      * @param arg 個別のコマンドライン引数
98      * @return 合致したEnum。どれとも合致しなければnull
99      */
100     public static CmdOption parseCmdOption(String arg){
101         for(CmdOption option : values()){
102             if(option.matches(arg)) return option;
103         }
104         return null;
105     }
106
107
108     /**
109      * 任意のオプション文字列がこのオプションに合致するか判定する。
110      *
111      * @param option ハイフンの付いたオプション文字列
112      * @return 合致すればtrue
113      */
114     public boolean matches(String option){
115         boolean result = this.nameList.contains(option);
116         return result;
117     }
118
119     /**
120      * 単体で意味をなすオプションか判定する。
121      *
122      * @return 単体で意味をなすならtrue
123      */
124     public boolean isIndepOption(){
125         boolean result = OPTS_INDEPENDENT.contains(this);
126         return result;
127     }
128
129     /**
130      * 真偽指定を一つ必要とするオプションか判定する。
131      *
132      * @return 真偽指定を一つ必要とするオプションならtrue
133      */
134     public boolean isBooleanOption(){
135         boolean result = OPTS_BOOLEAN.contains(this);
136         return result;
137     }
138
139     /**
140      * オプション名を返す。
141      *
142      * <p>オプション別名が複数指定されている場合は最初のオプション名
143      *
144      * @return オプション名
145      */
146     @Override
147     public String toString(){
148         String result = this.nameList.get(0);
149         return result;
150     }
151
152 }