Java技术学习 https://www.itkc8.com
ArrayUtils
数组工具类,可用于数组常用操作,如:
isEmpty(V[] sourceArray) 判断数组是否为空或长度为0
getLast(V[] sourceArray, V value, V defaultValue, boolean isCircle) 得到数组中某个元素前一个元素,isCircle表示是否循环
getNext(V[] sourceArray, V value, V defaultValue, boolean isCircle) 得到数组中某个元素下一个元素,isCircle表示是否循环
/**
* Array Utils
*
private ArrayUtils() {
throw new AssertionError();
}
/**
* is null or its length is 0
*
* @param
* @param sourceArray
* @return
*/
public static
return (sourceArray == null || sourceArray.length == 0);
}
/**
* get last element of the target element, before the first one that match the target element front to back
*
int currentPosition = -1;
for (int i = 0; i < sourceArray.length; i++) {
if (ObjectUtils.isEquals(value, sourceArray[i])) {
currentPosition = i;
break;
}
}
if (currentPosition == -1) {
return defaultValue;
}
if (currentPosition == 0) {
return isCircle ? sourceArray[sourceArray.length - 1] : defaultValue;
}
return sourceArray[currentPosition - 1];
}
/**
* get next element of the target element, after the first one that match the target element front to back
*
int currentPosition = -1;
for (int i = 0; i < sourceArray.length; i++) {
if (ObjectUtils.isEquals(value, sourceArray[i])) {
currentPosition = i;
break;
}
}
if (currentPosition == -1) {
return defaultValue;
}
if (currentPosition == sourceArray.length - 1) {
return isCircle ? sourceArray[0] : defaultValue;
}
return sourceArray[currentPosition + 1];
}
/**
* @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} defaultValue is null
*/
public static
return getLast(sourceArray, value, null, isCircle);
}
/**
* @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} defaultValue is null
*/
public static
return getNext(sourceArray, value, null, isCircle);
}
/**
* @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} Object is Long
*/
public static long getLast(long[] sourceArray, long value, long defaultValue, boolean isCircle) {
if (sourceArray.length == 0) {
throw new IllegalArgumentException("The length of source array must be greater than 0.");
}
Long[] array = ObjectUtils.transformLongArray(sourceArray);
return getLast(array, value, defaultValue, isCircle);
}
/**
* @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} Object is Long
*/
public static long getNext(long[] sourceArray, long value, long defaultValue, boolean isCircle) {
if (sourceArray.length == 0) {
throw new IllegalArgumentException("The length of source array must be greater than 0.");
}
Long[] array = ObjectUtils.transformLongArray(sourceArray);
return getNext(array, value, defaultValue, isCircle);
}
/**
* @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} Object is Integer
*/
public static int getLast(int[] sourceArray, int value, int defaultValue, boolean isCircle) {
if (sourceArray.length == 0) {
throw new IllegalArgumentException("The length of source array must be greater than 0.");
}
Integer[] array = ObjectUtils.transformIntArray(sourceArray);
return getLast(array, value, defaultValue, isCircle);
}
/**
* @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} Object is Integer
*/
public static int getNext(int[] sourceArray, int value, int defaultValue, boolean isCircle) {
if (sourceArray.length == 0) {
throw new IllegalArgumentException("The length of source array must be greater than 0.");
}
Integer[] array = ObjectUtils.transformIntArray(sourceArray);
return getNext(array, value, defaultValue, isCircle);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
ListUtils
List工具类,可用于List常用操作,如:
isEmpty(List sourceList) 判断List是否为空或长度为0
join(List list, String separator) List转换为字符串,并以固定分隔符分割
addDistinctEntry(List sourceList, V entry) 向list中添加不重复元素
import java.util.ArrayList;
import java.util.List;
import android.text.TextUtils;
/**
* List Utils
*/
public class ListUtils {
/** default join separator **/
public static final String DEFAULT_JOIN_SEPARATOR = ",";
private ListUtils() {
throw new AssertionError();
}
/**
* get size of list
*
*
* getSize(null) = 0;
* getSize({}) = 0;
* getSize({1}) = 1;
*
/**
* is null or its size is 0
*
*
* isEmpty(null) = true;
* isEmpty({}) = true;
* isEmpty({1}) = false;
*
/**
* compare two list
*
*
* isEquals(null, null) = true;
* isEquals(new ArrayList<String>(), null) = false;
* isEquals(null, new ArrayList<String>()) = false;
* isEquals(new ArrayList<String>(), new ArrayList<String>()) = true;
*
for (int i = 0; i < actual.size(); i++) {
if (!ObjectUtils.isEquals(actual.get(i), expected.get(i))) {
return false;
}
}
return true;
}
/**
* join list to string, separator is ","
*
*
* join(null) = "";
* join({}) = "";
* join({a,b}) = "a,b";
*
/**
* join list to string
*
*
* join(null, '#') = "";
* join({}, '#') = "";
* join({a,b,c}, ' ') = "abc";
* join({a,b,c}, '#') = "a#b#c";
*
/**
* join list to string. if separator is null, use {@link #DEFAULT_JOIN_SEPARATOR}
*
*
* join(null, "#") = "";
* join({}, "#$") = "";
* join({a,b,c}, null) = "a,b,c";
* join({a,b,c}, "") = "abc";
* join({a,b,c}, "#") = "a#b#c";
* join({a,b,c}, "#$") = "a#$b#$c";
*
/**
* add distinct entry to list
*
* @param
* @param sourceList
* @param entry
* @return if entry already exist in sourceList, return false, else add it and return true.
*/
public static
return (sourceList != null && !sourceList.contains(entry)) ? sourceList.add(entry) : false;
}
/**
* add all distinct entry to list1 from list2
*
* @param
* @param sourceList
* @param entryList
* @return the count of entries be added
*/
public static
if (sourceList == null || isEmpty(entryList)) {
return 0;
}
int sourceCount = sourceList.size();
for (V entry : entryList) {
if (!sourceList.contains(entry)) {
sourceList.add(entry);
}
}
return sourceList.size() - sourceCount;
}
/**
* remove duplicate entries in list
*
* @param
* @param sourceList
* @return the count of entries be removed
*/
public static
if (isEmpty(sourceList)) {
return 0;
}
int sourceCount = sourceList.size();
int sourceListSize = sourceList.size();
for (int i = 0; i < sourceListSize; i++) {
for (int j = (i + 1); j < sourceListSize; j++) {
if (sourceList.get(i).equals(sourceList.get(j))) {
sourceList.remove(j);
sourceListSize = sourceList.size();
j--;
}
}
}
return sourceCount - sourceList.size();
}
/**
* add not null entry to list
*
* @param sourceList
* @param value
* @return
/**
* @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} defaultValue is null, isCircle is true
*/
@SuppressWarnings("unchecked")
public static
return (sourceList == null) ? null : (V)ArrayUtils.getLast(sourceList.toArray(), value, true);
}
/**
* @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} defaultValue is null, isCircle is true
*/
@SuppressWarnings("unchecked")
public static
return (sourceList == null) ? null : (V)ArrayUtils.getNext(sourceList.toArray(), value, true);
}
/**
* invert list
*
* @param
* @param sourceList
* @return
*/
public static
if (isEmpty(sourceList)) {
return sourceList;
}
List
for (int i = sourceList.size() - 1; i >= 0; i--) {
invertList.add(sourceList.get(i));
}
return invertList;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
MapUtils
Map工具类,可用于Map常用操作,如:
isEmpty(Map
toJson(Map
1
2
3
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
* Map Utils
*/
public class MapUtils {
/** default separator between key and value **/
public static final String DEFAULT_KEY_AND_VALUE_SEPARATOR = ":";
/** default separator between key-value pairs **/
public static final String DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR = ",";
private MapUtils() {
throw new AssertionError();
}
/**
* is null or its size is 0
*
*
* isEmpty(null) = true;
* isEmpty({}) = true;
* isEmpty({1, 2}) = false;
*
/**
* add key-value pair to map, and key need not null or empty
*
* @param map
* @param key
* @param value
* @return
map.put(key, value);
return true;
}
/**
* add key-value pair to map, both key and value need not null or empty
*
* @param map
* @param key
* @param value
* @return
map.put(key, value);
return true;
}
/**
* add key-value pair to map, key need not null or empty
*
* @param map
* @param key
* @param value
* @param defaultValue
* @return
map.put(key, StringUtils.isEmpty(value) ? defaultValue : value);
return true;
}
/**
* add key-value pair to map, key need not null
*
* @param map
* @param key
* @param value
* @return
map.put(key, value);
return true;
}
/**
* add key-value pair to map, both key and value need not null
*
* @param map
* @param key
* @param value
* @return
map.put(key, value);
return true;
}
/**
* get key by value, match the first entry front to back
*
for (Entry
if (ObjectUtils.isEquals(entry.getValue(), value)) {
return entry.getKey();
}
}
return null;
}
/**
* parse key-value pairs to map, ignore empty key
*
*
* parseKeyAndValueToMap("","","",true)=null
* parseKeyAndValueToMap(null,"","",true)=null
* parseKeyAndValueToMap("a:b,:","","",true)={(a,b)}
* parseKeyAndValueToMap("a:b,:d","","",true)={(a,b)}
* parseKeyAndValueToMap("a:b,c:d","","",true)={(a,b),(c,d)}
* parseKeyAndValueToMap("a=b, c = d","=",",",true)={(a,b),(c,d)}
* parseKeyAndValueToMap("a=b, c = d","=",",",false)={(a, b),( c , d)}
* parseKeyAndValueToMap("a=b, c=d","=", ",", false)={(a,b),( c,d)}
* parseKeyAndValueToMap("a=b; c=d","=", ";", false)={(a,b),( c,d)}
* parseKeyAndValueToMap("a=b, c=d", ",", ";", false)={(a=b, c=d)}
*
if (StringUtils.isEmpty(keyAndValueSeparator)) {
keyAndValueSeparator = DEFAULT_KEY_AND_VALUE_SEPARATOR;
}
if (StringUtils.isEmpty(keyAndValuePairSeparator)) {
keyAndValuePairSeparator = DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR;
}
Map
String[] keyAndValueArray = source.split(keyAndValuePairSeparator);
if (keyAndValueArray == null) {
return null;
}
int seperator;
for (String valueEntity : keyAndValueArray) {
if (!StringUtils.isEmpty(valueEntity)) {
seperator = valueEntity.indexOf(keyAndValueSeparator);
if (seperator != -1) {
if (ignoreSpace) {
MapUtils.putMapNotEmptyKey(keyAndValueMap, valueEntity.substring(0, seperator).trim(),
valueEntity.substring(seperator + 1).trim());
} else {
MapUtils.putMapNotEmptyKey(keyAndValueMap, valueEntity.substring(0, seperator),
valueEntity.substring(seperator + 1));
}
}
}
}
return keyAndValueMap;
}
/**
* parse key-value pairs to map, ignore empty key
*
* @param source key-value pairs
* @param ignoreSpace whether ignore space at the begging or end of key and value
* @return
* @see {@link MapUtils#parseKeyAndValueToMap(String, String, String, boolean)}, keyAndValueSeparator is
* {@link #DEFAULT_KEY_AND_VALUE_SEPARATOR}, keyAndValuePairSeparator is
* {@link #DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR}
*/
public static Map
return parseKeyAndValueToMap(source, DEFAULT_KEY_AND_VALUE_SEPARATOR, DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR,
ignoreSpace);
}
/**
* parse key-value pairs to map, ignore empty key, ignore space at the begging or end of key and value
*
* @param source key-value pairs
* @return
* @see {@link MapUtils#parseKeyAndValueToMap(String, String, String, boolean)}, keyAndValueSeparator is
* {@link #DEFAULT_KEY_AND_VALUE_SEPARATOR}, keyAndValuePairSeparator is
* {@link #DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR}, ignoreSpace is true
*/
public static Map
return parseKeyAndValueToMap(source, DEFAULT_KEY_AND_VALUE_SEPARATOR, DEFAULT_KEY_AND_VALUE_PAIR_SEPARATOR,
true);
}
/**
* join map
*
* @param map
* @return
*/
public static String toJson(Map
if (map == null || map.size() == 0) {
return null;
}
StringBuilder paras = new StringBuilder();
paras.append("{");
Iterator
while (ite.hasNext()) {
Map.Entry
paras.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\"");
if (ite.hasNext()) {
paras.append(",");
}
}
paras.append("}");
return paras.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
ObjectUtils
Object工具类,可用于Object常用操作,如:
isEquals(Object actual, Object expected) 比较两个对象是否相等
compare(V v1, V v2) 比较两个对象大小
transformIntArray(int[] source) Integer 数组转换为int数组
1
2
3
4
public class ObjectUtils {
private ObjectUtils() {
throw new AssertionError();
}
/**
* compare two object
*
* @param actual
* @param expected
* @return
/**
* null Object to empty string
*
*
* nullStrToEmpty(null) = "";
* nullStrToEmpty("") = "";
* nullStrToEmpty("aa") = "aa";
*
/**
* convert long array to Long array
*
* @param source
* @return
*/
public static Long[] transformLongArray(long[] source) {
Long[] destin = new Long[source.length];
for (int i = 0; i < source.length; i++) {
destin[i] = source[i];
}
return destin;
}
/**
* convert Long array to long array
*
* @param source
* @return
*/
public static long[] transformLongArray(Long[] source) {
long[] destin = new long[source.length];
for (int i = 0; i < source.length; i++) {
destin[i] = source[i];
}
return destin;
}
/**
* convert int array to Integer array
*
* @param source
* @return
*/
public static Integer[] transformIntArray(int[] source) {
Integer[] destin = new Integer[source.length];
for (int i = 0; i < source.length; i++) {
destin[i] = source[i];
}
return destin;
}
/**
* convert Integer array to int array
*
* @param source
* @return
*/
public static int[] transformIntArray(Integer[] source) {
int[] destin = new int[source.length];
for (int i = 0; i < source.length; i++) {
destin[i] = source[i];
}
return destin;
}
/**
* compare two object
*
public class SystemUtils {
/** recommend default thread pool size according to system available processors, {@link #getDefaultThreadPoolSize()} **/
public static final int DEFAULT_THREAD_POOL_SIZE = getDefaultThreadPoolSize();
private SystemUtils() {
throw new AssertionError();
}
/**
* get recommend default thread pool size
*
* @return if 2 * availableProcessors + 1 less than 8, return it, else return 8;
* @see {@link #getDefaultThreadPoolSize(int)} max is 8
*/
public static int getDefaultThreadPoolSize() {
return getDefaultThreadPoolSize(8);
}
/**
* get recommend default thread pool size
*
* @param max
* @return if 2 * availableProcessors + 1 less than max, return it, else return max;
*/
public static int getDefaultThreadPoolSize(int max) {
int availableProcessors = 2 * Runtime.getRuntime().availableProcessors() + 1;
return availableProcessors > max ? max : availableProcessors;
}
}
原文:https://blog.csdn.net/King1425/article/details/53043596
Java技术学习 https://www.itkc8.com