View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.betwixt.strategy;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  import org.apache.commons.betwixt.expression.Context;
22  
23  /***
24   * Stores every ID that given to it into an internal <code>HashMap</code> and
25   * returns it on request.
26   * 
27   * @author <a href="mailto:christian@wilde-welt.de">Christian Aust </a>
28   * @since 0.7
29   */
30  public class DefaultIdStoringStrategy extends IdStoringStrategy {
31      private Map idByBeanMap;
32      private Map beanByIdMap;
33  
34      /***
35       * Constructs a {@link IdStoringStrategy}using a <code>HashMap</code> for
36       * storage.
37       */
38      public DefaultIdStoringStrategy() {
39          idByBeanMap = new HashMap();
40          beanByIdMap = new HashMap();
41      }
42  
43      /***
44       * Returns a String id for the given bean if it has been stored previously.
45       * Otherwise returns null.
46       * 
47       * @param context
48       *            current context, not null
49       * @param bean
50       *            the instance, not null
51       * @return id as String, or null if not found
52       * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#getReferenceFor(org.apache.commons.betwixt.expression.Context,
53       *      java.lang.Object)
54       */
55      public String getReferenceFor(Context context, Object bean) {
56          return (String) idByBeanMap.get(bean);
57      }
58  
59      /***
60       * Stores an ID for the given instance and context. It will check first if
61       * this ID has been previously stored and will do nothing in that case.
62       * 
63       * @param context
64       *            current context, not null
65       * @param bean
66       *            current instance, not null
67       * @param id
68       *            the ID to store
69       * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#setReference(org.apache.commons.betwixt.expression.Context,
70       *      java.lang.Object, java.lang.String)
71       */
72      public void setReference(Context context, Object bean, String id) {
73          if (!idByBeanMap.containsKey(bean)) {
74              idByBeanMap.put(bean, id);
75              beanByIdMap.put(id, bean);
76          }
77      }
78      
79      /***
80       * Gets an object matching the given reference.
81       * @param context <code>Context</code>, not null
82       * @param id the reference id
83       * @return an bean matching the given reference, 
84       * or null if there is no bean matching the given reference
85       */
86      public Object getReferenced(Context context, String id) {
87          return beanByIdMap.get(id);
88      }
89  
90      /***
91       * Clears all beans.
92       */
93      public void reset() {
94          idByBeanMap.clear();
95          beanByIdMap.clear();
96      }
97      
98      
99  }