@Test public void getKey() { String ONE = "one"; String TWO = "two"; String THREE = "three"; Options<Integer> opt = new Options<Integer>(); opt.put(1, ONE); opt.put(2, TWO); opt.put(3, THREE); assertEquals("opt.getKey(THREE)", 3, (int)opt.getKey(THREE)); assertEquals("opt.getKey(TWO)", 2, (int)opt.getKey(TWO)); assertEquals("opt.getKey(ONE)", 1, (int)opt.getKey(ONE)); try { opt.getKey("foo"); fail("expected NoSuchElementException"); } catch (NoSuchElementException ex) { } }
public K getKey(String value) { if (value == null) { throw new IllegalArgumentException("value==null"); } for (K key : map.keySet()) { String val = map.get(key); if (value.equals(val)) { return key; } } throw new NoSuchElementException("value='" + value + "' not found"); }
Options { public K getKey(String value) { if (value == null) { throw new IllegalArgumentException("value==null"); } for (K key : map.keySet()) { String val = map.get(key); if (value.equals(val)) { return key; } } throw new NoSuchElementException("value='" + value + "' not found"); } }
Options { public K getKey(String value) { if (value == null) { throw new IllegalArgumentException("value==null"); } for (K key : map.keySet()) { String val = map.get(key); if (value.equals(val)) { return key; } } throw new NoSuchElementException("value='" + value + "' not found"); }  }
Options { public K getKey(String value) { if (value == null) { throw new IllegalArgumentException("value==null"); } for (K key : map.keySet()) { String val = map.get(key); if (value.equals(val)) { return key; } } throw new NoSuchElementException("value='" + value + "' not found"); }  Map<K, String> toMap(); void clear(); boolean containsKey(Object key); boolean containsValue(Object value); String get(Object key); K getKey(String value); boolean isEmpty(); Set<K> keySet(); String put(K key, String value); void putAll(Options other); void putAll(Map<K, String> aMap); String remove(Object key); Set<String> removeAll(Set<K> keys); int size(); Set<String> values(); String[] getValues(); int indexOf(String value); String getValue(int index); void addOptionsListener(OptionsListener l); void removeOptionsListener(OptionsListener l); static Options<T> create(T... values); }
Options { public K getKey(String value) { if (value == null) { throw new IllegalArgumentException("value==null"); } for (K key : map.keySet()) { String val = map.get(key); if (value.equals(val)) { return key; } } throw new NoSuchElementException("value='" + value + "' not found"); }  Map<K, String> toMap(); void clear(); boolean containsKey(Object key); boolean containsValue(Object value); String get(Object key); K getKey(String value); boolean isEmpty(); Set<K> keySet(); String put(K key, String value); void putAll(Options other); void putAll(Map<K, String> aMap); String remove(Object key); Set<String> removeAll(Set<K> keys); int size(); Set<String> values(); String[] getValues(); int indexOf(String value); String getValue(int index); void addOptionsListener(OptionsListener l); void removeOptionsListener(OptionsListener l); static Options<T> create(T... values);  }