litesoft
@ 965
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / foundation / client / pavment / PlaceTokenPackaging.java
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 |
package com.temp.foundation.client.pavment; import com.temp.common.shared.utils.*; import java.util.*; /** * Code to support converting a list of Strings to a single String and back. * * @author georgs */ public class PlaceTokenPackaging { /** * Pack the passed Strings into a single String. * * @return "" if null passed OR no strings, individual nulls treated as "" */ public static String pack( String... strings ) { return pack( (strings != null) ? Arrays.asList( strings ) : null ); } /** * Pack the passed Strings into a single String. * * @return "" if null passed OR no strings, individual nulls treated as "" */ public static String pack( List<String> strings ) { if ( null != (strings = normalize( strings )) ) { StringBuilder sb = new StringBuilder(); for ( String string : strings ) { appendString( sb, string ); } return sb.toString(); } return ""; } /** * Unpack the passed String into multiple Strings. * * @return empty if minimumListSize < 1 AND null or "" passed, otherwise the * list will be at least minimumListSize (or 1 if minimumListSize < * 1) and no entries will be null. */ public static List<String> unpack( String packed, int minimumListSize ) { List<String> results = new ArrayList<String>(); if ( packed != null ) { if ( (packed = packed.trim()).length() != 0 ) { CharSource cs = new CharSource( packed ); do { results.add( parseString( cs ) ); } while ( cs.anyRemaining() ); } } while ( results.size() < minimumListSize ) { results.add( "" ); } return results; } private static String parseString( CharSource cs ) { int length = IntegerCodec.decodePositive( cs ); if ( length == 0 ) { return ""; } StringBuilder sb = new StringBuilder(); while ( length-- > 0 ) { sb.append( cs.getRequired() ); } return sb.toString(); } private static void appendString( StringBuilder sb, String string ) { sb.append( IntegerCodec.encodePositive( string.length() ) ).append( string ); } private static List<String> normalize( List<String> strings ) { if ( strings != null ) { List<String> normalized = new ArrayList<String>( strings.size() ); int empties = 0; for ( String string : strings ) { string = (string != null) ? string.trim() : ""; if ( string.isEmpty() ) { empties++; } else { for (; empties > 0; empties-- ) { normalized.add( "" ); } normalized.add( string ); } } if ( !normalized.isEmpty() ) { return normalized; } } return null; } } |
Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/foundation/client/pavment/PlaceTokenPackaging.java
Revision | Author | Commited | Message |
---|---|---|---|
965 | GeorgeS | Fri 01 Aug, 2014 03:20:35 +0000 | ! |