This project has retired. For details please refer to its Attic page.
PairList xref
View Javadoc

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   */
18  
19  package org.apache.giraph.utils;
20  
21  import com.google.common.collect.Lists;
22  
23  import java.util.List;
24  
25  /**
26   * Collection to keep pairs in, without creating a wrapper object around
27   * each pair of objects.
28   *
29   * @param <U> Type of the first element in a pair
30   * @param <V> Type of the second element in a pair
31   */
32  public class PairList<U, V> {
33    /** List to keep first elements of pairs in */
34    protected List<U> firstList;
35    /** List to keep second elements of pairs in */
36    protected List<V> secondList;
37  
38    /**
39     * Constructor
40     */
41    public PairList() {
42    }
43  
44    /**
45     * Initialize the inner state. Must be called before {@code add()} is
46     * called.
47     */
48    public void initialize() {
49      firstList = Lists.newArrayList();
50      secondList = Lists.newArrayList();
51    }
52  
53  
54    /**
55     * Initialize the inner state, with a known size. Must be called before
56     * {@code add()} is called.
57     *
58     * @param size Number of pairs which will be added to the list
59     */
60    public void initialize(int size) {
61      firstList = Lists.newArrayListWithCapacity(size);
62      secondList = Lists.newArrayListWithCapacity(size);
63    }
64  
65    /**
66     * Add a pair to the collection.
67     *
68     * @param first First element of the pair
69     * @param second Second element of the pair
70     */
71    public void add(U first, V second) {
72      firstList.add(first);
73      secondList.add(second);
74    }
75  
76    /**
77     * Get number of pairs in this list.
78     *
79     * @return Number of pairs in the list
80     */
81    public int getSize() {
82      return firstList.size();
83    }
84  
85    /**
86     * Check if the list is empty.
87     *
88     * @return True iff there are no pairs in the list
89     */
90    public boolean isEmpty() {
91      return getSize() == 0;
92    }
93  
94    /**
95     * Get iterator through elements of this object.
96     *
97     * @return {@link Iterator} iterator
98     */
99    public Iterator getIterator() {
100     return new Iterator();
101   }
102 
103   /**
104    * Special iterator class which we'll use to iterate through elements of
105    * {@link PairList}, without having to create new object as wrapper for
106    * each pair.
107    *
108    * Protocol is somewhat similar to the protocol of {@link java.util.Iterator}
109    * only here next() doesn't return the next object, it just moves along in
110    * the collection. Values related to current pair can be retrieved by calling
111    * getCurrentFirst() and getCurrentSecond() methods.
112    *
113    * Not thread-safe.
114    */
115   public class Iterator {
116     /** Current position of the iterator */
117     private int position = -1;
118 
119     /**
120      * Returns true if the iteration has more elements.
121      *
122      * @return True if the iteration has more elements.
123      */
124     public boolean hasNext() {
125       return position < getSize() - 1;
126     }
127 
128     /**
129      * Moves to the next element in the iteration.
130      */
131     public void next() {
132       position++;
133     }
134 
135     /**
136      * Get first element of the current pair of the iteration.
137      *
138      * @return First element of the current pair of the iteration
139      */
140     public U getCurrentFirst() {
141       return firstList.get(position);
142     }
143 
144     /**
145      * Get second element of the current pair of the iteration.
146      *
147      * @return Second element of the current pair of the iteration
148      */
149     public V getCurrentSecond() {
150       return secondList.get(position);
151     }
152   }
153 }