1 52 53 package freemarker.template.utility; 54 55 import java.io.Serializable ; 56 import java.util.AbstractList ; 57 import java.util.AbstractMap ; 58 import java.util.Collection ; 59 import java.util.Collections ; 60 import java.util.List ; 61 import java.util.Map ; 62 import java.util.Set ; 63 64 69 public class Collections12 70 { 71 public static final Map EMPTY_MAP = new EmptyMap(); 72 73 private Collections12() 74 { 75 } 76 77 private static final class EmptyMap 78 extends AbstractMap 79 implements Serializable 80 { 81 public int size() 82 { 83 return 0; 84 } 85 86 public boolean isEmpty() 87 { 88 return true; 89 } 90 91 public boolean containsKey(Object key) 92 { 93 return false; 94 } 95 96 public boolean containsValue(Object value) 97 { 98 return false; 99 } 100 101 public Object get(Object key) 102 { 103 return null; 104 } 105 106 public Set keySet() 107 { 108 return Collections.EMPTY_SET; 109 } 110 111 public Collection values() 112 { 113 return Collections.EMPTY_SET; 114 } 115 116 public Set entrySet() 117 { 118 return Collections.EMPTY_SET; 119 } 120 121 public boolean equals(Object o) 122 { 123 return (o instanceof Map ) && ((Map ) o).size() == 0; 124 } 125 126 public int hashCode() 127 { 128 return 0; 129 } 130 } 131 132 public static Map singletonMap(Object key, Object value) 133 { 134 return new SingletonMap(key, value); 135 } 136 137 private static class SingletonMap 138 extends AbstractMap 139 implements Serializable 140 { 141 private final Object k, v; 142 143 SingletonMap(Object key, Object value) 144 { 145 k = key; 146 v = value; 147 } 148 149 public int size() 150 { 151 return 1; 152 } 153 154 public boolean isEmpty() 155 { 156 return false; 157 } 158 159 public boolean containsKey(Object key) 160 { 161 return eq(key, k); 162 } 163 164 public boolean containsValue(Object value) 165 { 166 return eq(value, v); 167 } 168 169 public Object get(Object key) 170 { 171 return (eq(key, k) ? v : null); 172 } 173 174 private transient Set keySet = null; 175 private transient Set entrySet = null; 176 private transient Collection values = null; 177 178 public Set keySet() 179 { 180 if (keySet == null) 181 keySet = Collections.singleton(k); 182 return keySet; 183 } 184 185 public Set entrySet() 186 { 187 if (entrySet == null) 188 entrySet = Collections.singleton(new ImmutableEntry(k, v)); 189 return entrySet; 190 } 191 192 public Collection values() 193 { 194 if (values == null) 195 values = Collections.singleton(v); 196 return values; 197 } 198 199 private static class ImmutableEntry implements Map.Entry 200 { 201 final Object k; 202 final Object v; 203 204 ImmutableEntry(Object key, Object value) 205 { 206 k = key; 207 v = value; 208 } 209 210 public Object getKey() 211 { 212 return k; 213 } 214 215 public Object getValue() 216 { 217 return v; 218 } 219 220 public Object setValue(Object value) 221 { 222 throw new UnsupportedOperationException (); 223 } 224 225 public boolean equals(Object o) 226 { 227 if (!(o instanceof Map.Entry )) 228 return false; 229 Map.Entry e = (Map.Entry ) o; 230 return eq(e.getKey(), k) && eq(e.getValue(), v); 231 } 232 233 public int hashCode() 234 { 235 return ( 236 (k == null ? 0 : k.hashCode()) 237 ^ (v == null ? 0 : v.hashCode())); 238 } 239 240 public String toString() 241 { 242 return k + "=" + v; 243 } 244 } 245 } 246 247 public static List singletonList(Object o) 248 { 249 return new SingletonList(o); 250 } 251 252 private static class SingletonList 253 extends AbstractList 254 implements Serializable 255 { 256 private final Object element; 257 258 SingletonList(Object obj) 259 { 260 element = obj; 261 } 262 263 public int size() 264 { 265 return 1; 266 } 267 268 public boolean contains(Object obj) 269 { 270 return eq(obj, element); 271 } 272 273 public Object get(int index) 274 { 275 if (index != 0) 276 throw new IndexOutOfBoundsException ( 277 "Index: " + index + ", Size: 1"); 278 return element; 279 } 280 } 281 282 private static boolean eq(Object o1, Object o2) 283 { 284 return (o1 == null ? o2 == null : o1.equals(o2)); 285 } 286 } 287 | Popular Tags |