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