KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > plugins > preferences > AbstractPreferenceSet


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.plugins.preferences;
10
11 import java.io.Serializable JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.jboss.portal.common.value.Value;
17
18 /**
19  * A simple implementation that store the values in the preference objects which
20  * are mutable. The read only set is not supported and throws UnsupportedOperationException.
21  *
22  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
23  * @version $Revision: 1.2 $
24  */

25 public class AbstractPreferenceSet implements PreferenceSet, Serializable JavaDoc
26 {
27
28    protected Map JavaDoc map;
29
30    public AbstractPreferenceSet()
31    {
32       this(new HashMap JavaDoc());
33    }
34
35    public AbstractPreferenceSet(Map JavaDoc map)
36    {
37       this.map = map;
38    }
39
40    public Set JavaDoc keySet()
41    {
42       return map.keySet();
43    }
44
45    public Preference getPreference(String JavaDoc key)
46    {
47       if (key == null)
48       {
49          throw new IllegalArgumentException JavaDoc("key must not be null");
50       }
51       return (Preference)map.get(key);
52    }
53
54    public Value getValue(String JavaDoc key)
55    {
56       if (key == null)
57       {
58          throw new IllegalArgumentException JavaDoc("key must not be null");
59       }
60       Preference preference = getPreference(key);
61       if (preference != null)
62       {
63          return preference.getValue();
64       }
65       else
66       {
67          return null;
68       }
69    }
70
71    public void setValue(String JavaDoc key, Value value)
72    {
73       if (key == null)
74       {
75          throw new IllegalArgumentException JavaDoc("key must not be null");
76       }
77       if (value == null)
78       {
79          map.remove(key);
80       }
81       else
82       {
83          AbstractPreference pref = (AbstractPreference)map.get(key);
84          if (pref == null)
85          {
86             pref = new AbstractPreference(key, value, false);
87             map.put(key, pref);
88          }
89          else
90          {
91             pref.setValue(value);
92          }
93       }
94    }
95
96    public boolean isReadOnly(String JavaDoc key)
97    {
98       if (key == null)
99       {
100          throw new IllegalArgumentException JavaDoc("key must not be null");
101       }
102       return false;
103    }
104
105    public void setReadOnly(String JavaDoc key, boolean readOnly)
106    {
107       throw new UnsupportedOperationException JavaDoc("Should not be called");
108    }
109 }
110
Popular Tags