View Javadoc

1   /*
2    * $Id: MapEntryCombiner.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.spring.parsers.assembly;
12  
13  import java.util.Collection;
14  import java.util.HashMap;
15  import java.util.Map;
16  import java.util.Set;
17  
18  /**
19   * This is used internally by {@link org.mule.config.spring.parsers.assembly.DefaultBeanAssembler}
20   * along with {@link org.mule.config.spring.parsers.collection.ChildSingletonMapDefinitionParser}.
21   * It creates a map with a single key/value pair.  This may seem odd, but the result is not
22   * manipulated within the assembler - that means that, unlike
23   * {@link org.mule.config.spring.parsers.collection.ChildMapEntryDefinitionParser}, this element
24   * can contain nested values.  Note that most uses will set
25   * {@link org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration#isCollection(String)}
26   * so that several entries can be combined.
27   */
28  public class MapEntryCombiner implements Map
29  {
30  
31      public static final String KEY = "key";
32      public static final String VALUE = "value";
33  
34      private Object key;
35      private Object value;
36      private Map cachedMerge = new HashMap();
37      private boolean isMerged = false;
38  
39      private synchronized Map getCachedMerge()
40      {
41          if (!isMerged)
42          {
43              cachedMerge.put(key, value);
44              isMerged = true;
45          }
46          return cachedMerge;
47      }
48  
49      public Object getKey()
50      {
51          assertNotMerged();
52          return key;
53      }
54  
55      public void setKey(Object key)
56      {
57          assertNotMerged();
58          this.key = key;
59      }
60  
61      public Object getValue()
62      {
63          assertNotMerged();
64          return value;
65      }
66  
67      public void setValue(Object value)
68      {
69          assertNotMerged();
70          this.value = value;
71      }
72  
73      private synchronized void assertNotMerged()
74      {
75          if (isMerged)
76          {
77              throw new IllegalStateException("Maps have already been merged");
78          }
79      }
80  
81      // map delegates (except hashCode and equals)
82  
83      public int size()
84      {
85          return getCachedMerge().size();
86      }
87  
88      public void clear()
89      {
90          getCachedMerge().clear();
91      }
92  
93      public boolean isEmpty()
94      {
95          return getCachedMerge().isEmpty();
96      }
97  
98      public boolean containsKey(Object key)
99      {
100         return getCachedMerge().containsKey(key);
101     }
102 
103     public boolean containsValue(Object value)
104     {
105         return getCachedMerge().containsValue(value);
106     }
107 
108     public Collection values()
109     {
110         return getCachedMerge().values();
111     }
112 
113     public void putAll(Map t)
114     {
115         getCachedMerge().putAll(t);
116     }
117 
118     public Set entrySet()
119     {
120         return getCachedMerge().entrySet();
121     }
122 
123     public Set keySet()
124     {
125         return getCachedMerge().keySet();
126     }
127 
128     public Object get(Object key)
129     {
130         return getCachedMerge().get(key);
131     }
132 
133     public Object remove(Object key)
134     {
135         return getCachedMerge().remove(key);
136     }
137 
138     public Object put(Object key, Object value)
139     {
140         return getCachedMerge().put(key, value);
141     }
142 
143 }