|
@@ -2,10 +2,10 @@ |
2 |
2 |
|
|
3 |
3 |
|
import org.litesoft.commonfoundation.annotations.*; |
4 |
4 |
|
import org.litesoft.commonfoundation.iterators.*; |
|
5 |
+ |
import org.litesoft.commonfoundation.typeutils.Objects; |
5 |
6 |
|
import org.litesoft.commonfoundation.typeutils.*; |
6 |
7 |
|
import org.litesoft.core.simpletypes.nonpublic.*; |
7 |
8 |
|
|
8 |
|
- |
import org.litesoft.commonfoundation.typeutils.Objects; |
9 |
9 |
|
import java.util.*; |
10 |
10 |
|
|
11 |
11 |
|
/** |
|
@@ -16,46 +16,37 @@ |
16 |
16 |
|
* <li>null values for values on a <code>put</code> are treated as a call to <code>remove</code></li> |
17 |
17 |
|
* <li>keys are trimmed of their leading and trailing whitespace and if the resulting key is empty it is treated as a null</li> |
18 |
18 |
|
*/ |
19 |
|
- |
public class LocalStorageImpl extends AbstractMap<String, String> implements LocalStorage |
20 |
|
- |
{ |
|
19 |
+ |
public class LocalStorageImpl extends AbstractMap<String, String> implements LocalStorage { |
21 |
20 |
|
private final Html5LocalStorage mHtml5LS; |
22 |
21 |
|
private int mVersion = 0; |
23 |
22 |
|
|
24 |
|
- |
public LocalStorageImpl( Html5LocalStorage pHtml5LS ) |
25 |
|
- |
{ |
|
23 |
+ |
public LocalStorageImpl( Html5LocalStorage pHtml5LS ) { |
26 |
24 |
|
mHtml5LS = Objects.assertNotNull( "Html5LocalStorage", pHtml5LS ); |
27 |
25 |
|
} |
28 |
26 |
|
|
29 |
|
- |
public LocalStorageImpl() |
30 |
|
- |
{ |
|
27 |
+ |
public LocalStorageImpl() { |
31 |
28 |
|
this( new Html5LocalStorageImpl() ); |
32 |
29 |
|
} |
33 |
30 |
|
|
34 |
31 |
|
@Override |
35 |
|
- |
public String get( @NotNull String pKey ) |
36 |
|
- |
{ |
|
32 |
+ |
public String get( @NotNull String pKey ) { |
37 |
33 |
|
return innerGet( pKey ); |
38 |
34 |
|
} |
39 |
35 |
|
|
40 |
36 |
|
@Override |
41 |
|
- |
public String get( Object key ) |
42 |
|
- |
{ |
|
37 |
+ |
public String get( Object key ) { |
43 |
38 |
|
return innerGet( keyToString( key ) ); |
44 |
39 |
|
} |
45 |
40 |
|
|
46 |
41 |
|
@Override |
47 |
|
- |
public int size() |
48 |
|
- |
{ |
|
42 |
+ |
public int size() { |
49 |
43 |
|
return mHtml5LS.length(); |
50 |
44 |
|
} |
51 |
45 |
|
|
52 |
46 |
|
@Override |
53 |
|
- |
public void clear() |
54 |
|
- |
{ |
55 |
|
- |
synchronized ( mHtml5LS ) |
56 |
|
- |
{ |
57 |
|
- |
if ( mHtml5LS.length() != 0 ) |
58 |
|
- |
{ |
|
47 |
+ |
public void clear() { |
|
48 |
+ |
synchronized ( mHtml5LS ) { |
|
49 |
+ |
if ( mHtml5LS.length() != 0 ) { |
59 |
50 |
|
mHtml5LS.clear(); |
60 |
51 |
|
mVersion++; |
61 |
52 |
|
} |
|
@@ -63,17 +54,12 @@ |
63 |
54 |
|
} |
64 |
55 |
|
|
65 |
56 |
|
@Override |
66 |
|
- |
public String put( @NotNull String pKey, @Nullable String pValue ) |
67 |
|
- |
{ |
68 |
|
- |
synchronized ( mHtml5LS ) |
69 |
|
- |
{ |
|
57 |
+ |
public String put( @NotNull String pKey, @Nullable String pValue ) { |
|
58 |
+ |
synchronized ( mHtml5LS ) { |
70 |
59 |
|
String zPrevValue = mHtml5LS.getItem( pKey = normalize( pKey ) ); |
71 |
|
- |
if ( pValue == null ) |
72 |
|
- |
{ |
|
60 |
+ |
if ( pValue == null ) { |
73 |
61 |
|
mHtml5LS.removeItem( pKey ); |
74 |
|
- |
} |
75 |
|
- |
else if ( !mHtml5LS.setItem( pKey, pValue ) ) |
76 |
|
- |
{ |
|
62 |
+ |
} else if ( !mHtml5LS.setItem( pKey, pValue ) ) { |
77 |
63 |
|
throw new UnsupportedOperationException(); |
78 |
64 |
|
} |
79 |
65 |
|
mVersion++; |
|
@@ -82,14 +68,11 @@ |
82 |
68 |
|
} |
83 |
69 |
|
|
84 |
70 |
|
@Override |
85 |
|
- |
public String remove( @NotNull Object key ) |
86 |
|
- |
{ |
|
71 |
+ |
public String remove( @NotNull Object key ) { |
87 |
72 |
|
String zKey = keyToString( key ); |
88 |
|
- |
synchronized ( mHtml5LS ) |
89 |
|
- |
{ |
|
73 |
+ |
synchronized ( mHtml5LS ) { |
90 |
74 |
|
String zPrevValue = mHtml5LS.getItem( zKey ); |
91 |
|
- |
if ( zPrevValue != null ) |
92 |
|
- |
{ |
|
75 |
+ |
if ( zPrevValue != null ) { |
93 |
76 |
|
mHtml5LS.removeItem( zKey ); |
94 |
77 |
|
mVersion++; |
95 |
78 |
|
} |
|
@@ -98,55 +81,42 @@ |
98 |
81 |
|
} |
99 |
82 |
|
|
100 |
83 |
|
@Override |
101 |
|
- |
public Set<Entry<String, String>> entrySet() |
102 |
|
- |
{ |
103 |
|
- |
return new AbstractSet<Entry<String, String>>() |
104 |
|
- |
{ |
|
84 |
+ |
public Set<Entry<String, String>> entrySet() { |
|
85 |
+ |
return new AbstractSet<Entry<String, String>>() { |
105 |
86 |
|
@Override |
106 |
|
- |
public int size() |
107 |
|
- |
{ |
|
87 |
+ |
public int size() { |
108 |
88 |
|
return mHtml5LS.length(); |
109 |
89 |
|
} |
110 |
90 |
|
|
111 |
91 |
|
@Override |
112 |
|
- |
public Iterator<Entry<String, String>> iterator() |
113 |
|
- |
{ |
114 |
|
- |
return new Iterators.AbstractReadOnly<Entry<String, String>>() |
115 |
|
- |
{ |
|
92 |
+ |
public Iterator<Entry<String, String>> iterator() { |
|
93 |
+ |
return new Iterators.AbstractReadOnly<Entry<String, String>>() { |
116 |
94 |
|
private final int mVersion = getVersion(); |
117 |
95 |
|
private final int mSize = size(); |
118 |
96 |
|
private int mNextIndex = 0; |
119 |
97 |
|
|
120 |
|
- |
private boolean versionOK() |
121 |
|
- |
{ |
|
98 |
+ |
private boolean versionOK() { |
122 |
99 |
|
return mVersion == getVersion(); |
123 |
100 |
|
} |
124 |
101 |
|
|
125 |
|
- |
private boolean indexOK() |
126 |
|
- |
{ |
|
102 |
+ |
private boolean indexOK() { |
127 |
103 |
|
return mNextIndex < mSize; |
128 |
104 |
|
} |
129 |
105 |
|
|
130 |
106 |
|
@Override |
131 |
|
- |
public boolean hasNext() |
132 |
|
- |
{ |
133 |
|
- |
synchronized ( mHtml5LS ) |
134 |
|
- |
{ |
|
107 |
+ |
public boolean hasNext() { |
|
108 |
+ |
synchronized ( mHtml5LS ) { |
135 |
109 |
|
return versionOK() && indexOK(); |
136 |
110 |
|
} |
137 |
111 |
|
} |
138 |
112 |
|
|
139 |
113 |
|
@Override |
140 |
|
- |
public Entry<String, String> next() |
141 |
|
- |
{ |
142 |
|
- |
synchronized ( mHtml5LS ) |
143 |
|
- |
{ |
144 |
|
- |
if ( !versionOK() ) |
145 |
|
- |
{ |
|
114 |
+ |
public Entry<String, String> next() { |
|
115 |
+ |
synchronized ( mHtml5LS ) { |
|
116 |
+ |
if ( !versionOK() ) { |
146 |
117 |
|
throw new ConcurrentModificationException(); |
147 |
118 |
|
} |
148 |
|
- |
if ( !indexOK() ) |
149 |
|
- |
{ |
|
119 |
+ |
if ( !indexOK() ) { |
150 |
120 |
|
throw new NoSuchElementException(); |
151 |
121 |
|
} |
152 |
122 |
|
String zKey = mHtml5LS.key( mNextIndex++ ); |
|
@@ -158,82 +128,68 @@ |
158 |
128 |
|
}; |
159 |
129 |
|
} |
160 |
130 |
|
|
161 |
|
- |
private int getVersion() |
162 |
|
- |
{ |
|
131 |
+ |
private int getVersion() { |
163 |
132 |
|
return mVersion; |
164 |
133 |
|
} |
165 |
134 |
|
|
166 |
|
- |
private String innerGet( String pKey ) |
167 |
|
- |
{ |
|
135 |
+ |
private String innerGet( String pKey ) { |
168 |
136 |
|
return mHtml5LS.getItem( Strings.assertNotNullNotEmpty( "Key", pKey ) ); |
169 |
137 |
|
} |
170 |
138 |
|
|
171 |
|
- |
private String keyToString( Object pKey ) |
172 |
|
- |
{ |
173 |
|
- |
if ( (pKey == null) || (pKey instanceof String) ) |
174 |
|
- |
{ |
|
139 |
+ |
private String keyToString( Object pKey ) { |
|
140 |
+ |
if ( (pKey == null) || (pKey instanceof String) ) { |
175 |
141 |
|
return normalize( (String) pKey ); |
176 |
142 |
|
} |
177 |
143 |
|
throw new IllegalArgumentException( "Key not a String, but a: " + Objects.justSimpleName( pKey ) ); |
178 |
144 |
|
} |
179 |
145 |
|
|
180 |
|
- |
private String normalize( String pKey ) |
181 |
|
- |
{ |
|
146 |
+ |
private String normalize( String pKey ) { |
182 |
147 |
|
return Strings.assertNotNullNotEmpty( "Key", pKey ); |
183 |
148 |
|
} |
184 |
149 |
|
|
185 |
150 |
|
/** |
186 |
151 |
|
* Keys & Values can't be null! |
187 |
152 |
|
*/ |
188 |
|
- |
private static class OurEntry implements Entry<String, String> |
189 |
|
- |
{ |
|
153 |
+ |
private static class OurEntry implements Entry<String, String> { |
190 |
154 |
|
private final String mKey; |
191 |
155 |
|
private final String mValue; |
192 |
156 |
|
|
193 |
|
- |
public OurEntry( String pKey, String pValue ) |
194 |
|
- |
{ |
|
157 |
+ |
public OurEntry( String pKey, String pValue ) { |
195 |
158 |
|
this.mKey = pKey; |
196 |
159 |
|
this.mValue = pValue; |
197 |
160 |
|
} |
198 |
161 |
|
|
199 |
162 |
|
@Override |
200 |
|
- |
public String getKey() |
201 |
|
- |
{ |
|
163 |
+ |
public String getKey() { |
202 |
164 |
|
return mKey; |
203 |
165 |
|
} |
204 |
166 |
|
|
205 |
167 |
|
@Override |
206 |
|
- |
public String getValue() |
207 |
|
- |
{ |
|
168 |
+ |
public String getValue() { |
208 |
169 |
|
return mValue; |
209 |
170 |
|
} |
210 |
171 |
|
|
211 |
172 |
|
@Override |
212 |
|
- |
public String setValue( String value ) |
213 |
|
- |
{ |
|
173 |
+ |
public String setValue( String value ) { |
214 |
174 |
|
throw new UnsupportedOperationException(); |
215 |
175 |
|
} |
216 |
176 |
|
|
217 |
|
- |
public boolean equals( Object o ) |
218 |
|
- |
{ |
|
177 |
+ |
public boolean equals( Object o ) { |
219 |
178 |
|
return (this == o) || ((o instanceof Map.Entry) && equals( (Map.Entry) o )); |
220 |
179 |
|
} |
221 |
180 |
|
|
222 |
|
- |
public boolean equals( Map.Entry them ) |
223 |
|
- |
{ |
|
181 |
+ |
public boolean equals( Map.Entry them ) { |
224 |
182 |
|
return (this == them) || // |
225 |
183 |
|
((them != null) && // |
226 |
184 |
|
mKey.equals( them.getKey() ) && // |
227 |
185 |
|
mValue.equals( them.getValue() )); |
228 |
186 |
|
} |
229 |
187 |
|
|
230 |
|
- |
public int hashCode() |
231 |
|
- |
{ |
|
188 |
+ |
public int hashCode() { |
232 |
189 |
|
return EqualSupport.hashCodeEm( mKey.hashCode(), mValue.hashCode() ); |
233 |
190 |
|
} |
234 |
191 |
|
|
235 |
|
- |
public java.lang.String toString() |
236 |
|
- |
{ |
|
192 |
+ |
public java.lang.String toString() { |
237 |
193 |
|
return mKey + "=" + mValue; |
238 |
194 |
|
} |
239 |
195 |
|
} |