Subversion Repository Public Repository

litesoft

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
package org.litesoft.core.util;

import java.util.*;

import org.litesoft.codec.StringCodec;

/**
 * Code to support converting a list of Strings to a single String and back.
 */
public class MultiStringPackaging
{
    /**
     * Pack the passed Strings into a single String.
     *
     * @return "" if null passed OR no strings
     */
    public static String pack( String... pStrings )
    {
        return pack( (pStrings != null) ? Arrays.asList( pStrings ) : null );
    }

    /**
     * Pack the passed Strings into a single String.
     *
     * @return "" if null passed OR no strings
     */
    public static String pack( List<String> pStrings )
    {
        if ( null != (pStrings = normalize( pStrings )) )
        {
            StringBuilder zSB = new StringBuilder();
            for ( String zString : pStrings )
            {
                zSB.append( StringCodec.INSTANCE.encode( zString ) );
            }
            return zSB.toString();
        }
        return "";
    }

    /**
     * Unpack the passed String into multiple Strings.
     *
     * @return list will be at least minimumListSize (or 1 if minimumListSize <1)
     */
    public static List<String> unpack( String pPacked, int pMinimumListSize )
    {
        List<String> zResults = new ArrayList<String>( Math.max( pMinimumListSize, 1 ) );
        if ( pPacked != null )
        {
            if ( (pPacked = pPacked.trim()).length() != 0 )
            {
                CharSource zCS = new CharSource( pPacked );
                do
                {
                    zResults.add( StringCodec.INSTANCE.decode( zCS ) );
                }
                while ( zCS.anyRemaining() );
            }
        }
        return zResults;
    }

    private static List<String> normalize( List<String> pStrings )
    {
        if ( pStrings != null )
        {
            List<String> zNormalized = new ArrayList<String>( pStrings.size() );
            int zEmpties = 0;
            for ( String zString : pStrings )
            {
                zString = (zString != null) ? zString.trim() : "";
                if ( zString.isEmpty() )
                {
                    zEmpties++;
                }
                else
                {
                    for (; zEmpties > 0; zEmpties-- )
                    {
                        zNormalized.add( "" );
                    }
                    zNormalized.add( zString );
                }
            }
            if ( !zNormalized.isEmpty() )
            {
                return zNormalized;
            }
        }
        return null;
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/util/MultiStringPackaging.java

Diff revisions: vs.
Revision Author Commited Message
767 Diff Diff GeorgeS picture GeorgeS Sat 14 Jul, 2012 18:19:39 +0000
766 Diff Diff GeorgeS picture GeorgeS Sat 14 Jul, 2012 16:38:04 +0000

!

755 GeorgeS picture GeorgeS Sun 08 Jul, 2012 21:59:08 +0000