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
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
// This Source Code is in the Public Domain per: http://unlicense.org
package org.litesoft.core.util.text;

import java.util.*;

import org.litesoft.core.simpletypes.*;
import org.litesoft.core.util.*;

public class CodedTextAccessor<T extends CodedText>
{
    private Map<Character, List<T>> mCodedTextsByCodeFirstChar = new HashMap<Character, List<T>>();

    public CodedTextAccessor( List<T> pCodedTexts )
    {
        if ( (pCodedTexts != null) && !pCodedTexts.isEmpty() )
        {
            List<T> zAll = new ArrayList<T>( pCodedTexts ); // copy to injsure that we can sort it
            Collections.sort( zAll );

            Helper zHelper = new Helper();
            for ( T zCodedText : zAll )
            {
                zHelper.process( zCodedText );
            }
            zHelper.fini();
        }
    }

    public CodedTextAccessor( T... pCodedTexts )
    {
        if ( (pCodedTexts != null) && (pCodedTexts.length != 0) )
        {
            Arrays.sort( pCodedTexts );

            Helper zHelper = new Helper();
            for ( T zCodedText : pCodedTexts )
            {
                zHelper.process( zCodedText );
            }
            zHelper.fini();
        }
    }

    public CodedTextAccessor( String... pCodes )
    {
        if ( (pCodes != null) && (pCodes.length != 0) )
        {
            Arrays.sort( pCodes );

            Helper zHelper = new Helper();
            for ( String zCode : pCodes )
            {
                zHelper.process( createSame( zCode ) );
            }
            zHelper.fini();
        }
    }

    public List<T> getListFor( String pCodeStartsWith )
    {
        if ( (pCodeStartsWith != null) && (pCodeStartsWith.length() != 0) )
        {
            pCodeStartsWith = pCodeStartsWith.toUpperCase();
            List<T> zList = mCodedTextsByCodeFirstChar.get( pCodeStartsWith.charAt( 0 ) );
            if ( zList != null )
            {
                return (pCodeStartsWith.length() == 1) ? zList : createFilteredList( zList, pCodeStartsWith );
            }
        }
        return ImmutableArrayList.emptyList();
    }

    private List<T> createFilteredList( List<T> pList, String pCodeStartsWith )
    {
        ArrayList<T> zList = new ArrayList<T>();
        for ( T zView : pList )
        {
            if ( zView.getCode().startsWith( pCodeStartsWith ) )
            {
                zList.add( zView );
            }
        }
        return zList;
    }

    public T getCodedTextFor( String pCode )
    {
        if ( (pCode == null) || (pCode.length() == 0) )
        {
            return null;
        }
        String zCode = pCode.toUpperCase();
        List<T> zList = mCodedTextsByCodeFirstChar.get( zCode.charAt( 0 ) );
        if ( zList != null )
        {
            for ( T zView : zList )
            {
                if ( zView.getCode().equals( zCode ) )
                {
                    return zView;
                }
            }
        }
        return createSame( pCode );
    }

    @SuppressWarnings({"unchecked"})
    private T createSame( String pCode )
    {
        return (T) createRawSame( pCode );
    }

    private static CodedText createRawSame( String pCode )
    {
        if ( pCode.length() == 1 )
        {
            switch ( pCode.charAt( 0 ) )
            {
                case ' ':
                    return CodedText.SPACE;
                case ',':
                    return CodedText.SPACE;
                case '.':
                    return CodedText.PERIOD;
                default:
                    break;
            }
        }
        return new CodedText.Same( pCode );
    }

    private class Helper
    {
        private char mCurChar = ' ';
        private List<T> mCurList = new ArrayList<T>();

        public void process( T pCodedText )
        {
            String zCode = pCodedText.getCode();
            if ( (zCode != null) && (zCode.length() > 0) )
            {
                char zChar = zCode.charAt( 0 );
                if ( zChar != mCurChar )
                {
                    if ( !mCurList.isEmpty() )
                    {
                        mCodedTextsByCodeFirstChar.put( mCurChar, mCurList );
                        mCurList = new ArrayList<T>();
                    }
                    mCurChar = zChar;
                }
                mCurList.add( pCodedText );
            }
        }

        public void fini()
        {
            if ( !mCurList.isEmpty() )
            {
                mCodedTextsByCodeFirstChar.put( mCurChar, mCurList );
            }
        }
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
947 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 23:36:56 +0000

Correct Spelling of package!

49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000