Skip to content

Commit 588b822

Browse files
committed
Fix inconsistent indents, Modify method name
1 parent 5113206 commit 588b822

File tree

2 files changed

+115
-113
lines changed

2 files changed

+115
-113
lines changed

src/main/java/net/lamgc/utils/base/ArgumentsProperties.java

Lines changed: 114 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -3,154 +3,156 @@
33
import java.util.ArrayList;
44
import java.util.HashMap;
55
import java.util.List;
6+
import java.util.Set;
67

78
public class ArgumentsProperties extends HashMap<String, String>{
89

9-
private static final long serialVersionUID = 3008463812837214134L;
10-
11-
private static transient final String[] defaultKeyFlags = new String[] {
12-
"--",
13-
"-",
14-
"/"
15-
};
10+
private static final long serialVersionUID = 3008463812837214134L;
11+
12+
private static transient final String[] defaultKeyFlags = new String[] {
13+
"--",
14+
"-",
15+
"/"
16+
};
1617

17-
private transient String[] keyFlags;
18+
private String[] keyFlags;
1819

19-
private ArrayList<String> keyList = new ArrayList<>();
20+
private ArrayList<String> keyList = new ArrayList<>();
2021

2122
public ArgumentsProperties(){
22-
this(null, null);
23-
}
23+
this(null);
24+
}
2425

25-
public ArgumentsProperties(String[] args){
26-
this(args, null);
27-
}
26+
public ArgumentsProperties(String[] args){
27+
this(args, null);
28+
}
2829

29-
// 对指定参数提供过滤选项,以在使用不正确选项的时候抛出异常
30+
// 对指定参数提供过滤选项,以在使用不正确选项的时候抛出异常
3031
public ArgumentsProperties(String[] args, String[] keyFlags){
31-
this.keyFlags = keyFlags == null ? defaultKeyFlags : keyFlags;
32-
if(args != null){
33-
this.load(args);
34-
}
32+
this.keyFlags = keyFlags == null ? defaultKeyFlags : keyFlags;
33+
if(args != null){
34+
this.load(args);
35+
}
3536
}
3637

3738
/*
38-
后期提供的功能:
39-
解析事件
40-
对选项的限制
41-
42-
43-
*/
44-
45-
/*
46-
//通用格式
39+
//通用格式
4740
-key value
4841
-key:value
49-
50-
//长参数格式
42+
43+
//长参数格式
5144
--key value
5245
--key:value
53-
54-
//windows格式
55-
/key value
46+
47+
//windows格式
48+
/key value
5649
/key:value
5750
多个值需要使用:(windows) ;(linux) (自动识别)(以第一个为准)
5851
*/
5952

