88import java .util .List ;
99import java .util .Map ;
1010
11+ import static com .zeroone .conceal .model .Constant .UTF8 ;
12+
1113/**
1214 * @author : hafiq on 23/03/2017.
1315 */
1416
1517class ConverterListUtils {
1618
1719 static List <Integer > toIntArray (String string ) {
18- String [] strings = string . replace ( "[" , "" ). replace ( "]" , "" ). split ( ", " );
20+ String [] strings = getArray ( string );
1921 Integer result [] = new Integer [strings .length ];
2022 try {
2123 for (int i = 0 ; i < result .length ; i ++) {
@@ -29,7 +31,7 @@ static List<Integer> toIntArray(String string) {
2931 }
3032
3133 static List <Boolean > toBooleanArray (String string ) {
32- String [] strings = string . replace ( "[" , "" ). replace ( "]" , "" ). split ( ", " );
34+ String [] strings = getArray ( string );
3335 Boolean result [] = new Boolean [strings .length ];
3436 try {
3537 for (int i = 0 ; i < result .length ; i ++) {
@@ -43,7 +45,7 @@ static List<Boolean> toBooleanArray(String string) {
4345 }
4446
4547 static List <Long > toLongArray (String string ) {
46- String [] strings = string . replace ( "[" , "" ). replace ( "]" , "" ). split ( ", " );
48+ String [] strings = getArray ( string );
4749 Long result [] = new Long [strings .length ];
4850 try {
4951 for (int i = 0 ; i < result .length ; i ++) {
@@ -58,7 +60,7 @@ static List<Long> toLongArray(String string) {
5860 }
5961
6062 static List <Double > toDoubleArray (String string ) {
61- String [] strings = string . replace ( "[" , "" ). replace ( "]" , "" ). split ( ", " );
63+ String [] strings = getArray ( string );
6264 Double result [] = new Double [strings .length ];
6365 try {
6466 for (int i = 0 ; i < result .length ; i ++) {
@@ -73,7 +75,7 @@ static List<Double> toDoubleArray(String string) {
7375 }
7476
7577 static List <Float > toFloatArray (String string ) {
76- String [] strings = string . replace ( "[" , "" ). replace ( "]" , "" ). split ( ", " );
78+ String [] strings = getArray ( string );
7779 Float result [] = new Float [strings .length ];
7880 try {
7981 for (int i = 0 ; i < result .length ; i ++) {
@@ -87,7 +89,11 @@ static List<Float> toFloatArray(String string) {
8789 }
8890
8991 static List <String > toStringArray (String string ) {
90- return Arrays .asList (string .replace ("[" , "" ).replace ("]" , "" ).split (", " ));
92+ return Arrays .asList (getArray (string ));
93+ }
94+
95+ static String [] getArray (String string ) {
96+ return string .replace ("[" , "" ).replace ("]" , "" ).split (", " );
9197 }
9298
9399
@@ -100,9 +106,9 @@ static String convertMapToString(Map<String,String> maps){
100106 }
101107 String value = maps .get (key );
102108 try {
103- stringBuilder .append ((key != null ? URLEncoder .encode (key , "UTF-8" ) : "" ));
109+ stringBuilder .append ((key != null ? URLEncoder .encode (key , UTF8 ) : "" ));
104110 stringBuilder .append ("=" );
105- stringBuilder .append (value != null ? URLEncoder .encode (value , "UTF-8" ) : "" );
111+ stringBuilder .append (value != null ? URLEncoder .encode (value , UTF8 ) : "" );
106112 } catch (UnsupportedEncodingException e ) {
107113 throw new RuntimeException ("This method requires UTF-8 encoding support" , e );
108114 }
@@ -118,7 +124,7 @@ static LinkedHashMap<String,String> convertStringToMap(String input){
118124 for (String nameValuePair : nameValuePairs ) {
119125 String [] nameValue = nameValuePair .split ("=" );
120126 try {
121- map .put (URLDecoder .decode (nameValue [0 ], "UTF-8" ), nameValue .length > 1 ? URLDecoder .decode (nameValue [1 ], "UTF-8" ) : "" );
127+ map .put (URLDecoder .decode (nameValue [0 ], UTF8 ), nameValue .length > 1 ? URLDecoder .decode (nameValue [1 ], UTF8 ) : "" );
122128 } catch (UnsupportedEncodingException e ) {
123129 throw new RuntimeException ("This method requires UTF-8 encoding support" , e );
124130 }
0 commit comments