View Javadoc

1   /**
2    * Copyright 2008 Bryan Ray
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0 
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package uk.org.bryanray.testtoys.generator;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import uk.org.bryanray.testtoys.generator.sequencegenerators.SequenceGenerator;
26  
27  /**
28   * <DL>
29   * <DT><B>Project:</B></DT>
30   * <DD>TestDataLoader</DD>
31   * <DT><B>Filename:</B></DT>
32   * <DD>SequenceMapAdapter.java</DD>
33   * <DT><B>Creation Date:</B></DT>
34   * <DD>26 May 2008</DD>
35   * </DL>
36   * 
37   * Wraps a {@link Map<String,SequenceGenerator>} so that it can be used to
38   * populate a template. This class does not implement all methods. The map
39   * values will appear to change as the objects in the target map appear to
40   * change.
41   * 
42   * @author Bryan Ray
43   */
44  public class SequenceMapAdapter implements Map<String, String> {
45  
46  	// TODO This class could be parameterised by using a functor that calls the
47  	// appropriate method on the object.
48  	
49  	private Map<String, SequenceGenerator> map;
50  
51  	/**
52  	 * @param sequenceMap The map to wrap.
53  	 * */
54  	public SequenceMapAdapter(Map<String, SequenceGenerator> sequenceMap) {
55  		map = sequenceMap;
56  	}
57  
58  	/**
59  	 * {@inheritDoc}
60  	 */
61  	public void clear() {
62  		map.clear();
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 */
68  	public boolean containsKey(Object key) {
69  		return map.containsKey(key);
70  	}
71  
72  	/**
73  	 * {@inheritDoc}
74  	 */
75  	public boolean containsValue(Object value) {
76  		if (null != value) {
77  			for (Iterator<SequenceGenerator> i = map.values().iterator(); i
78  					.hasNext();) {
79  				SequenceGenerator sequenceGenerator = i.next();
80  				if (sequenceGenerator.value().equals(value)) {
81  					return true;
82  				}
83  			}
84  		}
85  		return false;
86  	}
87  
88  	/**
89  	 * {@inheritDoc}
90  	 */
91  	public Set<Entry<String, String>> entrySet() {
92  		Set<Entry<String, String>> set = new HashSet<Entry<String, String>>();
93  		for (Iterator<Entry<String, SequenceGenerator>> i = map.entrySet()
94  				.iterator(); i.hasNext();) {
95  			Entry<String, SequenceGenerator> e = i.next();
96  			set.add(new AdapterEntry(e.getKey(), e.getValue().value()));
97  		}
98  		return set;
99  
100 	}
101 
102 	/**
103 	 * {@inheritDoc}
104 	 */
105 	public String get(Object key) {
106 		return map.get(key).value();
107 	}
108 
109 	/**
110 	 * {@inheritDoc}
111 	 */
112 	public boolean isEmpty() {
113 		return map.isEmpty();
114 	}
115 
116 	/**
117 	 * {@inheritDoc}
118 	 */
119 	public Set<String> keySet() {
120 		return map.keySet();
121 	}
122 
123 	/**
124 	 * This method is not supported.
125 	 * 
126 	 * {@inheritDoc}
127 	 */
128 	public String put(String key, String value) {
129 		throw new UnsupportedOperationException();
130 	}
131 
132 	/**
133 	 * This method is not supported.
134 	 * 
135 	 * {@inheritDoc}
136 	 */
137 	public void putAll(Map<? extends String, ? extends String> t) {
138 		throw new UnsupportedOperationException();
139 	}
140 
141 	/**
142 	 * {@inheritDoc}
143 	 */
144 	public String remove(Object key) {
145 		return map.remove(key).value();
146 	}
147 
148 	/**
149 	 * {@inheritDoc}
150 	 */
151 	public int size() {
152 		return map.size();
153 	}
154 
155 	/**
156 	 * {@inheritDoc}
157 	 */
158 	public Collection<String> values() {
159 		Collection<String> result = new ArrayList<String>();
160 		for (Iterator<SequenceGenerator> i = map.values().iterator(); i
161 				.hasNext();) {
162 			SequenceGenerator sequenceGenerator = i.next();
163 			result.add(sequenceGenerator.value());
164 		}
165 		return result;
166 	}
167 
168 	/**
169 	 * A class that implements the {@link Entry} in a {@link SequenceMapAdapter}.
170 	 */
171 	private class AdapterEntry implements Entry<String, String> {
172 
173 		private String key;
174 		private String value;
175 
176 		public AdapterEntry(String key, String value) {
177 			this.key = key;
178 			this.value = value;
179 		}
180 
181 		/**
182 		 * {@inheritDoc}
183 		 */
184 		public String getKey() {
185 			return key;
186 		}
187 
188 		/**
189 		 * {@inheritDoc}
190 		 */
191 		public String getValue() {
192 			return value;
193 		}
194 
195 		/**
196 		 * {@inheritDoc}
197 		 */
198 		public String setValue(String value) {
199 			String oldValue = this.value;
200 			this.value = value;
201 			return oldValue;
202 		}
203 	}
204 }