60-
/**
61-
* 将参数数组加载进行处理
62-
* @param args 参数数组
63-
*/
53+
/**
54+
* 将参数数组加载进行处理
55+
* @param args 参数数组
56+
*/
6457
public void load(String[] args){
65-
String lastKey = null;
66-
for(String arg : args){
67-
String cacheKey = getKey(arg);
68-
if(cacheKey != null){
69-
if(lastKey != null){
70-
super.put(lastKey, "");
71-
keyList.add(lastKey);
72-
}
73-
74-
//尝试取值
75-
String cacheValue = getValueFromKey(arg);
76-
if(cacheValue == null){
77-
//等待取值
78-
lastKey = cacheKey;
79-
}else{
80-
super.put(cacheKey, cacheValue);
81-
keyList.add(cacheKey);
82-
}
83-
}else{
84-
//作为参数值存储
85-
super.put(lastKey, arg);
58+
String lastKey = null;
59+
for(String arg : args){
60+
String cacheKey = getKeyFilterFlag(arg);
61+
if(cacheKey != null){
62+
if(lastKey != null){
63+
super.put(lastKey, "");
64+
keyList.add(lastKey);
65+
}
66+
67+
//尝试取值
68+
String cacheValue = getValueFromKey(arg);
69+
if(cacheValue == null){
70+
//等待取值
71+
lastKey = cacheKey;
72+
}else{
73+
super.put(cacheKey, cacheValue);
74+
keyList.add(cacheKey);
75+
}
76+
}else{
77+
//作为参数值存储
78+
super.put(lastKey, arg);
8679
keyList.add(lastKey);
87-
lastKey = null;
88-
}
89-
}
90-
if(lastKey != null){
91-
super.put(lastKey, "");
80+
lastKey = null;
81+
}
82+
}
83+
if(lastKey != null){
84+
super.put(lastKey, "");
9285
keyList.add(lastKey);
9386
}
9487
}
95-
96-
/**
97-
* 键参数上是否有值
98-
* @return 有参数值则返回true
99-
*/
100-
private String getValueFromKey(String s){
101-
int index;
102-
if((index = s.indexOf(":")) == -1){
103-
return null;
104-
}
105-
return s.substring(index + 1).trim();
106-
}
107-
108-
/**
109-
* 过滤key标识符, 如果该字符串没有以预定的key标识符开头, 则不认定为是一个key, 返回null
110-
* @param s 需要检查并过滤的字符串
111-
* @return 如果是参数名, 返回已过滤标识符的参数名, 如果不是, 返回null
112-
*/
113-
private String getKey(String s){
114-
int flagIndex = -1;
115-
for(int i = 0; i < keyFlags.length; i++){
116-
if(s.indexOf(keyFlags[i]) == 0){
117-
flagIndex = i;
118-
break;
119-
}
120-
}
121-
if(flagIndex == -1){
122-
return null;
123-
}
124-
125-
int valueFlag = s.indexOf(":");
126-
if(valueFlag == -1){
88+
89+
/**
90+
* 键参数上是否有值
91+
* @return 有参数值则返回true
92+
*/
93+
private String getValueFromKey(String s){
94+
int index;
95+
if((index = s.indexOf(":")) == -1){
96+
return null;
97+
}
98+
return s.substring(index + 1).trim();
99+
}
100+
101+
/**
102+
* 过滤key标识符, 如果该字符串没有以预定的key标识符开头, 则不认定为是一个key, 返回null
103+
* @param s 需要检查并过滤的字符串
104+
* @return 如果是参数名, 返回已过滤标识符的参数名, 如果不是, 返回null
105+
*/
106+
private String getKeyFilterFlag(String s){
107+
int flagIndex = -1;
108+
for(int i = 0; i < keyFlags.length; i++){
109+
if(s.indexOf(keyFlags[i]) == 0){
110+
flagIndex = i;
111+
break;
112+
}
113+
}
114+
if(flagIndex == -1){
115+
return null;
116+
}
117+
118+
int valueFlag = s.indexOf(":");
119+
if(valueFlag == -1){
127120
return s.replace(keyFlags[flagIndex], "").trim();
128121
}else{
129-
return s.substring(keyFlags[flagIndex].length(), valueFlag).trim();
122+
return s.substring(keyFlags[flagIndex].length(), valueFlag).trim();
130123
}
131124

132-
}
125+
}
133126

134127

135-
/**
136-
* 获取指定参数的值
137-
* @param key 要获取值的参数名
138-
* @return 如果输入的参数中有指定的参数, 则返回非null(即使没有值), 如果该参数不存在, 返回null
139-
*/
140-
public String getValue(String key){
141-
if(!super.containsKey(key)){
142-
return null;
143-
}
144-
return super.get(key);
145-
}
128+
/**
129+
* 获取指定参数的值
130+
* @param key 要获取值的参数名
131+
* @return 如果输入的参数中有指定的参数, 则返回非null(即使没有值), 如果该参数不存在, 返回null
132+
*/
133+
public String getValue(String key){
134+
if(!super.containsKey(key)){
135+
return null;
136+
}
137+
return super.get(key);
138+
}
146139

147140
/**
148-
* 获取有序的参数列表,
149-
*
141+
* 获取原始输入的参数key列表.
142+
* 该列表中的参数属性带有flag, 无法从ArgumentsProperties中获取,
143+
* 要获取能够从ArgumentsProperties中取值的key列表请使用{@link #getKeys()}
150144
* @return 返回存储参数列表的List
151145
*/
152-
public List<String> getKeyList(){
153-
return keyList;
146+
public List<String> getKeysWithFlag(){
147+
return keyList;
148+
}
149+
150+
/**
151+
* 获取经过解析的key列表.
152+
* @return 返回成功解析的参数key列表
153+
*/
154+
public Set<String> getKeys(){
155+
return super.keySet();
154156
}
155157

156158
}

src/test/java/net/lamgc/utils/ArgumentsPropertiesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ArgumentsPropertiesTest{
1313
public void loadTest(){
1414
//解析参数
1515
ArgumentsProperties argsProperties = new ArgumentsProperties(argsList);
16-
System.out.println(Arrays.toString(argsProperties.getKeyList().toArray(new String[0])));
16+
System.out.println(Arrays.toString(argsProperties.getKeysWithFlag().toArray(new String[0])));
1717
for(String key : argsProperties.keySet().toArray(new String[0])){
1818
System.out.println(key + ": " + argsProperties.get(key));
1919
}

0 commit comments

Comments
 (0)