View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.bindings.cache.impl;
20  
21  import java.util.Map;
22  
23  import org.apache.chemistry.opencmis.client.bindings.cache.CacheLevel;
24  
25  /**
26   * Abstract Map cache.
27   */
28  public abstract class AbstractMapCacheLevel implements CacheLevel {
29  
30      private static final long serialVersionUID = 1L;
31  
32      private Map<String, Object> fMap;
33      private boolean fFallbackEnabled = false;
34      private String fFallbackKey;
35      private boolean fSingleValueEnabled = false;
36  
37      @Override
38      public abstract void initialize(Map<String, String> parameters);
39  
40      @Override
41      public Object get(String key) {
42          Object value = fMap.get(key);
43  
44          if (value == null && fFallbackEnabled) {
45              value = fMap.get(fFallbackKey);
46          }
47  
48          if (value == null && fSingleValueEnabled) {
49              if (fMap.size() == 1) {
50                  value = fMap.values().iterator().next();
51              }
52          }
53  
54          return value;
55      }
56  
57      @Override
58      public void put(Object value, String key) {
59          fMap.put(key, value);
60      }
61  
62      @Override
63      public void remove(String key) {
64          fMap.remove(key);
65      }
66  
67      /**
68       * Returns the internal map.
69       */
70      protected Map<String, Object> getMap() {
71          return fMap;
72      }
73  
74      /**
75       * Sets the internal map.
76       */
77      protected void setMap(Map<String, Object> map) {
78          fMap = map;
79      }
80  
81      /**
82       * Enables a fallback key if no value was found for a requested key.
83       */
84      protected void enableKeyFallback(String key) {
85          fFallbackKey = key;
86          fFallbackEnabled = true;
87      }
88  
89      /**
90       * Disables the fallback key.
91       */
92      protected void disableKeyFallback() {
93          fFallbackEnabled = false;
94      }
95  
96      /**
97       * Enables the single value fallback.
98       */
99      protected void enableSingeValueFallback() {
100         fSingleValueEnabled = true;
101     }
102 
103     /**
104      * Disables the single value fallback.
105      */
106     protected void disableSingeValueFallback() {
107         fSingleValueEnabled = false;
108     }
109 
110     /**
111      * Extracts an integer parameter from the parameters.
112      * 
113      * @param parameters
114      *            the parameter map
115      * @param name
116      *            the parameter name
117      * @param defValue
118      *            the default value if the parameter can't be found
119      */
120     protected int getIntParameter(Map<String, String> parameters, String name, int defValue) {
121         if (parameters == null) {
122             return defValue;
123         }
124 
125         String value = parameters.get(name);
126         if (value == null || value.trim().length() == 0) {
127             return defValue;
128         }
129 
130         try {
131             return Integer.valueOf(value);
132         } catch (NumberFormatException e) {
133             return defValue;
134         }
135     }
136 
137     /**
138      * Extracts a float parameter from the parameters.
139      * 
140      * @param parameters
141      *            the parameter map
142      * @param name
143      *            the parameter name
144      * @param defValue
145      *            the default value if the parameter can't be found
146      */
147     protected float getFloatParameter(Map<String, String> parameters, String name, float defValue) {
148         if (parameters == null) {
149             return defValue;
150         }
151 
152         String value = parameters.get(name);
153         if (value == null || value.trim().length() == 0) {
154             return defValue;
155         }
156 
157         try {
158             return Float.valueOf(value);
159         } catch (NumberFormatException e) {
160             return defValue;
161         }
162     }
163 
164     /**
165      * Extracts a boolean parameter from the parameters.
166      * 
167      * @param parameters
168      *            the parameter map
169      * @param name
170      *            the parameter name
171      * @param defValue
172      *            the default value if the parameter can't be found
173      */
174     protected boolean getBooleanParameter(Map<String, String> parameters, String name, boolean defValue) {
175         if (parameters == null) {
176             return defValue;
177         }
178 
179         String value = parameters.get(name);
180         if (value == null || value.trim().length() == 0) {
181             return defValue;
182         }
183 
184         return Boolean.parseBoolean(value);
185     }
186 
187     /*
188      * (non-Javadoc)
189      * 
190      * @see java.lang.Object#toString()
191      */
192     @Override
193     public String toString() {
194         return fMap == null ? "[no map]" : fMap.toString();
195     }
196 }