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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package org.litesoft.GWT.client.localstorage;

import java.util.*;

import org.litesoft.core.annotations.*;
import org.litesoft.core.simpletypes.nonpublic.*;
import org.litesoft.core.typeutils.*;
import org.litesoft.core.util.*;

/**
 * Local Storage is a Proxy/Adapter to the underlying HTML5 LocalStorage.
 * <p/>
 * The exposed API is that of a String to String Map with three important differences:
 * <li>null keys are NOT supported</li>
 * <li>null values for values on a <code>put</code> are treated as a call to <code>remove</code></li>
 * <li>keys are trimmed of their leading and trailing whitespace and if the resulting key is empty it is treated as a null</li>
 */
public class LocalStorageImpl extends AbstractMap<String, String> implements LocalStorage
{
    private final Html5LocalStorage mHtml5LS;
    private int mVersion = 0;

    public LocalStorageImpl( Html5LocalStorage pHtml5LS )
    {
        mHtml5LS = Objects.assertNotNull( "Html5LocalStorage", pHtml5LS );
    }

    public LocalStorageImpl()
    {
        this( new Html5LocalStorageImpl() );
    }

    @Override
    public String get( @NotNull String pKey )
    {
        return innerGet( pKey );
    }

    @Override
    public String get( Object key )
    {
        return innerGet( keyToString( key ) );
    }

    @Override
    public int size()
    {
        return mHtml5LS.length();
    }

    @Override
    public void clear()
    {
        synchronized ( mHtml5LS )
        {
            if ( mHtml5LS.length() != 0 )
            {
                mHtml5LS.clear();
                mVersion++;
            }
        }
    }

    @Override
    public String put( @NotNull String pKey, @Nullable String pValue )
    {
        synchronized ( mHtml5LS )
        {
            String zPrevValue = mHtml5LS.getItem( pKey = normalize( pKey ) );
            if ( pValue == null )
            {
                mHtml5LS.removeItem( pKey );
            }
            else if ( !mHtml5LS.setItem( pKey, pValue ) )
            {
                throw new UnsupportedOperationException();
            }
            mVersion++;
            return zPrevValue;
        }
    }

    @Override
    public String remove( @NotNull Object key )
    {
        String zKey = keyToString( key );
        synchronized ( mHtml5LS )
        {
            String zPrevValue = mHtml5LS.getItem( zKey );
            if ( zPrevValue != null )
            {
                mHtml5LS.removeItem( zKey );
                mVersion++;
            }
            return zPrevValue;
        }
    }

    @Override
    public Set<Entry<String, String>> entrySet()
    {
        return new AbstractSet<Entry<String, String>>()
        {
            @Override
            public int size()
            {
                return mHtml5LS.length();
            }

            @Override
            public Iterator<Entry<String, String>> iterator()
            {
                return new AbstractReadOnlyIterator<Entry<String, String>>()
                {
                    private final int mVersion = getVersion();
                    private final int mSize = size();
                    private int mNextIndex = 0;

                    private boolean versionOK()
                    {
                        return mVersion == getVersion();
                    }

                    private boolean indexOK()
                    {
                        return mNextIndex < mSize;
                    }

                    @Override
                    public boolean hasNext()
                    {
                        synchronized ( mHtml5LS )
                        {
                            return versionOK() && indexOK();
                        }
                    }

                    @Override
                    public Entry<String, String> next()
                    {
                        synchronized ( mHtml5LS )
                        {
                            if ( !versionOK() )
                            {
                                throw new ConcurrentModificationException();
                            }
                            if ( !indexOK() )
                            {
                                throw new NoSuchElementException();
                            }
                            String zKey = mHtml5LS.key( mNextIndex++ );
                            return new OurEntry( zKey, mHtml5LS.getItem( zKey ) );
                        }
                    }
                };
            }
        };
    }

    private int getVersion()
    {
        return mVersion;
    }

    private String innerGet( String pKey )
    {
        return mHtml5LS.getItem( Strings.assertNotNullNotEmpty( "Key", pKey ) );
    }

    private String keyToString( Object pKey )
    {
        if ( (pKey == null) || (pKey instanceof String) )
        {
            return normalize( (String) pKey );
        }
        throw new IllegalArgumentException( "Key not a String, but a: " + Objects.justSimpleName( pKey ) );
    }

    private String normalize( String pKey )
    {
        return Strings.assertNotNullNotEmpty( "Key", pKey );
    }

    /**
     * Keys & Values can't be null!
     */
    private static class OurEntry implements Entry<String, String>
    {
        private final String mKey;
        private final String mValue;

        public OurEntry( String pKey, String pValue )
        {
            this.mKey = pKey;
            this.mValue = pValue;
        }

        public String getKey()
        {
            return mKey;
        }

        public String getValue()
        {
            return mValue;
        }

        public String setValue( String value )
        {
            throw new UnsupportedOperationException();
        }

        public boolean equals( Object o )
        {
            return (this == o) || ((o instanceof Map.Entry) && equals( (Map.Entry) o ));
        }

        public boolean equals( Map.Entry them )
        {
            return (this == them) || //
                   ((them != null) && //
                    mKey.equals( them.getKey() ) && //
                    mValue.equals( them.getValue() ));
        }

        public int hashCode()
        {
            return EqualSupport.hashCodeEm( mKey.hashCode(), mValue.hashCode() );
        }

        public java.lang.String toString()
        {
            return mKey + "=" + mValue;
        }
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/client/localstorage/LocalStorageImpl.java

Diff revisions: vs.
Revision Author Commited Message
821 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 00:08:41 +0000
811 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 13:45:18 +0000
804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
777 Diff Diff GeorgeS picture GeorgeS Mon 16 Jul, 2012 00:48:01 +0000
765 Diff Diff GeorgeS picture GeorgeS Sat 14 Jul, 2012 15:06:46 +0000

!

764 Diff Diff GeorgeS picture GeorgeS Thu 12 Jul, 2012 14:22:25 +0000
759 Diff Diff GeorgeS picture GeorgeS Wed 11 Jul, 2012 05:05:42 +0000
758 Diff Diff GeorgeS picture GeorgeS Wed 11 Jul, 2012 03:57:35 +0000

!

757 GeorgeS picture GeorgeS Mon 09 Jul, 2012 05:32:56 +0000