001: /*
002: * $Id: MapEntryCombiner.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.config.spring.parsers.assembly;
012:
013: import java.util.Collection;
014: import java.util.HashMap;
015: import java.util.Map;
016: import java.util.Set;
017:
018: /**
019: * This is used internally by {@link org.mule.config.spring.parsers.assembly.DefaultBeanAssembler}
020: * along with {@link org.mule.config.spring.parsers.collection.ChildSingletonMapDefinitionParser}.
021: * It creates a map with a single key/value pair. This may seem odd, but the result is not
022: * manipulated within the assembler - that means that, unlike
023: * {@link org.mule.config.spring.parsers.collection.ChildMapEntryDefinitionParser}, this element
024: * can contain nested values. Note that most uses will set
025: * {@link org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration#isCollection(String)}
026: * so that several entries can be combined.
027: */
028: public class MapEntryCombiner implements Map {
029:
030: public static final String KEY = "key";
031: public static final String VALUE = "value";
032:
033: private Object key;
034: private Object value;
035: private Map cachedMerge = new HashMap();
036: private boolean isMerged = false;
037:
038: private synchronized Map getCachedMerge() {
039: if (!isMerged) {
040: cachedMerge.put(key, value);
041: isMerged = true;
042: }
043: return cachedMerge;
044: }
045:
046: public Object getKey() {
047: assertNotMerged();
048: return key;
049: }
050:
051: public void setKey(Object key) {
052: assertNotMerged();
053: this .key = key;
054: }
055:
056: public Object getValue() {
057: assertNotMerged();
058: return value;
059: }
060:
061: public void setValue(Object value) {
062: assertNotMerged();
063: this .value = value;
064: }
065:
066: private synchronized void assertNotMerged() {
067: if (isMerged) {
068: throw new IllegalStateException(
069: "Maps have already been merged");
070: }
071: }
072:
073: // map delegates (except hashCode and equals)
074:
075: public int size() {
076: return getCachedMerge().size();
077: }
078:
079: public void clear() {
080: getCachedMerge().clear();
081: }
082:
083: public boolean isEmpty() {
084: return getCachedMerge().isEmpty();
085: }
086:
087: public boolean containsKey(Object key) {
088: return getCachedMerge().containsKey(key);
089: }
090:
091: public boolean containsValue(Object value) {
092: return getCachedMerge().containsValue(value);
093: }
094:
095: public Collection values() {
096: return getCachedMerge().values();
097: }
098:
099: public void putAll(Map t) {
100: getCachedMerge().putAll(t);
101: }
102:
103: public Set entrySet() {
104: return getCachedMerge().entrySet();
105: }
106:
107: public Set keySet() {
108: return getCachedMerge().keySet();
109: }
110:
111: public Object get(Object key) {
112: return getCachedMerge().get(key);
113: }
114:
115: public Object remove(Object key) {
116: return getCachedMerge().remove(key);
117: }
118:
119: public Object put(Object key, Object value) {
120: return getCachedMerge().put(key, value);
121: }
122:
123: }
|