Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
DefaultIdStoringStrategy |
|
| 1.1666666666666667;1.167 |
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 | ||
22 | import org.apache.commons.betwixt.expression.Context; |
|
23 | ||
24 | /** |
|
25 | * <p>Stores every ID that given to it into an internal <code>HashMap</code> and |
|
26 | * returns it on request. |
|
27 | * </p><p> |
|
28 | * {@link #DefaultIdStoringStrategy(Map, Map)} allows the implementations |
|
29 | * to be specified. |
|
30 | * For example, those who want to use identity (rather than equality) |
|
31 | * should pass a <code>IdentityHashMap</code> instance. |
|
32 | * </p> |
|
33 | * |
|
34 | * @author <a href="mailto:christian@wilde-welt.de">Christian Aust</a> |
|
35 | * @since 0.7 |
|
36 | */ |
|
37 | public class DefaultIdStoringStrategy extends IdStoringStrategy { |
|
38 | private Map idByBeanMap; |
|
39 | private Map beanByIdMap; |
|
40 | ||
41 | /** |
|
42 | * Constructs a {@link IdStoringStrategy} using a <code>HashMap</code> for |
|
43 | * storage. |
|
44 | */ |
|
45 | 1047 | public DefaultIdStoringStrategy() { |
46 | 4597 | this(new HashMap(), new HashMap()); |
47 | 3550 | } |
48 | ||
49 | /** |
|
50 | * Constructs a {@link IdStoringStrategy}using the <code>Map</code> |
|
51 | * implementations provided for storage. |
|
52 | * |
|
53 | * @param idByBeanMap <code>Map</code> implementation stores the ID's by bean |
|
54 | * @param beanByIdMap <code>Map</code> implementation stores the bean's by ID |
|
55 | 1047 | */ |
56 | 4597 | public DefaultIdStoringStrategy(Map idByBeanMap, Map beanByIdMap) { |
57 | 4597 | this.idByBeanMap = idByBeanMap; |
58 | 4597 | this.beanByIdMap = beanByIdMap; |
59 | 3550 | } |
60 | ||
61 | ||
62 | /** |
|
63 | * Returns a String id for the given bean if it has been stored previously. |
|
64 | * Otherwise returns null. |
|
65 | * |
|
66 | * @param context |
|
67 | * current context, not null |
|
68 | * @param bean |
|
69 | * the instance, not null |
|
70 | * @return id as String, or null if not found |
|
71 | * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#getReferenceFor(org.apache.commons.betwixt.expression.Context, |
|
72 | * java.lang.Object) |
|
73 | */ |
|
74 | 255 | public String getReferenceFor(Context context, Object bean) { |
75 | 850 | return (String) idByBeanMap.get(bean); |
76 | } |
|
77 | ||
78 | /** |
|
79 | * Stores an ID for the given instance and context. It will check first if |
|
80 | * this ID has been previously stored and will do nothing in that case. |
|
81 | * |
|
82 | * @param context |
|
83 | * current context, not null |
|
84 | * @param bean |
|
85 | * current instance, not null |
|
86 | * @param id |
|
87 | * the ID to store |
|
88 | * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#setReference(org.apache.commons.betwixt.expression.Context, |
|
89 | * java.lang.Object, java.lang.String) |
|
90 | */ |
|
91 | 1869 | public void setReference(Context context, Object bean, String id) { |
92 | 8051 | if (!idByBeanMap.containsKey(bean)) { |
93 | 7871 | idByBeanMap.put(bean, id); |
94 | 6056 | beanByIdMap.put(id, bean); |
95 | 1869 | } |
96 | 6236 | } |
97 | ||
98 | /** |
|
99 | * Gets an object matching the given reference. |
|
100 | * @param context <code>Context</code>, not null |
|
101 | * @param id the reference id |
|
102 | * @return an bean matching the given reference, |
|
103 | * or null if there is no bean matching the given reference |
|
104 | */ |
|
105 | 6 | public Object getReferenced(Context context, String id) { |
106 | 20 | return beanByIdMap.get(id); |
107 | } |
|
108 | ||
109 | /** |
|
110 | * Clears all beans. |
|
111 | */ |
|
112 | 342 | public void reset() { |
113 | 1500 | idByBeanMap.clear(); |
114 | 1500 | beanByIdMap.clear(); |
115 | 1158 | } |
116 | ||
117 | ||
118 | } |