From ff9ab0abd69f98fc67739c7e8f365006ac0cff25 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Tue, 15 Sep 2009 05:56:03 +0000 Subject: [PATCH] Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956. Also see the following revisions: ------------------------------------------------------------------------ r751865 | mbenson | 2009-03-09 15:06:27 -0700 (Mon, 09 Mar 2009) | 1 line javadoc + extension point ------------------------------------------------------------------------ r740150 | mbenson | 2009-02-02 15:24:00 -0800 (Mon, 02 Feb 2009) | 1 line make all [collections] maps implement IterableMap ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815076 13f79535-47bb-0310-9956-ffa450edef68 --- .../map/EntrySetToMapIteratorAdapter.java | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/java/org/apache/commons/collections/map/EntrySetToMapIteratorAdapter.java diff --git a/src/java/org/apache/commons/collections/map/EntrySetToMapIteratorAdapter.java b/src/java/org/apache/commons/collections/map/EntrySetToMapIteratorAdapter.java new file mode 100644 index 000000000..d3ac26d6c --- /dev/null +++ b/src/java/org/apache/commons/collections/map/EntrySetToMapIteratorAdapter.java @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.collections.map; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.collections.MapIterator; +import org.apache.commons.collections.ResettableIterator; + +/** + * Adapts a Map entrySet to the MapIterator interface. + * + * @since Commons Collections 5 + * @TODO fix version + * @version $Revision$ $Date$ + * + * @author Matt Benson + */ +public class EntrySetToMapIteratorAdapter implements MapIterator, ResettableIterator { + /** The adapted Map entry Set. */ + protected Set> entrySet; + + /** The resettable iterator in use. */ + protected transient Iterator> iterator; + + /** The currently positioned Map entry. */ + protected transient Map.Entry entry; + + /** + * Create a new EntrySetToMapIteratorAdapter. + */ + public EntrySetToMapIteratorAdapter(Set> entrySet) { + this.entrySet = entrySet; + reset(); + } + + /** + * {@inheritDoc} + */ + public K getKey() { + return current().getKey(); + } + + /** + * {@inheritDoc} + */ + public V getValue() { + return current().getValue(); + } + + /** + * {@inheritDoc} + */ + public V setValue(V value) { + return current().setValue(value); + }; + + /** + * {@inheritDoc} + */ + public boolean hasNext() { + return iterator.hasNext(); + } + + /** + * {@inheritDoc} + */ + public K next() { + entry = iterator.next(); + return getKey(); + } + + /** + * {@inheritDoc} + */ + public synchronized void reset() { + iterator = entrySet.iterator(); + } + + /** + * {@inheritDoc} + */ + public void remove() { + iterator.remove(); + entry = null; + } + + /** + * Get the currently active entry. + * @return Map.Entry + */ + protected synchronized Map.Entry current() { + if (entry == null) { + throw new IllegalStateException(); + } + return entry; + } +}