KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashSet JavaDoc;
12 import java.util.Set JavaDoc;
13
14 import org.apache.log4j.Logger;
15
16 /**
17  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
18  * @version $Revision: 1.2 $
19  */

20 public class SimpleMergeStrategy implements MergeStrategy
21 {
22
23    /** Our logger. */
24    private static Logger log = Logger.getLogger(SimpleMergeStrategy.class);
25
26    /** The singleton to avoid creation. */
27    private static final SimpleMergeStrategy singleton = new SimpleMergeStrategy();
28
29    public static SimpleMergeStrategy getSingleton()
30    {
31       return singleton;
32    }
33
34    public Preference getPreference(PreferenceSet[] sets, String JavaDoc key) throws IllegalArgumentException JavaDoc
35    {
36       // Arg check
37
if (sets == null)
38       {
39          throw new IllegalArgumentException JavaDoc("sets must not be null");
40       }
41       if (key == null)
42       {
43          throw new IllegalArgumentException JavaDoc("key must not be null");
44       }
45
46       // The returned preference
47
Preference preference = null;
48
49       // Get the pref
50
for (int i = sets.length - 1;i >= 0;i--)
51       {
52          Preference tmp = sets[i].getPreference(key);
53          if (tmp != null)
54          {
55             if (tmp.isReadOnly())
56             {
57                preference = tmp;
58                break;
59             }
60             else if (preference == null)
61             {
62                preference = tmp;
63             }
64          }
65       }
66
67       return preference;
68    }
69
70    public boolean isReadOnly(PreferenceSet[] sets, String JavaDoc key) throws IllegalArgumentException JavaDoc
71    {
72       // Arg check
73
if (sets == null)
74       {
75          throw new IllegalArgumentException JavaDoc("sets must not be null");
76       }
77       if (key == null)
78       {
79          throw new IllegalArgumentException JavaDoc("key must not be null");
80       }
81
82       //
83
for (int i = sets.length - 1;i >= 0;i--)
84       {
85          Preference preference = sets[i].getPreference(key);
86          if (preference != null && preference.isReadOnly())
87          {
88             return true;
89          }
90       }
91
92       //
93
return false;
94    }
95
96    public Set JavaDoc getKeySet(PreferenceSet[] sets) throws IllegalArgumentException JavaDoc
97    {
98       // Arg check
99
if (sets == null)
100       {
101          throw new IllegalArgumentException JavaDoc("sets must not be null");
102       }
103
104       // Perform merge
105
Set JavaDoc keys = new HashSet JavaDoc();
106       for (int i = 0;i < sets.length;i++)
107       {
108          Set JavaDoc tmp = sets[i].keySet();
109          keys.addAll(tmp);
110       }
111       return keys;
112    }
113 }
114
Popular Tags