Namespaces { public static Map<String, String> wrap(final Set<Namespace> namespaces) { return new Map<String, String>() { @Override public void clear() { namespaces.clear(); } @Override public boolean containsKey(Object nextKey) { if (nextKey instanceof String) { for (Namespace nextNamespace : namespaces) { if (nextNamespace.getPrefix().equals(nextKey)) { return true; } } } return false; } @Override public boolean containsValue(Object nextValue) { if (nextValue instanceof String) { for (Namespace nextNamespace : namespaces) { if (nextNamespace.getName().equals(nextValue)) { return true; } } } return false; } @Override public Set<java.util.Map.Entry<String, String>> entrySet() { Set<java.util.Map.Entry<String, String>> result = new LinkedHashSet<>(); for (Namespace nextNamespace : namespaces) { AbstractMap.SimpleImmutableEntry<String, String> nextEntry = new SimpleImmutableEntry<>( nextNamespace.getPrefix(), nextNamespace.getName()); result.add(nextEntry); } return Collections.unmodifiableSet(result); } @Override public String get(Object nextKey) { if (nextKey instanceof String) { for (Namespace nextNamespace : namespaces) { if (nextNamespace.getPrefix().equals(nextKey)) { return nextNamespace.getName(); } } } return null; } @Override public boolean isEmpty() { return namespaces.isEmpty(); } @Override public Set<String> keySet() { Set<String> result = new LinkedHashSet<>(); for (Namespace nextNamespace : namespaces) { result.add(nextNamespace.getPrefix()); } return result; } @Override public String put(String nextKey, String nextValue) { String result = null; for (Namespace nextNamespace : new LinkedHashSet<>(namespaces)) { if (nextNamespace.getPrefix().equals(nextKey)) { result = nextNamespace.getName(); namespaces.remove(nextNamespace); } } namespaces.add(new SimpleNamespace(nextKey, nextValue)); return result; } @Override public void putAll(Map<? extends String, ? extends String> nextSet) { for (Map.Entry<? extends String, ? extends String> nextEntry : nextSet.entrySet()) { put(nextEntry.getKey(), nextEntry.getValue()); } } @Override public String remove(Object nextKey) { String result = null; for (Namespace nextNamespace : new LinkedHashSet<>(namespaces)) { if (nextNamespace.getPrefix().equals(nextKey)) { result = nextNamespace.getName(); namespaces.remove(nextNamespace); } } return result; } @Override public int size() { return namespaces.size(); } @Override public Collection<String> values() { List<String> result = new ArrayList<>(); for (Namespace nextNamespace : namespaces) { result.add(nextNamespace.getName()); } return result; } }; } private  Namespaces(); static Map<String, String> asMap(Set<Namespace> namespaces); static Map<String, String> wrap(final Set<Namespace> namespaces);  }
@Test public final void testWrapClear() throws Exception { Set<Namespace> testSet = new LinkedHashSet<>(); Map<String, String> testMap = Namespaces.wrap(testSet); testMap.clear(); testSet.add(new SimpleNamespace(testPrefix1, testName1)); assertFalse(testMap.isEmpty()); assertEquals(1, testMap.size()); testMap.clear(); assertTrue(testMap.isEmpty()); assertEquals(0, testMap.size()); }
@Test public final void testWrapContainsKey() throws Exception { Set<Namespace> testSet = new LinkedHashSet<>(); Map<String, String> testMap = Namespaces.wrap(testSet); assertFalse(testMap.containsKey(testPrefix1)); testSet.add(new SimpleNamespace(testPrefix1, testName1)); assertTrue(testMap.containsKey(testPrefix1)); testSet.clear(); assertFalse(testMap.containsKey(testPrefix1)); }
@Test public final void testWrapContainsValue() throws Exception { Set<Namespace> testSet = new LinkedHashSet<>(); Map<String, String> testMap = Namespaces.wrap(testSet); assertFalse(testMap.containsValue(testName1)); testSet.add(new SimpleNamespace(testPrefix1, testName1)); assertTrue(testMap.containsValue(testName1)); testSet.clear(); assertFalse(testMap.containsValue(testName1)); }
@Test public final void testWrapEntrySet() throws Exception { Set<Namespace> testSet = new LinkedHashSet<>(); Map<String, String> testMap = Namespaces.wrap(testSet); Set<Entry<String, String>> entrySet1 = testMap.entrySet(); assertNotNull(entrySet1); assertTrue(entrySet1.isEmpty()); testSet.add(new SimpleNamespace(testPrefix1, testName1)); Set<Entry<String, String>> entrySet2 = testMap.entrySet(); assertNotNull(entrySet2); assertFalse(entrySet2.isEmpty()); assertEquals(1, entrySet2.size()); Entry<String, String> nextEntry = entrySet2.iterator().next(); assertEquals(testPrefix1, nextEntry.getKey()); assertEquals(testName1, nextEntry.getValue()); testSet.clear(); Set<Entry<String, String>> entrySet3 = testMap.entrySet(); assertNotNull(entrySet3); assertTrue(entrySet3.isEmpty()); }
@Test public final void testWrapGet() throws Exception { Set<Namespace> testSet = new LinkedHashSet<>(); Map<String, String> testMap = Namespaces.wrap(testSet); assertNull(testMap.get(testPrefix1)); testSet.add(new SimpleNamespace(testPrefix1, testName1)); assertEquals(testName1, testMap.get(testPrefix1)); testSet.clear(); assertNull(testMap.get(testPrefix1)); }
@Test public final void testWrapIsEmpty() throws Exception { Set<Namespace> testSet = new LinkedHashSet<>(); Map<String, String> testMap = Namespaces.wrap(testSet); assertTrue(testMap.isEmpty()); testSet.add(new SimpleNamespace(testPrefix1, testName1)); assertFalse(testMap.isEmpty()); testSet.clear(); assertTrue(testMap.isEmpty()); }
@Test public final void testWrapKeySet() throws Exception { Set<Namespace> testSet = new LinkedHashSet<>(); Map<String, String> testMap = Namespaces.wrap(testSet); Set<String> keySet1 = testMap.keySet(); assertNotNull(keySet1); assertTrue(keySet1.isEmpty()); testSet.add(new SimpleNamespace(testPrefix1, testName1)); Set<String> keySet2 = testMap.keySet(); assertNotNull(keySet2); assertFalse(keySet2.isEmpty()); assertEquals(1, keySet2.size()); String nextKey = keySet2.iterator().next(); assertEquals(testPrefix1, nextKey); testSet.clear(); Set<String> keySet3 = testMap.keySet(); assertNotNull(keySet3); assertTrue(keySet3.isEmpty()); }
@Test public final void testWrapPut() throws Exception { Set<Namespace> testSet = new LinkedHashSet<>(); Map<String, String> testMap = Namespaces.wrap(testSet); String put1 = testMap.put(testPrefix1, testName1); assertNull("Should have returned null from put on an empty backing set", put1); assertEquals(1, testSet.size()); assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1))); assertTrue(testMap.containsKey(testPrefix1)); assertTrue(testMap.containsValue(testName1)); String put2 = testMap.put(testPrefix1, testName2); assertEquals(put2, testName1); assertEquals(1, testSet.size()); assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName2))); assertTrue(testMap.containsKey(testPrefix1)); assertFalse(testMap.containsValue(testName1)); assertTrue(testMap.containsValue(testName2)); testSet.clear(); assertTrue(testMap.isEmpty()); assertEquals(0, testMap.size()); assertFalse(testMap.containsKey(testPrefix1)); assertFalse(testMap.containsValue(testName1)); assertFalse(testMap.containsValue(testName2)); String put3 = testMap.put(testPrefix1, testName1); assertNull("Should have returned null from put on an empty backing set", put3); assertEquals(1, testSet.size()); assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1))); assertTrue(testMap.containsKey(testPrefix1)); assertTrue(testMap.containsValue(testName1)); }
@Test public final void testWrapPutAll() throws Exception { Set<Namespace> testSet = new LinkedHashSet<>(); Map<String, String> testMap = Namespaces.wrap(testSet); Map<String, String> testPutMap = new LinkedHashMap<>(); testMap.putAll(testPutMap); assertTrue(testMap.isEmpty()); assertEquals(0, testMap.size()); assertTrue(testSet.isEmpty()); assertEquals(0, testSet.size()); testPutMap.put(testPrefix1, testName1); testPutMap.put(testPrefix2, testName2); testMap.putAll(testPutMap); assertFalse(testMap.isEmpty()); assertEquals(2, testMap.size()); assertFalse(testSet.isEmpty()); assertEquals(2, testSet.size()); assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1))); assertTrue(testSet.contains(new SimpleNamespace(testPrefix2, testName2))); assertTrue(testMap.containsKey(testPrefix1)); assertTrue(testMap.containsValue(testName1)); assertTrue(testMap.containsKey(testPrefix2)); assertTrue(testMap.containsValue(testName2)); testSet.clear(); assertTrue(testMap.isEmpty()); assertEquals(0, testMap.size()); assertTrue(testSet.isEmpty()); assertEquals(0, testSet.size()); assertFalse(testMap.containsKey(testPrefix1)); assertFalse(testMap.containsValue(testName1)); assertFalse(testMap.containsKey(testPrefix2)); assertFalse(testMap.containsValue(testName2)); testMap.putAll(testPutMap); assertFalse(testMap.isEmpty()); assertEquals(2, testMap.size()); assertFalse(testSet.isEmpty()); assertEquals(2, testSet.size()); assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1))); assertTrue(testSet.contains(new SimpleNamespace(testPrefix2, testName2))); assertTrue(testMap.containsKey(testPrefix1)); assertTrue(testMap.containsValue(testName1)); assertTrue(testMap.containsKey(testPrefix2)); assertTrue(testMap.containsValue(testName2)); }
@Test public final void testWrapRemove() throws Exception { Set<Namespace> testSet = new LinkedHashSet<>(); Map<String, String> testMap = Namespaces.wrap(testSet); assertTrue(testMap.isEmpty()); assertEquals(0, testMap.size()); assertTrue(testSet.isEmpty()); assertEquals(0, testSet.size()); assertFalse(testMap.containsKey(testPrefix1)); assertFalse(testMap.containsValue(testName1)); testSet.add(new SimpleNamespace(testPrefix1, testName1)); assertFalse(testMap.isEmpty()); assertEquals(1, testMap.size()); assertFalse(testSet.isEmpty()); assertEquals(1, testSet.size()); assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1))); assertTrue(testMap.containsKey(testPrefix1)); assertTrue(testMap.containsValue(testName1)); testSet.remove(new SimpleNamespace(testPrefix1, testName1)); assertTrue(testMap.isEmpty()); assertEquals(0, testMap.size()); assertTrue(testSet.isEmpty()); assertEquals(0, testSet.size()); assertFalse(testMap.containsKey(testPrefix1)); assertFalse(testMap.containsValue(testName1)); testSet.clear(); testSet.add(new SimpleNamespace(testPrefix1, testName1)); assertFalse(testMap.isEmpty()); assertEquals(1, testMap.size()); assertFalse(testSet.isEmpty()); assertEquals(1, testSet.size()); assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1))); assertTrue(testMap.containsKey(testPrefix1)); assertTrue(testMap.containsValue(testName1)); testSet.remove(new SimpleNamespace(testPrefix1, testName1)); assertTrue(testMap.isEmpty()); assertEquals(0, testMap.size()); assertTrue(testSet.isEmpty()); assertEquals(0, testSet.size()); assertFalse(testMap.containsKey(testPrefix1)); assertFalse(testMap.containsValue(testName1)); }
@Test public final void testWrapValues() throws Exception { Set<Namespace> testSet = new LinkedHashSet<>(); Map<String, String> testMap = Namespaces.wrap(testSet); Collection<String> values1 = testMap.values(); assertNotNull(values1); assertTrue(values1.isEmpty()); testSet.add(new SimpleNamespace(testPrefix1, testName1)); Collection<String> values2 = testMap.values(); assertNotNull(values2); assertFalse(values2.isEmpty()); assertEquals(1, values2.size()); String nextValue = values2.iterator().next(); assertEquals(testName1, nextValue); testSet.clear(); Collection<String> values3 = testMap.values(); assertNotNull(values3); assertTrue(values3.isEmpty()); }