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 /** 065 * Returns a String id for the given bean if it has been stored previously. 066 * Otherwise returns null. 067 * 068 * @param context 069 * current context, not null 070 * @param bean 071 * the instance, not null 072 * @return id as String, or null if not found 073 * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#getReferenceFor(org.apache.commons.betwixt.expression.Context, 074 * java.lang.Object) 075 */ 076 public String getReferenceFor(Context context, Object bean) { 077 return (String) idByBeanMap.get(bean); 078 } 079 080 /** 081 * Stores an ID for the given instance and context. It will check first if 082 * this ID has been previously stored and will do nothing in that case. 083 * 084 * @param context 085 * current context, not null 086 * @param bean 087 * current instance, not null 088 * @param id 089 * the ID to store 090 * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#setReference(org.apache.commons.betwixt.expression.Context, 091 * java.lang.Object, java.lang.String) 092 */ 093 public void setReference(Context context, Object bean, String id) { 094 if (!idByBeanMap.containsKey(bean)) { 095 idByBeanMap.put(bean, id); 096 beanByIdMap.put(id, bean); 097 } 098 } 099 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 }