| 1 | /*- |
|---|
| 2 | * Copyright (c) 2007, Derek Konigsberg |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * |
|---|
| 9 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 10 | * notice, this list of conditions and the following disclaimer. |
|---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 13 | * documentation and/or other materials provided with the distribution. |
|---|
| 14 | * 3. Neither the name of the project nor the names of its |
|---|
| 15 | * contributors may be used to endorse or promote products derived |
|---|
| 16 | * from this software without specific prior written permission. |
|---|
| 17 | * |
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|---|
| 22 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|---|
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 30 | */ |
|---|
| 31 | |
|---|
| 32 | package org.logicprobe.LogicMail.util; |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Provides a common interface for persistent data storage implementations. |
|---|
| 36 | * The interface provides the ability to store objects by name or unique id. |
|---|
| 37 | * Since all serializable objects provide a unique id, everything should |
|---|
| 38 | * actually be stored by unique id. A mapping table should be internally |
|---|
| 39 | * maintained to allow name lookups. Named storage allows retrieval of |
|---|
| 40 | * top-level objects for which the unique id is unknown. |
|---|
| 41 | */ |
|---|
| 42 | public interface DataStore { |
|---|
| 43 | /** |
|---|
| 44 | * Gets an object by name lookup. |
|---|
| 45 | * |
|---|
| 46 | * @param name Name for the object |
|---|
| 47 | * @return Matching object |
|---|
| 48 | */ |
|---|
| 49 | Serializable getNamedObject(String name); |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * Gets an array of all the named objects available. |
|---|
| 53 | * |
|---|
| 54 | * @return Array of available named objects |
|---|
| 55 | */ |
|---|
| 56 | String[] getNamedObjects(); |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * Gets an object by unique id lookup. |
|---|
| 60 | * |
|---|
| 61 | * @param id Unique id for the object |
|---|
| 62 | * @return Matching object |
|---|
| 63 | */ |
|---|
| 64 | Serializable getObject(long id); |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * Puts an object into the store with a name mapping. |
|---|
| 68 | * This should also create the necessary unique id mapping. |
|---|
| 69 | * If the object matching the name already exists in the store, |
|---|
| 70 | * it will be overwritten. |
|---|
| 71 | * |
|---|
| 72 | * @param name Name for the object |
|---|
| 73 | * @param object Object to store |
|---|
| 74 | */ |
|---|
| 75 | void putNamedObject(String name, Serializable object); |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Puts an object into the store. |
|---|
| 79 | * If the object matching the same unique id already exists in the store, |
|---|
| 80 | * it will be overwritten. |
|---|
| 81 | * |
|---|
| 82 | * @param object Object to store |
|---|
| 83 | */ |
|---|
| 84 | void putObject(Serializable object); |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * Removes an object with a name mapping from the store. |
|---|
| 88 | * This will also remove the unique id mapping. |
|---|
| 89 | * |
|---|
| 90 | * @param name Name of object to remove |
|---|
| 91 | */ |
|---|
| 92 | void removeNamedObject(String name); |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Removes an object from the store. |
|---|
| 96 | * |
|---|
| 97 | * @param object Object to remove |
|---|
| 98 | */ |
|---|
| 99 | void removeObject(Serializable object); |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 102 | * Removes an object from the store. |
|---|
| 103 | * |
|---|
| 104 | * @param id Unique id for the object to remove |
|---|
| 105 | */ |
|---|
| 106 | void removeObject(long id); |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * Save the contents of the store to persistent memory. |
|---|
| 110 | */ |
|---|
| 111 | void save(); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * Load the contents of the store from persistent memory. |
|---|
| 116 | */ |
|---|
| 117 | void load(); |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Delete this store from the device. |
|---|
| 121 | * Calling save() or load() after this method may result |
|---|
| 122 | * in the store being recreated. |
|---|
| 123 | */ |
|---|
| 124 | void delete(); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Gets the UID that the synchronization object for this store will be |
|---|
| 129 | * using. This is necessary so than a <code>SyncCollection</code> |
|---|
| 130 | * implementation can know which synchronization object belongs to this |
|---|
| 131 | * data store. |
|---|
| 132 | * |
|---|
| 133 | * @return the sync object UID |
|---|
| 134 | */ |
|---|
| 135 | int getSyncObjectUID(); |
|---|
| 136 | |
|---|
| 137 | /** |
|---|
| 138 | * Gets a sync object containing the persistent data this data store is |
|---|
| 139 | * responsible for managing. Implementations must be completely |
|---|
| 140 | * independent of the loaded state of the data store, and thus operate |
|---|
| 141 | * directly upon the backing persistence mechanism. |
|---|
| 142 | * |
|---|
| 143 | * @return the sync object |
|---|
| 144 | */ |
|---|
| 145 | DataStoreSyncObject getSyncObject(); |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | * Sets the persistent data this data store is responsible for managing |
|---|
| 149 | * from the provided sync object. Implementations must be completely |
|---|
| 150 | * independent of the loaded state of the data store, and thus operate |
|---|
| 151 | * directly upon the backing persistence mechanism. |
|---|
| 152 | * |
|---|
| 153 | * @param syncObject the sync object from which to populate this data store |
|---|
| 154 | * @return true, if this data store was actually populated |
|---|
| 155 | */ |
|---|
| 156 | boolean setSyncObject(DataStoreSyncObject syncObject); |
|---|
| 157 | } |
|---|