|
@@ -1,11 +1,8 @@ |
1 |
1 |
|
package com.temp.client.foundation.pavment; |
2 |
2 |
|
|
3 |
|
- |
import java.util.ArrayList; |
4 |
|
- |
import java.util.Arrays; |
5 |
|
- |
import java.util.List; |
|
3 |
+ |
import com.temp.shared.utils.*; |
6 |
4 |
|
|
7 |
|
- |
import com.temp.shared.utils.CharSource; |
8 |
|
- |
import com.temp.shared.utils.IntegerCodec; |
|
5 |
+ |
import java.util.*; |
9 |
6 |
|
|
10 |
7 |
|
/** |
11 |
8 |
|
* Code to support converting a list of Strings to a single String and back. |
|
@@ -19,8 +16,8 @@ |
19 |
16 |
|
* |
20 |
17 |
|
* @return "" if null passed OR no strings, individual nulls treated as "" |
21 |
18 |
|
*/ |
22 |
|
- |
public static String pack(String... strings) { |
23 |
|
- |
return pack((strings != null) ? Arrays.asList(strings) : null); |
|
19 |
+ |
public static String pack( String... strings ) { |
|
20 |
+ |
return pack( (strings != null) ? Arrays.asList( strings ) : null ); |
24 |
21 |
|
} |
25 |
22 |
|
|
26 |
23 |
|
/** |
|
@@ -28,11 +25,11 @@ |
28 |
25 |
|
* |
29 |
26 |
|
* @return "" if null passed OR no strings, individual nulls treated as "" |
30 |
27 |
|
*/ |
31 |
|
- |
public static String pack(List<String> strings) { |
32 |
|
- |
if (null != (strings = normalize(strings))) { |
|
28 |
+ |
public static String pack( List<String> strings ) { |
|
29 |
+ |
if ( null != (strings = normalize( strings )) ) { |
33 |
30 |
|
StringBuilder sb = new StringBuilder(); |
34 |
|
- |
for (String string : strings) { |
35 |
|
- |
appendString(sb, string); |
|
31 |
+ |
for ( String string : strings ) { |
|
32 |
+ |
appendString( sb, string ); |
36 |
33 |
|
} |
37 |
34 |
|
return sb.toString(); |
38 |
35 |
|
} |
|
@@ -43,57 +40,57 @@ |
43 |
40 |
|
* Unpack the passed String into multiple Strings. |
44 |
41 |
|
* |
45 |
42 |
|
* @return empty if minimumListSize < 1 AND null or "" passed, otherwise the |
46 |
|
- |
* list will be at least minimumListSize (or 1 if minimumListSize < |
47 |
|
- |
* 1) and no entries will be null. |
|
43 |
+ |
* list will be at least minimumListSize (or 1 if minimumListSize < |
|
44 |
+ |
* 1) and no entries will be null. |
48 |
45 |
|
*/ |
49 |
|
- |
public static List<String> unpack(String packed, int minimumListSize) { |
|
46 |
+ |
public static List<String> unpack( String packed, int minimumListSize ) { |
50 |
47 |
|
List<String> results = new ArrayList<String>(); |
51 |
|
- |
if (packed != null) { |
52 |
|
- |
if ((packed = packed.trim()).length() != 0) { |
53 |
|
- |
CharSource cs = new CharSource(packed); |
|
48 |
+ |
if ( packed != null ) { |
|
49 |
+ |
if ( (packed = packed.trim()).length() != 0 ) { |
|
50 |
+ |
CharSource cs = new CharSource( packed ); |
54 |
51 |
|
do { |
55 |
|
- |
results.add(parseString(cs)); |
56 |
|
- |
} while (cs.anyRemaining()); |
|
52 |
+ |
results.add( parseString( cs ) ); |
|
53 |
+ |
} while ( cs.anyRemaining() ); |
57 |
54 |
|
} |
58 |
55 |
|
} |
59 |
|
- |
while (results.size() < minimumListSize) { |
60 |
|
- |
results.add(""); |
|
56 |
+ |
while ( results.size() < minimumListSize ) { |
|
57 |
+ |
results.add( "" ); |
61 |
58 |
|
} |
62 |
59 |
|
return results; |
63 |
60 |
|
} |
64 |
61 |
|
|
65 |
|
- |
private static String parseString(CharSource cs) { |
66 |
|
- |
int length = IntegerCodec.decodePositive(cs); |
67 |
|
- |
if (length == 0) { |
|
62 |
+ |
private static String parseString( CharSource cs ) { |
|
63 |
+ |
int length = IntegerCodec.decodePositive( cs ); |
|
64 |
+ |
if ( length == 0 ) { |
68 |
65 |
|
return ""; |
69 |
66 |
|
} |
70 |
67 |
|
StringBuilder sb = new StringBuilder(); |
71 |
|
- |
while (length-- > 0) { |
72 |
|
- |
sb.append(cs.getRequired()); |
|
68 |
+ |
while ( length-- > 0 ) { |
|
69 |
+ |
sb.append( cs.getRequired() ); |
73 |
70 |
|
} |
74 |
71 |
|
return sb.toString(); |
75 |
72 |
|
} |
76 |
73 |
|
|
77 |
|
- |
private static void appendString(StringBuilder sb, String string) { |
78 |
|
- |
sb.append(IntegerCodec.encodePositive(string.length())).append(string); |
|
74 |
+ |
private static void appendString( StringBuilder sb, String string ) { |
|
75 |
+ |
sb.append( IntegerCodec.encodePositive( string.length() ) ).append( string ); |
79 |
76 |
|
} |
80 |
77 |
|
|
81 |
|
- |
private static List<String> normalize(List<String> strings) { |
82 |
|
- |
if (strings != null) { |
83 |
|
- |
List<String> normalized = new ArrayList<String>(strings.size()); |
|
78 |
+ |
private static List<String> normalize( List<String> strings ) { |
|
79 |
+ |
if ( strings != null ) { |
|
80 |
+ |
List<String> normalized = new ArrayList<String>( strings.size() ); |
84 |
81 |
|
int empties = 0; |
85 |
|
- |
for (String string : strings) { |
|
82 |
+ |
for ( String string : strings ) { |
86 |
83 |
|
string = (string != null) ? string.trim() : ""; |
87 |
|
- |
if (string.isEmpty()) { |
|
84 |
+ |
if ( string.isEmpty() ) { |
88 |
85 |
|
empties++; |
89 |
86 |
|
} else { |
90 |
|
- |
for (; empties > 0; empties--) { |
91 |
|
- |
normalized.add(""); |
|
87 |
+ |
for (; empties > 0; empties-- ) { |
|
88 |
+ |
normalized.add( "" ); |
92 |
89 |
|
} |
93 |
|
- |
normalized.add(string); |
|
90 |
+ |
normalized.add( string ); |
94 |
91 |
|
} |
95 |
92 |
|
} |
96 |
|
- |
if (!normalized.isEmpty()) { |
|
93 |
+ |
if ( !normalized.isEmpty() ) { |
97 |
94 |
|
return normalized; |
98 |
95 |
|
} |
99 |
96 |
|
} |