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.*;

public class CodedTextManager<T extends CodedText>
{
    private CodedTextAccessor<T> mCTA;
    private List<T> mCurrent = new ArrayList<T>();

    public CodedTextManager( CodedTextAccessor<T> pCTA )
    {
        mCTA = pCTA;
    }

    @Override
    public String toString()
    {
        if ( mCurrent.isEmpty() )
        {
            return "";
        }
        StringBuilder zSB = new StringBuilder();
        for ( T zT : mCurrent )
        {
            zSB.append( zT.getText() );
        }
        return zSB.toString();
    }

    /**
     * Update the current codedText List appropriately
     *
     * @return null if more than one code changed, otherwise the 'code' of the changed entry
     *         (if the only change was the addition of a punctuation entry, then return the one just before it)
     *         (if the only change was the elimination of a punctuation entry, then return the one just before it)
     *         (if the change consisted of the elimination of a punctuation entry, and the entry just before or just after changed,
     *         then return the changed one)
     */
    public String update( String pCoded )
    {
        List<String> zCodes = CodedTextParser.parse( pCoded );
        if ( !zCodes.isEmpty() )
        {
            switch ( zCodes.size() - mCurrent.size() )
            {
                case 0:
                    return processSameLength( zCodes );
                case -1:
                    return processRemovedOne( zCodes );
                case 1:
                    return processAddedOne( zCodes );
                default:
                    break;
            }
        }
        return processRebuild( zCodes );
    }

    private static class Indexes
    {
        private int mCode, mCurrent;

        private Indexes( int pCode, int pCurrent )
        {
            mCode = pCode;
            mCurrent = pCurrent;
        }

        public int getCode()
        {
            return mCode;
        }

        public int getCurrent()
        {
            return mCurrent;
        }
    }

    private Indexes determineChanged( List<String> pCodes, int pFromCodes, int pToCodesExcluded, int pFromCurrent, int pToCurrentExcluded, int pDelta )
    {
        while ( pCodes.get( pFromCodes ).equals( mCurrent.get( pFromCurrent ).getCode() ) )
        {
            pFromCodes += pDelta;
            pFromCurrent += pDelta;
            if ( (pFromCodes == pToCodesExcluded) || pFromCurrent == pToCurrentExcluded )
            {
                break;
            }
        }
        return new Indexes( pFromCodes, pFromCurrent );
    }

    private Indexes determineChangedForward( List<String> pCodes )
    {
        return determineChanged( pCodes, 0, pCodes.size(), 0, mCurrent.size(), 1 );
    }

    private Indexes determineChangedBackward( List<String> pCodes )
    {

        return determineChanged( pCodes, pCodes.size() - 1, -1, mCurrent.size() - 1, -1, -1 );
    }

    private String processRebuild( List<String> pCodes )
    {
        mCurrent.clear();
        for ( String zCode : pCodes )
        {
            mCurrent.add( mCTA.getCodedTextFor( zCode ) );
        }
        return null;
    }

    private String processSameLength( List<String> pCodes )
    {
        List<String> zChangedCodes = new ArrayList<String>();
        for ( int i = 0; i < mCurrent.size(); i++ )
        {
            String zCurCode = mCurrent.get( i ).getCode();
            String zNewCode = pCodes.get( i );
            if ( !zCurCode.equals( zNewCode ) )
            {
                zChangedCodes.add( zNewCode );
                mCurrent.set( i, mCTA.getCodedTextFor( zNewCode ) );
            }
        }
        return (zChangedCodes.size() == 1) ? zChangedCodes.get( 0 ) : null;
    }

    /**
     * @return null if more than one code changed, otherwise:
     *         (if the only change was the addition of a punctuation entry, then return the one just before it)
     */
    private String processAddedOne( List<String> pCodes )
    {
        if ( pCodes.size() == 1 )
        {
            processRebuild( pCodes );
            return pCodes.get( 0 );
        }
        Indexes zForwardIndex = determineChangedForward( pCodes );
        Indexes zBackwardIndex = determineChangedBackward( pCodes );
        //To change body of created methods use File | Settings | File Templates.
        return null;
    }

    /**
     * @return null if more than one code changed, otherwise:
     *         (if the only change was the elimination of a punctuation entry, then return the one just before it)
     *         (if the change consisted of the elimination of a punctuation entry, and the entry just before or just after changed,
     *         then return the changed one)
     */
    private String processRemovedOne( List<String> pCodes )
    {
        Indexes zForwardIndex = determineChangedForward( pCodes );
        Indexes zBackwardIndex = determineChangedBackward( pCodes );
        //To change body of created methods use File | Settings | File Templates.
        return null;
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/util/text/CodedTextManager.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