@Test public void testIterator() { Counter g1k1 = new Counter("group1", "key1", 10); Counter g1k2 = new Counter("group1", "key2", 20); Counter g2k1 = new Counter("group2", "key1", 100); Counter g3k1 = new Counter("group3", "key1", 200); CounterMap base = new CounterMap(); base.add(g1k1); base.add(g1k2); base.add(g2k1); base.add(g3k1); CounterMap copy = new CounterMap(); copy.addAll(base); Counter c = copy.getCounter("group1", "key1"); assertNotNull(c); assertEquals(g1k1, c); c = copy.getCounter("group1", "key2"); assertNotNull(c); assertEquals(g1k2, c); c = copy.getCounter("group2", "key1"); assertNotNull(c); assertEquals(g2k1, c); c = copy.getCounter("group3", "key1"); assertNotNull(c); assertEquals(g3k1, c); }
@Override public Iterator<Counter> iterator() { return new Iterator<Counter>() { private Iterator<Map.Entry<String,Map<String,Counter>>> groupIter = internalMap.entrySet().iterator(); private Iterator<Map.Entry<String,Counter>> currentGroupIter = null; @Override public boolean hasNext() { if ((currentGroupIter == null || !currentGroupIter.hasNext()) && groupIter.hasNext()) { currentGroupIter = groupIter.next().getValue().entrySet().iterator(); } return currentGroupIter != null && currentGroupIter.hasNext(); } @Override public Counter next() { if (!hasNext()) { throw new NoSuchElementException("No more elements in iterator"); } return currentGroupIter.next().getValue(); } @Override public void remove() { throw new UnsupportedOperationException("remove() is not supported by CounterMap"); } }; }
CounterMap implements Iterable<Counter> { @Override public Iterator<Counter> iterator() { return new Iterator<Counter>() { private Iterator<Map.Entry<String,Map<String,Counter>>> groupIter = internalMap.entrySet().iterator(); private Iterator<Map.Entry<String,Counter>> currentGroupIter = null; @Override public boolean hasNext() { if ((currentGroupIter == null || !currentGroupIter.hasNext()) && groupIter.hasNext()) { currentGroupIter = groupIter.next().getValue().entrySet().iterator(); } return currentGroupIter != null && currentGroupIter.hasNext(); } @Override public Counter next() { if (!hasNext()) { throw new NoSuchElementException("No more elements in iterator"); } return currentGroupIter.next().getValue(); } @Override public void remove() { throw new UnsupportedOperationException("remove() is not supported by CounterMap"); } }; } }
CounterMap implements Iterable<Counter> { @Override public Iterator<Counter> iterator() { return new Iterator<Counter>() { private Iterator<Map.Entry<String,Map<String,Counter>>> groupIter = internalMap.entrySet().iterator(); private Iterator<Map.Entry<String,Counter>> currentGroupIter = null; @Override public boolean hasNext() { if ((currentGroupIter == null || !currentGroupIter.hasNext()) && groupIter.hasNext()) { currentGroupIter = groupIter.next().getValue().entrySet().iterator(); } return currentGroupIter != null && currentGroupIter.hasNext(); } @Override public Counter next() { if (!hasNext()) { throw new NoSuchElementException("No more elements in iterator"); } return currentGroupIter.next().getValue(); } @Override public void remove() { throw new UnsupportedOperationException("remove() is not supported by CounterMap"); } }; }  }
CounterMap implements Iterable<Counter> { @Override public Iterator<Counter> iterator() { return new Iterator<Counter>() { private Iterator<Map.Entry<String,Map<String,Counter>>> groupIter = internalMap.entrySet().iterator(); private Iterator<Map.Entry<String,Counter>> currentGroupIter = null; @Override public boolean hasNext() { if ((currentGroupIter == null || !currentGroupIter.hasNext()) && groupIter.hasNext()) { currentGroupIter = groupIter.next().getValue().entrySet().iterator(); } return currentGroupIter != null && currentGroupIter.hasNext(); } @Override public Counter next() { if (!hasNext()) { throw new NoSuchElementException("No more elements in iterator"); } return currentGroupIter.next().getValue(); } @Override public void remove() { throw new UnsupportedOperationException("remove() is not supported by CounterMap"); } }; }  Set<String> getGroups(); Map<String,Counter> getGroup(String group); Counter getCounter(String group, String name); Counter add(Counter counter); void addAll(Iterable<Counter> counters); @Override Iterator<Counter> iterator(); }
CounterMap implements Iterable<Counter> { @Override public Iterator<Counter> iterator() { return new Iterator<Counter>() { private Iterator<Map.Entry<String,Map<String,Counter>>> groupIter = internalMap.entrySet().iterator(); private Iterator<Map.Entry<String,Counter>> currentGroupIter = null; @Override public boolean hasNext() { if ((currentGroupIter == null || !currentGroupIter.hasNext()) && groupIter.hasNext()) { currentGroupIter = groupIter.next().getValue().entrySet().iterator(); } return currentGroupIter != null && currentGroupIter.hasNext(); } @Override public Counter next() { if (!hasNext()) { throw new NoSuchElementException("No more elements in iterator"); } return currentGroupIter.next().getValue(); } @Override public void remove() { throw new UnsupportedOperationException("remove() is not supported by CounterMap"); } }; }  Set<String> getGroups(); Map<String,Counter> getGroup(String group); Counter getCounter(String group, String name); Counter add(Counter counter); void addAll(Iterable<Counter> counters); @Override Iterator<Counter> iterator();  }