001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.strategy;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.commons.betwixt.expression.Context;
024:
025: /**
026: * <p>Stores every ID that given to it into an internal <code>HashMap</code> and
027: * returns it on request.
028: * </p><p>
029: * {@link #DefaultIdStoringStrategy(Map, Map)} allows the implementations
030: * to be specified.
031: * For example, those who want to use identity (rather than equality)
032: * should pass a <code>IdentityHashMap</code> instance.
033: * </p>
034: *
035: * @author <a href="mailto:christian@wilde-welt.de">Christian Aust</a>
036: * @since 0.7
037: */
038: public class DefaultIdStoringStrategy extends IdStoringStrategy {
039: private Map idByBeanMap;
040: private Map beanByIdMap;
041:
042: /**
043: * Constructs a {@link IdStoringStrategy} using a <code>HashMap</code> for
044: * storage.
045: */
046: public DefaultIdStoringStrategy() {
047: this (new HashMap(), new HashMap());
048: }
049:
050: /**
051: * Constructs a {@link IdStoringStrategy}using the <code>Map</code>
052: * implementations provided for storage.
053: *
054: * @param idByBeanMap <code>Map</code> implementation stores the ID's by bean
055: * @param beanByIdMap <code>Map</code> implementation stores the bean's by ID
056: * @since 0.8
057: */
058: public DefaultIdStoringStrategy(Map idByBeanMap, Map beanByIdMap) {
059: this .idByBeanMap = idByBeanMap;
060: this .beanByIdMap = beanByIdMap;
061: }
062:
063: /**
064: * Returns a String id for the given bean if it has been stored previously.
065: * Otherwise returns null.
066: *
067: * @param context
068: * current context, not null
069: * @param bean
070: * the instance, not null
071: * @return id as String, or null if not found
072: * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#getReferenceFor(org.apache.commons.betwixt.expression.Context,
073: * java.lang.Object)
074: */
075: public String getReferenceFor(Context context, Object bean) {
076: return (String) idByBeanMap.get(bean);
077: }
078:
079: /**
080: * Stores an ID for the given instance and context. It will check first if
081: * this ID has been previously stored and will do nothing in that case.
082: *
083: * @param context
084: * current context, not null
085: * @param bean
086: * current instance, not null
087: * @param id
088: * the ID to store
089: * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#setReference(org.apache.commons.betwixt.expression.Context,
090: * java.lang.Object, java.lang.String)
091: */
092: public void setReference(Context context, Object bean, String id) {
093: if (!idByBeanMap.containsKey(bean)) {
094: idByBeanMap.put(bean, id);
095: beanByIdMap.put(id, bean);
096: }
097: }
098:
099: /**
100: * Gets an object matching the given reference.
101: * @param context <code>Context</code>, not null
102: * @param id the reference id
103: * @return an bean matching the given reference,
104: * or null if there is no bean matching the given reference
105: */
106: public Object getReferenced(Context context, String id) {
107: return beanByIdMap.get(id);
108: }
109:
110: /**
111: * Clears all beans.
112: */
113: public void reset() {
114: idByBeanMap.clear();
115: beanByIdMap.clear();
116: }
117:
118: }
|