00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.collections;
00011
00012 import java.util.Collection;
00013 import java.util.Collections;
00014 import java.util.Iterator;
00015 import java.util.Map;
00016 import java.util.Set;
00017
00018 import com.sleepycat.bind.EntityBinding;
00019 import com.sleepycat.bind.EntryBinding;
00020 import com.sleepycat.db.Database;
00021
00046 public class StoredMap extends StoredContainer implements Map {
00047
00048 private StoredKeySet keySet;
00049 private StoredEntrySet entrySet;
00050 private StoredValueSet valueSet;
00051
00072 public StoredMap(Database database, EntryBinding keyBinding,
00073 EntryBinding valueBinding, boolean writeAllowed) {
00074
00075 super(new DataView(database, keyBinding, valueBinding, null,
00076 writeAllowed, null));
00077 initView();
00078 }
00079
00101 public StoredMap(Database database, EntryBinding keyBinding,
00102 EntryBinding valueBinding,
00103 PrimaryKeyAssigner keyAssigner) {
00104
00105 super(new DataView(database, keyBinding, valueBinding, null,
00106 true, keyAssigner));
00107 initView();
00108 }
00109
00130 public StoredMap(Database database, EntryBinding keyBinding,
00131 EntityBinding valueEntityBinding, boolean writeAllowed) {
00132
00133 super(new DataView(database, keyBinding, null, valueEntityBinding,
00134 writeAllowed, null));
00135 initView();
00136 }
00137
00159 public StoredMap(Database database, EntryBinding keyBinding,
00160 EntityBinding valueEntityBinding,
00161 PrimaryKeyAssigner keyAssigner) {
00162
00163 super(new DataView(database, keyBinding, null, valueEntityBinding,
00164 true, keyAssigner));
00165 initView();
00166 }
00167
00168 StoredMap(DataView view) {
00169
00170 super(view);
00171 initView();
00172 }
00173
00177 void initAfterClone() {
00178 initView();
00179 }
00180
00189 private void initView() {
00190
00191
00192 if (isOrdered()) {
00193 entrySet = new StoredSortedEntrySet(view);
00194 } else {
00195 entrySet = new StoredEntrySet(view);
00196 }
00197
00198
00199 DataView newView = view.keySetView();
00200 if (isOrdered()) {
00201 keySet = new StoredSortedKeySet(newView);
00202 } else {
00203 keySet = new StoredKeySet(newView);
00204 }
00205
00206
00207 newView = view.valueSetView();
00208 if (isOrdered() && newView.canDeriveKeyFromValue()) {
00209 valueSet = new StoredSortedValueSet(newView);
00210 } else {
00211 valueSet = new StoredValueSet(newView);
00212 }
00213 }
00214
00226 public Object get(Object key) {
00227
00228 return super.get(key);
00229 }
00230
00256 public Object put(Object key, Object value) {
00257
00258 return super.put(key, value);
00259 }
00260
00284 public Object append(Object value) {
00285
00286 boolean doAutoCommit = beginAutoCommit();
00287 try {
00288 Object[] key = new Object[1];
00289 view.append(value, key, null);
00290 commitAutoCommit(doAutoCommit);
00291 return key[0];
00292 } catch (Exception e) {
00293 throw handleException(e, doAutoCommit);
00294 }
00295 }
00296
00308 public Object remove(Object key) {
00309
00310 Object[] oldVal = new Object[1];
00311 removeKey(key, oldVal);
00312 return oldVal[0];
00313 }
00314
00322 public boolean containsKey(Object key) {
00323
00324 return super.containsKey(key);
00325 }
00326
00336 public boolean containsValue(Object value) {
00337
00338 return super.containsValue(value);
00339 }
00340
00354 public void putAll(Map map) {
00355
00356 boolean doAutoCommit = beginAutoCommit();
00357 Iterator entries = null;
00358 try {
00359 entries = map.entrySet().iterator();
00360 while (entries.hasNext()) {
00361 Map.Entry entry = (Map.Entry) entries.next();
00362 put(entry.getKey(), entry.getValue());
00363 }
00364 StoredIterator.close(entries);
00365 commitAutoCommit(doAutoCommit);
00366 } catch (Exception e) {
00367 StoredIterator.close(entries);
00368 throw handleException(e, doAutoCommit);
00369 }
00370 }
00371
00390 public Set keySet() {
00391
00392 return keySet;
00393 }
00394
00413 public Set entrySet() {
00414
00415 return entrySet;
00416 }
00417
00438 public Collection values() {
00439
00440 return valueSet;
00441 }
00442
00464 public Collection duplicates(Object key) {
00465
00466 try {
00467 DataView newView = view.valueSetView(key);
00468 return new StoredValueSet(newView, true);
00469 } catch (KeyRangeException e) {
00470 return Collections.EMPTY_SET;
00471 } catch (Exception e) {
00472 throw StoredContainer.convertException(e);
00473 }
00474 }
00475
00485 public boolean equals(Object other) {
00486
00487 if (other instanceof Map) {
00488 return entrySet().equals(((Map) other).entrySet());
00489 } else {
00490 return false;
00491 }
00492 }
00493
00494
00495
00496
00497
00498 public int hashCode() {
00499 return super.hashCode();
00500 }
00501
00512 public String toString() {
00513
00514 return entrySet().toString();
00515 }
00516 }
00517