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

import org.litesoft.commonfoundation.base.*;
import org.litesoft.commonfoundation.typeutils.*;

public class SortableBuilder
{
    public static final String INITIAL_VALUE = "m";

    private static final char SORT_BETWEEN_LOWEST = 'a';
    private static final char SORT_BETWEEN_HIGHEST = 'z';

    /**
     * For creating the new Insert to Front Value.
     *
     * @param pCurrentFirst !null & !""
     *
     * @return the new First Value unless the pCurrentFirst is all 'SORT_BETWEEN_LOWEST's then the pCurrentFirst + "m"
     */
    public static String newFirstBefore( String pCurrentFirst )
    {
        return createBetween( ConstantCharSource.LOWEST, //
                              new StringCharSource( "CurrentFirst", pCurrentFirst, SORT_BETWEEN_HIGHEST ) );
    }

    /**
     * For creating the new Insert Between Value.
     *
     * @param pCurrentBefore !null & !"" & must be <= pCurrentAfter
     * @param pCurrentAfter  !null & !"" & must be >= pCurrentBefore
     *
     * @return the Between value or if they are the same then pCurrentBefore
     */
    public static String newBetween( String pCurrentBefore, String pCurrentAfter )
    {
        StringCharSource low = new StringCharSource( "CurrentBefore", pCurrentBefore, SORT_BETWEEN_LOWEST );
        StringCharSource high = new StringCharSource( "CurrentAfter", pCurrentAfter, SORT_BETWEEN_HIGHEST );

        if ( pCurrentBefore.equals( pCurrentAfter ) )
        {
            return pCurrentBefore;
        }
        if ( Compare.firstGreaterThanSecond( pCurrentBefore, pCurrentAfter ) )
        {
            throw new IllegalArgumentException( "Out of order: " + pCurrentBefore + " , " + pCurrentAfter );
        }
        return createBetween( low, high );
    }

    /**
     * For creating the new Append to End value.
     *
     * @param pCurrentLast !null & !""
     *
     * @return the new Last Value
     */
    public static String newLastAfter( String pCurrentLast )
    {
        return createBetween( new StringCharSource( "CurrentLast", pCurrentLast, SORT_BETWEEN_LOWEST ), //
                              ConstantCharSource.HIGHEST );
    }

    private static String createBetween( CharSource pLow, CharSource pHigh )
    {
        StringBuilder between = new StringBuilder();

        char low = pLow.nextChar();
        char high = pHigh.nextChar();
        while ( low == high )
        {
            between.append( low );
            low = pLow.nextChar();
            high = pHigh.nextChar();
        }
        if ( high - low == 1 )
        {
            do
            {
                between.append( low );
                low = pLow.nextChar();
                high = SORT_BETWEEN_HIGHEST;
            }
            while ( low == high );
        }
        char c = (char) (((high - low) / 2) + low);
        between.append( c );
        return between.toString();
    }

    private interface CharSource
    {
        char nextChar();
    }

    private static class StringCharSource implements CharSource
    {
        private String mString;
        private char mBeyoundEnd;
        private int mIndex = 0;

        public StringCharSource( String pWhat, String pString, char pBeyoundEnd )
        {
            mString = Strings.assertNotNullNotEmpty( pWhat, pString );
            mBeyoundEnd = pBeyoundEnd;
        }

        @Override
        public char nextChar()
        {
            return (mIndex < mString.length()) ? mString.charAt( mIndex++ ) : mBeyoundEnd;
        }
    }

    private static class ConstantCharSource implements CharSource
    {
        public static final CharSource LOWEST = new ConstantCharSource( SORT_BETWEEN_LOWEST );
        public static final CharSource HIGHEST = new ConstantCharSource( SORT_BETWEEN_HIGHEST );

        private char mChar;

        private ConstantCharSource( char pChar )
        {
            mChar = pChar;
        }

        @Override
        public char nextChar()
        {
            return mChar;
        }
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

821 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 00:08:41 +0000
805 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:53:09 +0000
50 Diff Diff GeorgeS picture GeorgeS Tue 13 Apr, 2010 11:51:38 +0000
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