|
@@ -1,58 +1,104 @@ |
1 |
1 |
|
package org.litesoft.GWT.client.localstorage; |
2 |
2 |
|
|
|
3 |
+ |
import java.util.*; |
|
4 |
+ |
|
3 |
5 |
|
import org.litesoft.core.util.*; |
4 |
6 |
|
|
5 |
7 |
|
import com.sun.istack.internal.*; |
6 |
8 |
|
|
7 |
|
- |
public class LocalStorageImpl implements LocalStorage |
|
9 |
+ |
public class LocalStorageImpl extends AbstractMap<String, String> implements LocalStorage |
8 |
10 |
|
{ |
|
11 |
+ |
private final Html5LocalStorage mHtml5LS; |
|
12 |
+ |
|
|
13 |
+ |
public LocalStorageImpl( Html5LocalStorage pHtml5LS ) |
|
14 |
+ |
{ |
|
15 |
+ |
mHtml5LS = UtilsCommon.assertNotNull( "Html5LocalStorage", pHtml5LS ); |
|
16 |
+ |
} |
|
17 |
+ |
|
|
18 |
+ |
public LocalStorageImpl() |
|
19 |
+ |
{ |
|
20 |
+ |
this( new Html5LocalStorageImpl() ); |
|
21 |
+ |
} |
|
22 |
+ |
|
|
23 |
+ |
@Override |
|
24 |
+ |
public String get( @NotNull String pKey ) |
|
25 |
+ |
{ |
|
26 |
+ |
return innerGet( pKey ); |
|
27 |
+ |
} |
|
28 |
+ |
|
|
29 |
+ |
@Override |
|
30 |
+ |
public String get( Object key ) |
|
31 |
+ |
{ |
|
32 |
+ |
return innerGet( keyToString( key ) ); |
|
33 |
+ |
} |
|
34 |
+ |
|
9 |
35 |
|
@Override |
10 |
36 |
|
public int size() |
11 |
37 |
|
{ |
12 |
|
- |
return ls_length(); |
|
38 |
+ |
return mHtml5LS.length(); |
13 |
39 |
|
} |
14 |
40 |
|
|
15 |
41 |
|
@Override |
16 |
|
- |
public String get( @NotNull String pKey ) |
|
42 |
+ |
public void clear() |
17 |
43 |
|
{ |
18 |
|
- |
return ls_getItem( UtilsCommon.assertNotNullNotEmpty( "Key", pKey ) ); |
|
44 |
+ |
mHtml5LS.clear(); |
19 |
45 |
|
} |
20 |
46 |
|
|
21 |
47 |
|
@Override |
22 |
|
- |
public void put( @NotNull String pKey, @Nullable String pValue ) |
|
48 |
+ |
public String put( @NotNull String pKey, @Nullable String pValue ) |
23 |
49 |
|
{ |
24 |
|
- |
pKey = UtilsCommon.assertNotNullNotEmpty( "Key", pKey ); |
25 |
|
- |
if ( pValue == null ) |
|
50 |
+ |
synchronized ( mHtml5LS ) |
26 |
51 |
|
{ |
27 |
|
- |
ls_removeItem( pKey ); |
|
52 |
+ |
String zPrevValue = mHtml5LS.getItem( pKey = normalize( pKey ) ); |
|
53 |
+ |
if ( pValue == null ) |
|
54 |
+ |
{ |
|
55 |
+ |
mHtml5LS.removeItem( pKey ); |
|
56 |
+ |
} |
|
57 |
+ |
else if ( !mHtml5LS.setItem( pKey, pValue ) ) |
|
58 |
+ |
{ |
|
59 |
+ |
throw new UnsupportedOperationException(); |
|
60 |
+ |
} |
|
61 |
+ |
return zPrevValue; |
28 |
62 |
|
} |
29 |
|
- |
else |
|
63 |
+ |
} |
|
64 |
+ |
|
|
65 |
+ |
@Override |
|
66 |
+ |
public String remove( @NotNull Object key ) |
|
67 |
+ |
{ |
|
68 |
+ |
String zKey = keyToString( key ); |
|
69 |
+ |
synchronized ( mHtml5LS ) |
|
70 |
+ |
{ |
|
71 |
+ |
String zPrevValue = mHtml5LS.getItem( zKey ); |
|
72 |
+ |
if ( zPrevValue != null ) |
|
73 |
+ |
{ |
|
74 |
+ |
mHtml5LS.removeItem( zKey ); |
|
75 |
+ |
} |
|
76 |
+ |
return zPrevValue; |
|
77 |
+ |
} |
|
78 |
+ |
} |
|
79 |
+ |
|
|
80 |
+ |
@Override |
|
81 |
+ |
public Set<Entry<String, String>> entrySet() |
|
82 |
+ |
{ |
|
83 |
+ |
throw new IllegalStateException( "xxx" ); |
|
84 |
+ |
} |
|
85 |
+ |
|
|
86 |
+ |
private String innerGet( String pKey ) |
|
87 |
+ |
{ |
|
88 |
+ |
return mHtml5LS.getItem( UtilsCommon.assertNotNullNotEmpty( "Key", pKey ) ); |
|
89 |
+ |
} |
|
90 |
+ |
|
|
91 |
+ |
private String keyToString( Object pKey ) |
|
92 |
+ |
{ |
|
93 |
+ |
if ( (pKey == null) || (pKey instanceof String) ) |
30 |
94 |
|
{ |
31 |
|
- |
ls_setItem( pKey, pValue ); |
|
95 |
+ |
return normalize( (String) pKey ); |
32 |
96 |
|
} |
|
97 |
+ |
throw new IllegalArgumentException( "Key not a String, but a: " + UtilsCommon.justSimpleName( pKey ) ); |
33 |
98 |
|
} |
34 |
99 |
|
|
35 |
|
- |
private static native int ls_length() /*-{ |
36 |
|
- |
return $wnd.localStorage.length; |
37 |
|
- |
}-*/; |
38 |
|
- |
|
39 |
|
- |
private static native String ls_key( int index ) /*-{ |
40 |
|
- |
$wnd.localStorage.key( index ); |
41 |
|
- |
}-*/; |
42 |
|
- |
|
43 |
|
- |
private static native String ls_getItem( String key ) /*-{ |
44 |
|
- |
return $wnd.localStorage.getItem( key ); |
45 |
|
- |
}-*/; |
46 |
|
- |
|
47 |
|
- |
private static native void ls_setItem( String key, String value ) /*-{ |
48 |
|
- |
$wnd.localStorage.setItem( key, value ); |
49 |
|
- |
}-*/; |
50 |
|
- |
|
51 |
|
- |
private static native void ls_removeItem( String key ) /*-{ |
52 |
|
- |
$wnd.localStorage.removeItem( key ); |
53 |
|
- |
}-*/; |
54 |
|
- |
|
55 |
|
- |
private static native void ls_clear() /*-{ |
56 |
|
- |
$wnd.localStorage.clear(); |
57 |
|
- |
}-*/; |
|
100 |
+ |
private String normalize( String pKey ) |
|
101 |
+ |
{ |
|
102 |
+ |
return UtilsCommon.assertNotNullNotEmpty( "Key", pKey ); |
|
103 |
+ |
} |
58 |
104 |
|
} |