source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/util/CyclicIteration.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.2 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.util;
19
20import java.util.Iterator;
21import java.util.Map;
22import java.util.NavigableMap;
23import java.util.NoSuchElementException;
24
25/** Provide an cyclic {@link Iterator} for a {@link NavigableMap}.
26 * The {@link Iterator} navigates the entries of the map
27 * according to the map's ordering.
28 * If the {@link Iterator} hits the last entry of the map,
29 * it will then continue from the first entry.
30 */
31public class CyclicIteration<K, V> implements Iterable<Map.Entry<K, V>> {
32  private final NavigableMap<K, V> navigablemap;
33  private final NavigableMap<K, V> tailmap;
34
35  /** Construct an {@link Iterable} object,
36   * so that an {@link Iterator} can be created 
37   * for iterating the given {@link NavigableMap}.
38   * The iteration begins from the starting key exclusively.
39   */
40  public CyclicIteration(NavigableMap<K, V> navigablemap, K startingkey) {
41    if (navigablemap == null || navigablemap.isEmpty()) {
42      this.navigablemap = null;
43      this.tailmap = null;
44    }
45    else {
46      this.navigablemap = navigablemap;
47      this.tailmap = navigablemap.tailMap(startingkey, false); 
48    }
49  }
50
51  /** {@inheritDoc} */
52  public Iterator<Map.Entry<K, V>> iterator() {
53    return new CyclicIterator();
54  }
55
56  /** An {@link Iterator} for {@link CyclicIteration}. */
57  private class CyclicIterator implements Iterator<Map.Entry<K, V>> {
58    private boolean hasnext;
59    private Iterator<Map.Entry<K, V>> i;
60    /** The first entry to begin. */
61    private final Map.Entry<K, V> first;
62    /** The next entry. */
63    private Map.Entry<K, V> next;
64   
65    private CyclicIterator() {
66      hasnext = navigablemap != null;
67      if (hasnext) {
68        i = tailmap.entrySet().iterator();
69        first = nextEntry();
70        next = first;
71      }
72      else {
73        i = null;
74        first = null;
75        next = null;
76      }
77    }
78
79    private Map.Entry<K, V> nextEntry() {
80      if (!i.hasNext()) {
81        i = navigablemap.entrySet().iterator();
82      }
83      return i.next();
84    }
85
86    /** {@inheritDoc} */
87    public boolean hasNext() {
88      return hasnext;
89    }
90
91    /** {@inheritDoc} */
92    public Map.Entry<K, V> next() {
93      if (!hasnext) {
94        throw new NoSuchElementException();
95      }
96
97      final Map.Entry<K, V> curr = next;
98      next = nextEntry();
99      hasnext = !next.equals(first);
100      return curr;
101    }
102
103    /** Not supported */
104    public void remove() {
105      throw new UnsupportedOperationException("Not supported");
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.