1 16 package com.google.gwt.i18n.client.impl; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 33 public class ConstantMap extends HashMap { 34 35 private static class DummyMapEntry implements Map.Entry { 36 private final Object key; 37 38 private final Object value; 39 40 DummyMapEntry(Object key, Object value) { 41 this.key = key; 42 this.value = value; 43 } 44 45 public Object getKey() { 46 return key; 47 } 48 49 public Object getValue() { 50 return value; 51 } 52 53 public Object setValue(Object arg0) { 54 throw new UnsupportedOperationException (); 55 } 56 } 57 58 private class OrderedConstantSet extends ArrayList implements Set { 59 private class ImmutableIterator implements Iterator { 60 private final Iterator base; 61 62 ImmutableIterator(Iterator base) { 63 this.base = base; 64 } 65 66 public boolean hasNext() { 67 return base.hasNext(); 68 } 69 70 public Object next() { 71 return base.next(); 72 } 73 74 public void remove() { 75 throw new UnsupportedOperationException ("Immutable set"); 76 } 77 } 78 79 public void clear() { 80 throw new UnsupportedOperationException ("Immutable set"); 81 } 82 83 public Iterator iterator() { 84 Iterator base = super.iterator(); 85 return new ImmutableIterator(base); 86 } 87 } 88 89 private OrderedConstantSet entries; 90 91 private final OrderedConstantSet keys = new OrderedConstantSet(); 92 93 private OrderedConstantSet values; 94 95 public void clear() { 96 throw unsupported("clear"); 97 } 98 99 public Set entrySet() { 100 if (entries == null) { 101 entries = new OrderedConstantSet(); 102 for (int i = 0; i < keys.size(); i++) { 103 Object key = keys.get(i); 104 Object value = get(key); 105 entries.add(new DummyMapEntry(key, value)); 106 } 107 } 108 return entries; 109 } 110 111 public Set keySet() { 112 return keys; 113 } 114 115 public Object put(Object key, Object value) { 116 boolean exists = keys.contains(key); 118 if (!exists) { 119 keys.add(key); 120 } 121 return super.put(key, value); 122 } 123 124 public Object remove(Object key) { 125 throw unsupported("remove"); 126 } 127 128 public Collection values() { 129 if (values == null) { 130 values = new OrderedConstantSet(); 131 for (int i = 0; i < keys.size(); i++) { 132 Object element = keys.get(i); 133 values.add(this.get(element)); 134 } 135 } 136 return values; 137 } 138 139 private UnsupportedOperationException unsupported(String operation) { 140 return new UnsupportedOperationException (operation 141 + " not supported on a constant map"); 142 } 143 } 144 | Popular Tags |