|
@@ -1,7 +1,6 @@ |
1 |
1 |
|
package com.temp.client.foundation.pavment; |
2 |
2 |
|
|
3 |
|
- |
import com.temp.shared.utils.CharSource; |
4 |
|
- |
import com.temp.shared.utils.SixBitCodec; |
|
3 |
+ |
import com.temp.shared.utils.*; |
5 |
4 |
|
|
6 |
5 |
|
/** |
7 |
6 |
|
* Implementation that supports an encoding model of encoded Deltas from one |
|
@@ -16,33 +15,33 @@ |
16 |
15 |
|
* |
17 |
16 |
|
* @return "" if null passed in, otherwise the Encoded String. |
18 |
17 |
|
*/ |
19 |
|
- |
public final String encode(String unencoded) { |
20 |
|
- |
if ((unencoded == null) || (unencoded.length() == 0)) { |
|
18 |
+ |
public final String encode( String unencoded ) { |
|
19 |
+ |
if ( (unencoded == null) || (unencoded.length() == 0) ) { |
21 |
20 |
|
return ""; |
22 |
21 |
|
} |
23 |
|
- |
char initialCharacter = SixBitCodec.encode((int) (63 & System.currentTimeMillis())); |
24 |
|
- |
return new Helper(new CharSource(unencoded), initialCharacter, determinePerCharacterAdjustment(initialCharacter)).encode(); |
|
22 |
+ |
char initialCharacter = SixBitCodec.encode( (int) (63 & System.currentTimeMillis()) ); |
|
23 |
+ |
return new Helper( new CharSource( unencoded ), initialCharacter, determinePerCharacterAdjustment( initialCharacter ) ).encode(); |
25 |
24 |
|
} |
26 |
25 |
|
|
27 |
|
- |
abstract protected int determinePerCharacterAdjustment(char initialCharacter); |
|
26 |
+ |
abstract protected int determinePerCharacterAdjustment( char initialCharacter ); |
28 |
27 |
|
|
29 |
28 |
|
/** |
30 |
29 |
|
* Decode the previously "encoded" data, null or bad encoding will return |
31 |
30 |
|
* null. |
32 |
31 |
|
* |
33 |
32 |
|
* @return null indicates that the data was either null or contained |
34 |
|
- |
* unacceptable characters for decoding. |
|
33 |
+ |
* unacceptable characters for decoding. |
35 |
34 |
|
*/ |
36 |
|
- |
public final String decode(String encoded) { |
37 |
|
- |
if (encoded == null) { |
|
35 |
+ |
public final String decode( String encoded ) { |
|
36 |
+ |
if ( encoded == null ) { |
38 |
37 |
|
return null; |
39 |
38 |
|
} |
40 |
|
- |
if (encoded.length() == 0) { |
|
39 |
+ |
if ( encoded.length() == 0 ) { |
41 |
40 |
|
return ""; |
42 |
41 |
|
} |
43 |
|
- |
CharSource charSource = new CharSource(encoded); |
|
42 |
+ |
CharSource charSource = new CharSource( encoded ); |
44 |
43 |
|
char initialCharacter = charSource.getRequired(); |
45 |
|
- |
return new Helper(charSource, initialCharacter, determinePerCharacterAdjustment(initialCharacter)).decode(); |
|
44 |
+ |
return new Helper( charSource, initialCharacter, determinePerCharacterAdjustment( initialCharacter ) ).decode(); |
46 |
45 |
|
} |
47 |
46 |
|
|
48 |
47 |
|
private static class Helper { |
|
@@ -54,24 +53,23 @@ |
54 |
53 |
|
private int deltaRunning = 0; |
55 |
54 |
|
private int charDeltasRemaining = 0; |
56 |
55 |
|
|
57 |
|
- |
|
58 |
|
- |
public Helper(CharSource charSource, char firstChar, int perCharAdjustment) { |
|
56 |
+ |
public Helper( CharSource charSource, char firstChar, int perCharAdjustment ) { |
59 |
57 |
|
this.charSource = charSource; |
60 |
58 |
|
this.perCharAdjustment = perCharAdjustment; |
61 |
59 |
|
this.lastClearTextChar = firstChar; |
62 |
60 |
|
} |
63 |
61 |
|
|
64 |
62 |
|
public String encode() { |
65 |
|
- |
mSB.append((char) lastClearTextChar); |
66 |
|
- |
while (charSource.anyRemaining()) { |
67 |
|
- |
encode(charSource.get()); |
|
63 |
+ |
mSB.append( (char) lastClearTextChar ); |
|
64 |
+ |
while ( charSource.anyRemaining() ) { |
|
65 |
+ |
encode( charSource.get() ); |
68 |
66 |
|
} |
69 |
67 |
|
return mSB.toString(); |
70 |
68 |
|
} |
71 |
69 |
|
|
72 |
70 |
|
public String decode() { |
73 |
|
- |
while (charSource.anyRemaining()) { |
74 |
|
- |
if (!decode(charSource.get())) { |
|
71 |
+ |
while ( charSource.anyRemaining() ) { |
|
72 |
+ |
if ( !decode( charSource.get() ) ) { |
75 |
73 |
|
return null; |
76 |
74 |
|
} |
77 |
75 |
|
} |
|
@@ -79,45 +77,45 @@ |
79 |
77 |
|
return (charDeltasRemaining == 0) ? mSB.toString() : null; |
80 |
78 |
|
} |
81 |
79 |
|
|
82 |
|
- |
private void encode(int chr) { |
|
80 |
+ |
private void encode( int chr ) { |
83 |
81 |
|
int delta = (chr - lastClearTextChar) + perCharAdjustment; |
84 |
|
- |
if (delta < 0) { |
85 |
|
- |
LLencode(-delta, 'a'); |
|
82 |
+ |
if ( delta < 0 ) { |
|
83 |
+ |
LLencode( -delta, 'a' ); |
86 |
84 |
|
} else { |
87 |
|
- |
LLencode(delta, 'A'); |
|
85 |
+ |
LLencode( delta, 'A' ); |
88 |
86 |
|
} |
89 |
87 |
|
lastClearTextChar = chr; |
90 |
88 |
|
} |
91 |
89 |
|
|
92 |
|
- |
private void LLencode(int delta, int base) { |
93 |
|
- |
if (div4 <= delta) { |
94 |
|
- |
mSB.append('4'); |
95 |
|
- |
delta = addFactor(delta, base, div4); |
96 |
|
- |
delta = addFactor(delta, base, div3); |
97 |
|
- |
delta = addFactor(delta, base, div2); |
98 |
|
- |
} else if (div3 <= delta) { |
99 |
|
- |
mSB.append('3'); |
100 |
|
- |
delta = addFactor(delta, base, div3); |
101 |
|
- |
delta = addFactor(delta, base, div2); |
102 |
|
- |
} else if (div2 <= delta) { |
103 |
|
- |
mSB.append('2'); |
104 |
|
- |
delta = addFactor(delta, base, div2); |
|
90 |
+ |
private void LLencode( int delta, int base ) { |
|
91 |
+ |
if ( div4 <= delta ) { |
|
92 |
+ |
mSB.append( '4' ); |
|
93 |
+ |
delta = addFactor( delta, base, div4 ); |
|
94 |
+ |
delta = addFactor( delta, base, div3 ); |
|
95 |
+ |
delta = addFactor( delta, base, div2 ); |
|
96 |
+ |
} else if ( div3 <= delta ) { |
|
97 |
+ |
mSB.append( '3' ); |
|
98 |
+ |
delta = addFactor( delta, base, div3 ); |
|
99 |
+ |
delta = addFactor( delta, base, div2 ); |
|
100 |
+ |
} else if ( div2 <= delta ) { |
|
101 |
+ |
mSB.append( '2' ); |
|
102 |
+ |
delta = addFactor( delta, base, div2 ); |
105 |
103 |
|
} |
106 |
|
- |
LLencodeChar(delta, base); |
|
104 |
+ |
LLencodeChar( delta, base ); |
107 |
105 |
|
} |
108 |
106 |
|
|
109 |
|
- |
private int addFactor(int delta, int base, int divisor) { |
|
107 |
+ |
private int addFactor( int delta, int base, int divisor ) { |
110 |
108 |
|
int zDelta = 0; |
111 |
|
- |
while (delta >= divisor) { |
|
109 |
+ |
while ( delta >= divisor ) { |
112 |
110 |
|
zDelta++; |
113 |
111 |
|
delta -= divisor; |
114 |
112 |
|
} |
115 |
|
- |
LLencodeChar(zDelta, base); |
|
113 |
+ |
LLencodeChar( zDelta, base ); |
116 |
114 |
|
return delta; |
117 |
115 |
|
} |
118 |
116 |
|
|
119 |
|
- |
private void LLencodeChar(int delta, int base) { |
120 |
|
- |
mSB.append((delta == 0) ? '0' : (char) (base + (delta - 1))); |
|
117 |
+ |
private void LLencodeChar( int delta, int base ) { |
|
118 |
+ |
mSB.append( (delta == 0) ? '0' : (char) (base + (delta - 1)) ); |
121 |
119 |
|
} |
122 |
120 |
|
|
123 |
121 |
|
// 0 = No Change / same letter |
|
@@ -128,45 +126,45 @@ |
128 |
126 |
|
private static final int div3 = 729; // 27 * 27; |
129 |
127 |
|
private static final int div2 = 27; |
130 |
128 |
|
|
131 |
|
- |
public boolean decode(int chr) { |
132 |
|
- |
if (charDeltasRemaining > 0) { |
133 |
|
- |
Integer delta = LLdecode(chr); |
134 |
|
- |
if (delta == null) { |
|
129 |
+ |
public boolean decode( int chr ) { |
|
130 |
+ |
if ( charDeltasRemaining > 0 ) { |
|
131 |
+ |
Integer delta = LLdecode( chr ); |
|
132 |
+ |
if ( delta == null ) { |
135 |
133 |
|
return false; |
136 |
134 |
|
} |
137 |
135 |
|
deltaRunning = (deltaRunning * 27) + delta; |
138 |
|
- |
if (--charDeltasRemaining == 0) { |
139 |
|
- |
if (!LLdecodeChar(deltaRunning)) { |
|
136 |
+ |
if ( --charDeltasRemaining == 0 ) { |
|
137 |
+ |
if ( !LLdecodeChar( deltaRunning ) ) { |
140 |
138 |
|
return false; |
141 |
139 |
|
} |
142 |
140 |
|
deltaRunning = 0; |
143 |
141 |
|
} |
144 |
142 |
|
return true; |
145 |
143 |
|
} |
146 |
|
- |
if (('2' <= chr) && (chr <= '4')) { |
|
144 |
+ |
if ( ('2' <= chr) && (chr <= '4') ) { |
147 |
145 |
|
charDeltasRemaining = chr - '0'; |
148 |
146 |
|
return true; |
149 |
147 |
|
} |
150 |
|
- |
return LLdecodeChar(LLdecode(chr)); |
|
148 |
+ |
return LLdecodeChar( LLdecode( chr ) ); |
151 |
149 |
|
} |
152 |
150 |
|
|
153 |
|
- |
private boolean LLdecodeChar(Integer delta) { |
154 |
|
- |
if (delta == null) { |
|
151 |
+ |
private boolean LLdecodeChar( Integer delta ) { |
|
152 |
+ |
if ( delta == null ) { |
155 |
153 |
|
return false; |
156 |
154 |
|
} |
157 |
155 |
|
lastClearTextChar += delta - perCharAdjustment; |
158 |
|
- |
mSB.append((char) lastClearTextChar); |
|
156 |
+ |
mSB.append( (char) lastClearTextChar ); |
159 |
157 |
|
return true; |
160 |
158 |
|
} |
161 |
159 |
|
|
162 |
|
- |
private Integer LLdecode(int chr) { |
163 |
|
- |
if (chr == '0') { |
|
160 |
+ |
private Integer LLdecode( int chr ) { |
|
161 |
+ |
if ( chr == '0' ) { |
164 |
162 |
|
return 0; |
165 |
163 |
|
} |
166 |
|
- |
if (('a' <= chr) && (chr <= 'z')) { |
|
164 |
+ |
if ( ('a' <= chr) && (chr <= 'z') ) { |
167 |
165 |
|
return -(1 + (chr - 'a')); |
168 |
166 |
|
} |
169 |
|
- |
if (('A' <= chr) && (chr <= 'Z')) { |
|
167 |
+ |
if ( ('A' <= chr) && (chr <= 'Z') ) { |
170 |
168 |
|
return (1 + (chr - 'A')); |
171 |
169 |
|
} |
172 |
170 |
|
return null; // Unacceptable Char |