forked from yeshodha02/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtilities.java
More file actions
194 lines (173 loc) · 6.98 KB
/
StringUtilities.java
File metadata and controls
194 lines (173 loc) · 6.98 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
package com.edurekademo.utilities;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An utility class that is used to split an string into an array list and providing mechanism to
* build a HashMap value based on a variable number of arguements.
* @author Seshagiri Sriram
* @version 1.0
*
*/
public class StringUtilities {
/**
* The string separator for splitting a string into a list
*/
private final static String COMMA_SEPARATOR = ",";
/**
* The String separator for splitting a parameter value.
* Parameters are expected to be in form: "parametername=value"
*/
private final static String PARAM_SEPARATOR = "=";
/**
* The String separator for splitting a parameter value into appropriate type.
* Required for HQL Queries, never for Native SQL queries
* Parameters are expected to be in form: "parametername=value:type (int, string, float, double)"
*/
private final static String TYPE_SEPARATOR = ";";
/**
* The String separator for splitting a date parameter value into appropriate format.
*/
private final static String DATEFORMAT_SEPARATOR = "@";
/**
* The method to be invoked to convert a given String value to a specific Object type
*/
private final static String CONVERTOR_METHOD_NAME = "valueOf" ;
/**
* The String to represent the type "DATE"
*/
private final static String DATE_TYPE = "date" ;
/**
* Default Date format to which the date will be formatted
*/
private final static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss" ;
/**
* Variable to represent the type "STRING"
*/
private final static String STRING_TYPE ="string";
/**
* Logger enabled for the current class
*/
private static final Logger LOG =LoggerFactory.getLogger(StringUtilities.class);
/** Primitive type name -> class map. */
private static final HashMap<String,Class<?>> PRIMITIVE_NAME_TYPE_MAP = new HashMap<String,Class<?>>();
/** Setup the primitives map. */
static {
PRIMITIVE_NAME_TYPE_MAP.put("boolean", Boolean.class);
PRIMITIVE_NAME_TYPE_MAP.put("int", Integer.class);
PRIMITIVE_NAME_TYPE_MAP.put("long", Long.class);
PRIMITIVE_NAME_TYPE_MAP.put("float", Float.class);
PRIMITIVE_NAME_TYPE_MAP.put("double", Double.class);
}
/**
* given a comma separated string and type, returns an ArrayList of specific types
* @param strParamValueList The string (assumed to be comma separated). Usually meant for use in creating
* parameter values for passing in IN Clauses
* @param type The type of the Arraylist passed
* @return ArrayList if passed value is not null or empty, null otherwise
*/
public static List<Object> convertStringToList(String strParamValueList,String type){
if (strParamValueList==null||strParamValueList.trim().isEmpty()) return null;
ArrayList<Object> list = new ArrayList<Object>();
String arr[] = strParamValueList.trim().split(COMMA_SEPARATOR);
for(String tmpString: arr){
list.add(convert(tmpString,type));
}
return list;
}
/**
* given a variable list of String parameters, forms a hash map
* @param strParamValueList Variable list of arguments each of format: key=kevalue;type.
* For the type date,you can even pass the dateformat value as key=keyvalue@dateformat;type.
* If dateformat is not passed default format yyyy-MM-dd HH:mm:ss will be taken.
* @return HashMap if no arguments are passed, returns an empty list, else populated hashmap
* support only int, string, boolean, float, double, long, date
*/
public static HashMap<String, Object> createParameterList(String... strParamValueList){
HashMap<String, Object> hMap = new HashMap<String, Object>();
for(String strArg: strParamValueList){
String type = null;
if(strArg.contains(TYPE_SEPARATOR)){
type = strArg.split(TYPE_SEPARATOR)[1];
strArg = strArg.split(TYPE_SEPARATOR)[0];
}
if (strArg.contains(PARAM_SEPARATOR)){
String arr[] = strArg.split(PARAM_SEPARATOR);
if (arr[1].contains(COMMA_SEPARATOR)){
hMap.put(arr[0], convertStringToList(arr[1],type));
}
else {
hMap.put(arr[0], convert(arr[1],type));
}
}
}
return hMap;
}
/**
* Converts the given String value to the intended type of Object
* @param value The String value to be converted
* @param type The type to which the value needs to be converted
* @return Object Returns values as such if type or value is empty or null,else returns the converted Object
*/
private static Object convert(String value, String types) {
Class<?> finalClass = null ;
//If value or type passed is null or empty or string return back value as such
if ((value == null) || value.isEmpty() || types == null || types.isEmpty() || types.equalsIgnoreCase(STRING_TYPE)) return value;
String type = types.toLowerCase();
if (type.equals(DATE_TYPE)) return convertStringToDate(value);
//Based on the passed type load the wrapper class.
//If the given type not permitted returns values as such
if(PRIMITIVE_NAME_TYPE_MAP.containsKey(type))
finalClass = PRIMITIVE_NAME_TYPE_MAP.get(type);
try {
//Invoking the valueOf method of the Wrapper Class dynamically using reflection
if(finalClass!=null){
Method method = finalClass.getMethod(CONVERTOR_METHOD_NAME, String.class);
int mods = method.getModifiers();
if (Modifier.isStatic(mods) && Modifier.isPublic(mods)) {
return method.invoke(null, value);
}
}
}
catch (NoSuchMethodException e) {
LoggerStackTraceUtil.printErrorMessage(e);
}
catch (IllegalAccessException e) {
// this won't happen
LoggerStackTraceUtil.printErrorMessage(e);
}
catch (InvocationTargetException e) {
// when this happens, the string cannot be converted to the intended type
// we are ignoring it here - the original string will be returned
LoggerStackTraceUtil.printErrorMessage(e);
}
return value;
}
/**
* Convert the given date value in string to date object
* @param dateString The date to be formatted
* @return Object Returns the corresponding Date object
*/
private static Object convertStringToDate(String dateString) {
String dateFormat = null;
Object finalDate = null;
String dateStringVal=null;
//If the incoming date string contains the format as well parse using the given format, else parse using default
dateFormat = (dateString.contains(DATEFORMAT_SEPARATOR)) ? dateString.split(DATEFORMAT_SEPARATOR)[1] : DATE_FORMAT ;
dateStringVal = (dateString.contains(DATEFORMAT_SEPARATOR)) ? dateString.split(DATEFORMAT_SEPARATOR)[0] : dateString ;
SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);
try{
finalDate = dateFormatter.parse(dateStringVal);
}catch(ParseException e){
LoggerStackTraceUtil.printErrorMessage(e);
}
return finalDate;
}
}