litesoft
@ 804
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 |
package org.litesoft.GWT.client.localstorage; import org.litesoft.core.simpletypes.*; import org.litesoft.core.typeutils.*; import org.litesoft.core.util.*; import org.litesoft.core.annotations.*; /** * Specification for the HTML5 Local Storage api. * <p/> * Note: The setItem() and removeItem() methods must be atomic with respect to failure. * In the case of failure, the method does nothing. That is, changes to the data storage * area must either be successful, or the data storage area must not be changed at all. */ public class Html5LocalStorageTestImpl implements Html5LocalStorage { private final int mMaxSize; private final SSMap mStore = new SSMap(); private String[] mKeys = null; public Html5LocalStorageTestImpl( int pMaxSize ) { mMaxSize = UtilsCommon.assertPositive( "MaxSize", pMaxSize ); } /** * Return the number of key/value pairs currently present. */ public synchronized int length() { return mStore.size(); } /** * The key(<code>index</code>) method will return the name of the nth key in the list. * The order of keys is user-agent defined, but MUST BE CONSISTENT * so long as the number of keys doesn't change. (Thus, adding or * removing a key may change the order of the keys, but merely changing * the value of an existing key must not.) If <code>index</code> is greater than or equal * to the number of key/value pairs, then this method must return null. * * @param index of the key name to retrieve (index < 0 has un-specified behavior). * * @return key name if index is in range, null if index too large. */ public synchronized String key( int index ) { index = UtilsCommon.assertNotNegative( "index", index ); if ( mKeys == null ) { mKeys = mStore.keySet().toArray( new String[mStore.size()] ); } return (index < mKeys.length) ? mKeys[index] : null; } /** * The getItem(<code>key</code>) method will return the current value associated with the given <code>key</code>. * If the given <code>key</code> does not exist then a null is returned. * * @param key non-null key to lookup (any whitespace in the key has un-specified behavior). * * @return non-null value associated with <code>key</code> or null if <code>key</code> has no association. */ public synchronized String getItem( @NotNull String key ) { return mStore.get( UtilsCommon.assertNotNullNotEmpty( "key", key ) ); } /** * The setItem(key, value) method atomically stores the passed key/value pair by either * adding or updating as is appropriate if it can. * It first checks if there is already an entry for key. * If there is not, then the new key/value pair is added. * If there is, then the existing value is replaced/updated with the new value. * There are two reasons that the new key/value pair might not be able to be stored: either the * the user has disabled storage for the site, or the quota has been exceeded. * * @param key non-null key to store (any whitespace in the key has un-specified behavior). * @param value non-null value to store/associate with the key. * * @return true if the key/value pair was successfully stored. */ public synchronized boolean setItem( @NotNull String key, @NotNull String value ) { key = UtilsCommon.assertNotNullNotEmpty( "key", key ); value = Objects.assertNotNull( "value", value ); if ( null == mStore.put( key, value ) ) { mKeys = null; if ( mMaxSize < mStore.size() ) // Indicate "Full" { mStore.remove( key ); return false; } } return true; } /** * The removeItem(key) method will cause the key/value pair with * the given key to be removed, if it exists. If no entry with that * key exists, the method does nothing. * * @param key non-null key to remove (any whitespace in the key has un-specified behavior). */ public synchronized void removeItem( @NotNull String key ) { if ( null != mStore.remove( UtilsCommon.assertNotNullNotEmpty( "key", key ) ) ) { mKeys = null; } } /** * The clear() method will atomically remove all the key/value * pairs, if there are any. If there are no key/value * pairs when it checks, then the method does nothing. */ public synchronized void clear() { mKeys = null; mStore.clear(); } } |