This project has retired. For details please refer to its Attic page.
AbstractMapCacheLevel xref

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   * @author <a href="mailto:fmueller@opentext.com">Florian M&uuml;ller</a>
29   * 
30   */
31  public abstract class AbstractMapCacheLevel implements CacheLevel {
32  
33      private static final long serialVersionUID = 1L;
34  
35      private Map<String, Object> fMap;
36      private boolean fFallbackEnabled = false;
37      private String fFallbackKey;
38      private boolean fSingleValueEnabled = false;
39  
40      public abstract void initialize(Map<String, String> parameters);
41  
42      public Object get(String key) {
43          Object value = fMap.get(key);
44  
45          if ((value == null) && fFallbackEnabled) {
46              value = fMap.get(fFallbackKey);
47          }
48  
49          if ((value == null) && fSingleValueEnabled) {
50              if (fMap.size() == 1) {
51                  value = fMap.values().iterator().next();
52              }
53          }
54  
55          return value;
56      }
57  
58      public void put(Object value, String key) {
59          fMap.put(key, value);
60      }
61  
62      public void remove(String key) {
63          fMap.remove(key);
64      }
65  
66      /**
67       * Returns the internal map.
68       */
69      protected Map<String, Object> getMap() {
70          return fMap;
71      }
72  
73      /**
74       * Sets the internal map.
75       */
76      protected void setMap(Map<String, Object> map) {
77          fMap = map;
78      }
79  
80      /**
81       * Enables a fallback key if no value was found for a requested key.
82       */
83      protected void enableKeyFallback(String key) {
84          fFallbackKey = key;
85          fFallbackEnabled = true;
86      }
87  
88      /**
89       * Disables the fallback key.
90       */
91      protected void disableKeyFallback() {
92          fFallbackEnabled = false;
93      }
94  
95      /**
96       * Enables the single value fallback.
97       */
98      protected void enableSingeValueFallback() {
99          fSingleValueEnabled = true;
100     }
101 
102     /**
103      * Disables the single value fallback.
104      */
105     protected void disableSingeValueFallback() {
106         fSingleValueEnabled = false;
107     }
108 
109     /**
110      * Extracts an integer parameter from the parameters.
111      * 
112      * @param parameters
113      *            the parameter map
114      * @param name
115      *            the parameter name
116      * @param defValue
117      *            the default value if the parameter can't be found
118      */
119     protected int getIntParameter(Map<String, String> parameters, String name, int defValue) {
120         if (parameters == null) {
121             return defValue;
122         }
123 
124         String value = parameters.get(name);
125         if ((value == null) || (value.trim().length() == 0)) {
126             return defValue;
127         }
128 
129         try {
130             return Integer.valueOf(value);
131         } catch (NumberFormatException e) {
132             return defValue;
133         }
134     }
135 
136     /**
137      * Extracts a float parameter from the parameters.
138      * 
139      * @param parameters
140      *            the parameter map
141      * @param name
142      *            the parameter name
143      * @param defValue
144      *            the default value if the parameter can't be found
145      */
146     protected float getFloatParameter(Map<String, String> parameters, String name, float defValue) {
147         if (parameters == null) {
148             return defValue;
149         }
150 
151         String value = parameters.get(name);
152         if ((value == null) || (value.trim().length() == 0)) {
153             return defValue;
154         }
155 
156         try {
157             return Float.valueOf(value);
158         } catch (NumberFormatException e) {
159             return defValue;
160         }
161     }
162 
163     /**
164      * Extracts a boolean parameter from the parameters.
165      * 
166      * @param parameters
167      *            the parameter map
168      * @param name
169      *            the parameter name
170      * @param defValue
171      *            the default value if the parameter can't be found
172      */
173     protected boolean getBooleanParameter(Map<String, String> parameters, String name, boolean defValue) {
174         if (parameters == null) {
175             return defValue;
176         }
177 
178         String value = parameters.get(name);
179         if ((value == null) || (value.trim().length() == 0)) {
180             return defValue;
181         }
182 
183         return Boolean.parseBoolean(value);
184     }
185 
186     /*
187      * (non-Javadoc)
188      * 
189      * @see java.lang.Object#toString()
190      */
191     @Override
192     public String toString() {
193         return (fMap == null ? "[no map]" : fMap.toString());
194     }
195 }