DataUtils.java
9.93 KB
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
286
287
288
289
290
291
292
package com.huaheng.common.utils;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.PriorityQueue;
public class DataUtils {
static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/***
* 转化为Example方法名
* @param filedName 字段名
* @param methodSuffix 方法后缀
* @return
*/
public static String ConvertMethodName(String filedName, String methodSuffix) {
String returnValue = null;
if (filedName.length() > 0)
{
char[] nameChar = filedName.toCharArray();
if (nameChar[0] >= 'a' && nameChar[0] <= 'z') {
nameChar[0] = (char) (nameChar[0] - 32);
}
returnValue = "and" + String.valueOf(nameChar) + methodSuffix;
}
return returnValue;
}
/**
* 将对象转为字符串
* @param object
* @return
*/
public static String getString(Object object)
{
if (object == null) {
return null;
} else {
return object.toString();
}
}
/**
* 将对象转为Integer
* @param object
* @return
*/
public static Integer getInteger(Object object)
{
if (object == null) {
return null;
} else {
return Integer.valueOf(object.toString());
}
}
/**
* 将对象转为BigDecimal
* @param object
* @return
*/
public static BigDecimal getBigDecimal(Object object)
{
if (object == null) {
return null;
} else {
return new BigDecimal(object.toString());
}
}
/**
* 将对象转为Double
* @param object
* @return
*/
public static Double getDouble(Object object)
{
if (object == null) {
return null;
} else {
return new Double(object.toString());
}
}
/**
* 将long转为Integer
* @param object
* @return
*/
public static Integer getInteger(long object)
{
return Integer.valueOf(String.valueOf(object));
}
/**
* 将对象转为Date
* @param object
* @return
*/
public static Date getDateTime(Object object) throws ParseException
{
if (object == null)
{
return null;
}
else
{
return format.parse(object.toString());
}
}
/**
* 通过一个实体类,给另一个实体类赋值
* @param source 源实体类
* @param target 目标实体类
* @param ignoreProperties 忽略字段(多个字段用逗号隔开)
* @throws Exception
*/
public static void CopyDataByNotNull (Object source, Object target, String ignoreProperties) throws Exception {
Field[] sourceFields = source.getClass().getDeclaredFields();
Field[] targetFields = target.getClass().getDeclaredFields();
Field.setAccessible(targetFields, true);
for (int i = 0; i < sourceFields.length; i++) {
String sourceName = sourceFields[i].getName();
Object sourceValue = sourceFields[i].get(source);
if (sourceValue == null || ignoreProperties.indexOf(sourceName) > -1) {
continue;
}
for (Field targetField : targetFields) {
if (targetField.getName().equals(sourceName)) {
targetField.set(target, sourceValue);
break;
}
}
}
}
/**
* 格式化map
* @param map
* @return
*/
public static String sendGetFormat(Map<String, Object> map){
StringBuilder reslut = new StringBuilder();
for (Map.Entry<String, Object> a : map.entrySet()) {
reslut.append(a.getKey()).append("=").append(EncodingUtil.encodeURIComponent(String.valueOf(a.getValue()))+"&");
}
return String.valueOf(reslut);
}
/**
* 现有两个等长数组A和B,里面存放的是整型数据。现在分别要从数组A中取出M个数,B中取出N个数,
* 两个数组中所取数的数组下标不能相同,求所取整数之和的最大值
*/
public static int maxChoose(int[] A, int[] B, int M, int N) {// M > 0 && N > 0
int len = A.length;
int[][] diff = new int[len][3];
for (int i=0;i<len;i++){
if(A[i] >= B[i]){
diff[i][0]=A[i];
diff[i][2] = 1;
}else{
diff[i][0]=B[i];
diff[i][2] = 2;
}
diff[i][1] = i;
}
Integer result=0;
Arrays.sort(diff, (i, j) -> j[0]-i[0] );
for (int i = 0; i < diff.length; i++) {
if (diff[i][2] == 1) {
if(M>0){
M--;
System.out.println(diff[i][0]);
result=result+diff[i][0];
}
} else {
if(N>0){
N--;
System.out.println(diff[i][0]);
result=result+diff[i][0];
}
}
if (M == 0 && N == 0) {
break;
}
}
return result ;
}
public static void main(String[] args) {
// Integer max=maxChoose(new int[]{9,5,10}, new int[]{1,1,2}, 2, 1);
//取AB数组坐标相同的数中大数,组成数组C,再把C排序取前M+N的和。
Integer max=maxChoose(new int[]{9,45,10,52}, new int[]{1,55,84,6}, 2, 1);
System.out.println(max);
}
// /**
// * 根据传过来实体类,自动拼接查询Condition,如果是字符类型就用LIKE查询,如果是日期类型就用>=或是<=,其他的用=
// * @param entityClass
// * @return
// * @throws Exception
// */
// public static <T> Condition createCondition (T entityClass) throws Exception {
// Condition condition = new Condition(entityClass.getClass());
// Field[] fields = entityClass.getClass().getDeclaredFields();
// Field.setAccessible(fields, true);
// for (int i = 0; i < fields.length; i++) {
// String filedName = fields[i].getName();
// Object filedValue = fields[i].get(entityClass);
// if (filedValue != null && filedValue.toString().length() > 0) {
// if (filedValue.getClass() == String.class) {
// condition.and().andLike(filedName, "%" + filedValue.toString() + "%");
// } else if(filedValue.getClass() == Date.class){
// if (filedName.startsWith("begin") || filedName.startsWith("create")) {
// condition.and().andGreaterThanOrEqualTo(filedName, filedValue);
// }else if (filedName.startsWith("end")|| filedName.startsWith("update")){
// condition.and().andLessThanOrEqualTo(filedName, filedValue);
// }
// } else {
// condition.and().andEqualTo(filedName, filedValue);
// }
// }
// }
// return condition;
// }
// /**
// * 根据传过来实体类,自动拼接查询Example,如果是字符类型就用LIKE查询,如果是日期类型就用>=或是<=,其他的用=
// * @param entity 实体
// * @param example 需要生成的Example
// * @param <T> 实体类型
// * @param <V> Example类型
// * @throws Exception
// */
// public static <T, V> void createCriteria (T entity, V example) throws Exception {
// Field[] entityFields = entity.getClass().getDeclaredFields();
// Field.setAccessible(entityFields, true);
// Object criteria = example.getClass().getDeclaredMethod("createCriteria").invoke(example);
// Method[] exampleMethod = criteria.getClass().getDeclaredMethods();
// for (int i = 0; i < entityFields.length; i++) {
// String filedName = entityFields[i].getName();
// Object filedValue = entityFields[i].get(entity);
// if (filedName.equals("deleted")) filedValue = false;
// if (filedName.equals("warehouseCode")) filedValue = ShiroUtils.getWarehouseCode();
// if (filedValue != null && filedValue.toString().length() > 0) {
// String methodName = null;
// if (filedValue.getClass() == Integer.class || filedValue.getClass() == Boolean.class || filedName.equals("warehouseCode"))
// {
// methodName = ConvertMethodName(filedName, "EqualTo");
// }
// else if (filedValue.getClass() == String.class)
// {
// methodName = ConvertMethodName(filedName, "Like");
// filedValue = filedValue.toString() + "%";
// }
// else if(filedValue.getClass() == Date.class)
// {
// if (filedName.startsWith("begin") || filedName.startsWith("create")) {
// methodName = ConvertMethodName(filedName, "GreaterThanOrEqualTo");
// }else if (filedName.startsWith("end")|| filedName.startsWith("update")){
// methodName = ConvertMethodName(filedName, "andAddress1LessThanOrEqualTo");
// }
// }
// for (Method itemMethod : exampleMethod)
// {
// if (itemMethod.getName().equals(methodName)) {
// itemMethod.invoke(criteria, filedValue);
// }
// }
// }
// }
// }
}