Clover Coverage Report - Atlassian Core
Coverage timestamp: Sun Nov 30 2008 18:33:35 CST
63   237   33   4.5
36   177   0.52   14
14     2.36  
1    
 
 
  UserPreferences       Line # 24 63 33 39.8% 0.39823008
 
  (5)
 
1    /*
2    * Atlassian Source Code Template.
3    * User: owen
4    * Date: Oct 15, 2002
5    * Time: 11:30:01 AM
6    * CVS Revision: $Revision: 1.10 $
7    * Last CVS Commit: $Date: 2005/10/04 02:41:53 $
8    * Author of last CVS Commit: $Author: nfaiz $
9    */
10    package com.atlassian.core.user.preferences;
11   
12    import com.atlassian.core.AtlassianCoreException;
13    import com.atlassian.core.util.PropertyUtils;
14    import com.opensymphony.module.propertyset.PropertySet;
15    import com.opensymphony.module.propertyset.PropertySetManager;
16    import com.opensymphony.user.User;
17   
18    import java.io.Serializable;
19    import java.util.HashMap;
20    import java.util.Map;
21    import java.util.Set;
22    import java.util.HashSet;
23   
 
24    public class UserPreferences implements Preferences, Serializable
25    {
26    private PropertySet backingPS = null;
27   
28    // stores the keys for preferences that the default values should be used
29    private Set defaultKeys = null;
30   
 
31  1 toggle public UserPreferences()
32    {
33  1 this((PropertySet) null, true);
34    }
35   
 
36  7 toggle public UserPreferences(User pUser)
37    {
38  7 this(pUser, true);
39    }
40   
 
41  0 toggle public UserPreferences(PropertySet propertySet)
42    {
43  0 backingPS = propertySet;
44  0 defaultKeys = new HashSet();
45    }
46   
 
47  1 toggle public UserPreferences(PropertySet propertySet, boolean bulkload)
48    {
49    // Use this set even if the propertySet is null
50    // This will save some operations on checking if the userPs is null before
51    // looking at the default properties, and look into the default properties straight away
52  1 defaultKeys = new HashSet();
53   
54  1 if (propertySet != null)
55    {
56  0 PropertySet userPs = propertySet;
57   
58  0 Map params = new HashMap(2);
59  0 params.put("PropertySet", userPs);
60  0 params.put("bulkload", new Boolean(bulkload));
61   
62  0 backingPS = PropertySetManager.getInstance("cached", params);
63    }
64    }
65   
 
66  7 toggle public UserPreferences(User pUser, boolean bulkload)
67    {
68    // Use this set even if the pUser is null
69    // This will save some operations on checking if the userPs is null before
70    // looking at the default properties, and look into the default properties straight away
71  7 defaultKeys = new HashSet();
72   
73  7 if (pUser != null)
74    {
75  5 PropertySet userPs = pUser.getPropertySet();
76   
77  5 Map params = new HashMap(2);
78  5 params.put("PropertySet", userPs);
79  5 params.put("bulkload", new Boolean(bulkload));
80   
81  5 backingPS = PropertySetManager.getInstance("cached", params);
82    }
83    }
84   
 
85  4 toggle public long getLong(String key)
86    {
87    // Check if the default value should be used for this key
88  4 if (defaultKeys.contains(key))
89    {
90    // If the default value is used for the key just return it
91  0 return DefaultPreferences.getPreferences().getLong(key);
92    }
93    else
94    {
95  4 if (backingPS != null && backingPS.exists(key))
96    {
97  2 return backingPS.getLong(key);
98    }
99    else
100    {
101    // Remember that the default value for this key is used for the user
102    // So that we do not have look it up again
103  2 defaultKeys.add(key);
104    // Return the default value
105  2 return DefaultPreferences.getPreferences().getLong(key);
106    }
107    }
108    }
109   
 
110  2 toggle public void setLong(String key, long i) throws AtlassianCoreException
111    {
112  2 if (backingPS == null)
113    {
114  1 throw new AtlassianCoreException("Trying to set a property on a null user this is not allowed");
115    }
116    else
117    {
118    // Do not use the default value (if one was not used before, it does not matter)
119  1 defaultKeys.remove(key);
120  1 backingPS.setLong(key, i);
121    }
122    }
123   
 
124  0 toggle public String getString(String key)
125    {
126    // Check if the default value should be used for this key
127  0 if (defaultKeys.contains(key))
128    {
129    // If the default value is used for the key just return it
130  0 return DefaultPreferences.getPreferences().getString(key);
131    }
132    else
133    {
134  0 if (backingPS != null && backingPS.exists(key))
135    {
136  0 return backingPS.getString(key);
137    }
138    else
139    {
140    // Remember that the default value for this key is used for the user
141  0 defaultKeys.add(key);
142  0 return DefaultPreferences.getPreferences().getString(key);
143    }
144    }
145    }
146   
 
147  0 toggle public void setString(String key, String value) throws AtlassianCoreException
148    {
149  0 if (backingPS == null)
150    {
151  0 throw new AtlassianCoreException("Trying to set a property on a null user this is not allowed");
152    }
153    else
154    {
155    // Do not use the default value (if one was not used before, it does not matter)
156  0 defaultKeys.remove(key);
157  0 backingPS.setString(key, value);
158    }
159    }
160   
 
161  0 toggle public boolean getBoolean(String key)
162    {
163    // Check if the default value should be used for this key
164  0 if (defaultKeys.contains(key))
165    {
166    // If the default value is used for the key just return it
167  0 return DefaultPreferences.getPreferences().getBoolean(key);
168    }
169    else
170    {
171  0 if (backingPS != null && backingPS.exists(key))
172    {
173  0 return backingPS.getBoolean(key);
174    }
175    else
176    {
177    // Remember that the default value for this key is used for the user
178  0 defaultKeys.add(key);
179  0 return DefaultPreferences.getPreferences().getBoolean(key);
180    }
181    }
182    }
183   
 
184  0 toggle public void setBoolean(String key, boolean b) throws AtlassianCoreException
185    {
186  0 if (backingPS == null)
187    {
188  0 throw new AtlassianCoreException("Trying to set a property on a null user this is not allowed");
189    }
190    else
191    {
192    // Do not use the default value (if one was not used before, it does not matter)
193  0 defaultKeys.remove(key);
194  0 backingPS.setBoolean(key, b);
195    }
196    }
197   
 
198  0 toggle public void remove(String key) throws AtlassianCoreException
199    {
200  0 if (backingPS == null)
201    {
202  0 throw new AtlassianCoreException("Trying to remove a property on a null user this is not allowed");
203    }
204    else
205    {
206  0 if (backingPS.exists(key))
207    {
208    // Do not use the default value (if one was not used before, it does not matter)
209  0 defaultKeys.remove(key);
210  0 backingPS.remove(key);
211    }
212    else
213    {
214  0 throw new AtlassianCoreException("The property with key '" + key + "' does not exist.");
215    }
216    }
217    }
218   
 
219  3 toggle public boolean equals(Object o)
220    {
221    // NOTE: the defaultKeys is not used to determine equality of this object.
222  3 if (this == o) return true;
223  1 if (!(o instanceof UserPreferences)) return false;
224   
225  0 final UserPreferences userPreferences = (UserPreferences) o;
226   
227  0 if (backingPS != null ? !PropertyUtils.identical(backingPS, userPreferences.backingPS) : userPreferences.backingPS != null) return false;
228   
229  0 return true;
230    }
231   
 
232  2 toggle public int hashCode()
233    {
234    // NOTE: the defaultKeys is not used to determine the hashCode of this object.
235  2 return (backingPS != null ? backingPS.hashCode() : 0);
236    }
237    }