View Javadoc

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