View Javadoc

1   package org.starobjects.tested.fitnesse.internal.components;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.nakedobjects.metamodel.adapter.oid.Oid;
7   import org.nakedobjects.metamodel.commons.exceptions.NakedObjectException;
8   import org.nakedobjects.metamodel.facets.object.cached.CachedFacet;
9   import org.nakedobjects.metamodel.spec.NakedObjectSpecification;
10  import org.nakedobjects.runtime.objectstore.inmemory.internal.ObjectStoreInstances;
11  import org.nakedobjects.runtime.objectstore.inmemory.internal.ObjectStorePersistedObjects;
12  import org.nakedobjects.runtime.persistence.oidgenerator.simple.SimpleOidGenerator.Memento;
13  
14  import com.google.common.collect.Iterables;
15  
16  /**
17   * Stores instances in one of two maps, based on whether have their
18   * specification has the {@link CachedFacet} (represents cached or reference
19   * data) or not (represents operational data).
20   * <p>
21   * Those that are cached are stored in a <tt>static</tt> map that is never
22   * {@link #clear()}ed down. Those that are operational go in a regular instance
23   * cache and can be {@link #clear()}ed.
24   */
25  public class FitnesseObjectStorePersistedObjects implements
26          ObjectStorePersistedObjects {
27  
28      private static final Map<NakedObjectSpecification, ObjectStoreInstances> cachedInstancesBySpecMap =
29              new HashMap<NakedObjectSpecification, ObjectStoreInstances>();
30  
31      private final Map<NakedObjectSpecification, ObjectStoreInstances> operationalInstancesBySpecMap;
32  
33      private final Map<String, Oid> serviceOidByIdMap;
34      private Memento oidGeneratorMemento;
35  
36      public FitnesseObjectStorePersistedObjects() {
37          operationalInstancesBySpecMap = new HashMap<NakedObjectSpecification, ObjectStoreInstances>();
38          serviceOidByIdMap = new HashMap<String, Oid>();
39      }
40  
41      public Memento getOidGeneratorMemento() {
42          return oidGeneratorMemento;
43      }
44  
45      public void saveOidGeneratorMemento(final Memento memento) {
46          this.oidGeneratorMemento = memento;
47      }
48  
49      public Oid getService(final String name) {
50          return serviceOidByIdMap.get(name);
51      }
52  
53      public void registerService(final String name, final Oid oid) {
54          final Oid oidLookedUpByName = serviceOidByIdMap.get(name);
55          if (oidLookedUpByName != null) {
56              if (!oidLookedUpByName.equals(oid)) {
57                  throw new NakedObjectException(
58                          "Already another service registered as name: " + name
59                          + " (existing Oid: " + oidLookedUpByName + ", "
60                          + "intended: " + oid + ")");
61              }
62          } else {
63              serviceOidByIdMap.put(name, oid);
64          }
65      }
66  
67      public Iterable<NakedObjectSpecification> specifications() {
68          return Iterables.concat(
69                  FitnesseObjectStorePersistedObjects.cachedInstancesBySpecMap
70                  .keySet(), operationalInstancesBySpecMap.keySet());
71      }
72  
73      public Iterable<ObjectStoreInstances> instances() {
74          return Iterables.concat(
75                  FitnesseObjectStorePersistedObjects.cachedInstancesBySpecMap
76                  .values(), operationalInstancesBySpecMap.values());
77      }
78  
79      public ObjectStoreInstances instancesFor(final NakedObjectSpecification spec) {
80          if (isCached(spec)) {
81              return getFromMap(
82                      spec,
83                      FitnesseObjectStorePersistedObjects.cachedInstancesBySpecMap);
84          } else {
85              return getFromMap(spec, operationalInstancesBySpecMap);
86          }
87      }
88  
89      // //////////////////////////////////////////////////////////////////
90      // Helpers
91      // //////////////////////////////////////////////////////////////////
92  
93      private ObjectStoreInstances getFromMap(
94              final NakedObjectSpecification spec,
95              final Map<NakedObjectSpecification, ObjectStoreInstances> map) {
96          ObjectStoreInstances ins = map.get(spec);
97          if (ins == null) {
98              ins = new ObjectStoreInstances(spec);
99              map.put(spec, ins);
100         }
101         return ins;
102     }
103 
104     private boolean isCached(final NakedObjectSpecification spec) {
105         return spec.containsFacet(CachedFacet.class);
106     }
107 
108     /**
109      * Only clears the operational instances, not the cached instances.
110      */
111     public void clear() {
112         operationalInstancesBySpecMap.clear();
113     }
114 
115 }