View Javadoc

1   package com.explosion.utilities.preferences.impl.inmemory;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Vector;
26  
27  import com.explosion.utilities.preferences.Preference;
28  import com.explosion.utilities.preferences.groups.PreferenceGroup;
29  import com.explosion.utilities.preferences.groups.PreferencePersistenceException;
30  import com.explosion.utilities.preferences.persist.PreferenceGroupPersister;
31  
32  /***
33   * @author: Stephen Cowx
34   *
35   * A persister to allow preference groups to be persisted in memory using hashMaps
36   * 
37   */
38  
39  public class InMemoryPreferenceGroupPersister implements PreferenceGroupPersister
40  {
41      /***
42       * This is the parent store
43       */
44      private Map store;
45      
46      /***
47       * This is the substore that we are storing all of the items in this group in.
48       */
49      private Map groupStore;
50  
51      /***
52       * Hidden constructor, want to use the factory method
53       */
54      private InMemoryPreferenceGroupPersister()    {        
55      }
56      
57      /***
58       * Constructs an instance of a SunPreferencePersistor
59       * @param referenceToStore
60       * @return
61       */
62      public static PreferenceGroupPersister createPreferenceGroupPersistor(Object referenceToStore)
63      {
64          InMemoryPreferenceGroupPersister persister = new InMemoryPreferenceGroupPersister();
65          persister.setReferenceToStore((Map) referenceToStore);
66          persister.groupStore = new HashMap();
67          return persister;
68      }
69      
70      /***
71       * Returns a reference to the backing store as understood by the PreferencePersister
72       * @return
73       */
74      public Object getReferenceToStore()
75      {
76          return store;
77      }
78      
79      /***
80       * Sets the reference to the backing store as understood by the PreferencePersister
81       * Effectively moving it from one store to another or moving it from one location in the same store to another
82       * @return
83       */
84      public void setReferenceToStore(Object referenceToStore)
85      {
86          this.store = (Map) referenceToStore;
87      }
88      
89      /***
90       * This method deletes the preferenceGroup from the backing store
91       * @param group
92       * @throws PreferencePersistenceException
93       */
94      public void deleteGroup(PreferenceGroup group) throws PreferencePersistenceException
95  	{
96      	store.remove(group.getIdentifier());
97  	}
98      
99      /***
100      * This method saves a preference group in the store
101      * @param group
102      * @throws PreferencePersistenceException
103      */
104     public void saveGroup(PreferenceGroup group) throws PreferencePersistenceException
105 	{
106     	Vector attributesVector = group.getPreferencesVector();
107     	for (int i = 0; i < attributesVector.size(); i++)
108         {
109            Preference pref = (Preference) attributesVector.elementAt(i);
110            pref.setReferenceToStore(groupStore);
111         }
112 	}
113     
114     public void renameGroup(PreferenceGroup group, String newName) throws PreferencePersistenceException
115     {
116         /* remove this group from the parent store */
117         deleteGroup(group);
118         
119         /* add the group to the parent store under the new name */
120         store.put(group.getIdentifier(), group);
121           
122         /* Save everything */
123         saveGroup(group);
124     }
125 
126     
127 }