View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import java.lang.reflect.Array;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.NoSuchElementException;
27  
28  /**
29   * A collection class that contains multiple sub-lists, which allows us to not copy lists.
30   * This class does not support modification. The derived classes that add modifications are
31   * not thread-safe.
32   * NOTE: Doesn't implement list as it is not necessary for current usage, feel free to add.
33   */
34  public class ConcatenatedLists<T> implements Collection<T> {
35    protected final ArrayList<List<T>> components = new ArrayList<List<T>>();
36    protected int size = 0;
37  
38    public void addAllSublists(List<? extends List<T>> items) {
39      for (List<T> list : items) {
40        addSublist(list);
41      }
42    }
43  
44    public void addSublist(List<T> items) {
45      if (!items.isEmpty()) {
46        this.components.add(items);
47        this.size += items.size();
48      }
49    }
50  
51    @Override
52    public int size() {
53      return this.size;
54    }
55  
56    @Override
57    public boolean isEmpty() {
58      return this.size == 0;
59    }
60  
61    @Override
62    public boolean contains(Object o) {
63      for (List<T> component : this.components) {
64        if (component.contains(o)) return true;
65      }
66      return false;
67    }
68  
69    @Override
70    public boolean containsAll(Collection<?> c) {
71      for (Object o : c) {
72        if (!contains(o)) return false;
73      }
74      return true;
75    }
76  
77    @Override
78    public Object[] toArray() {
79      return toArray((Object[])Array.newInstance(Object.class, this.size));
80    }
81  
82    @Override
83    @SuppressWarnings("unchecked")
84    public <U> U[] toArray(U[] a) {
85      U[] result = (a.length == this.size()) ? a
86          : (U[])Array.newInstance(a.getClass().getComponentType(), this.size);
87      int i = 0;
88      for (List<T> component : this.components) {
89        for (T t : component) {
90          result[i] = (U)t;
91          ++i;
92        }
93      }
94      return result;
95    }
96  
97    @Override
98    public boolean add(T e) {
99      throw new UnsupportedOperationException();
100   }
101 
102   @Override
103   public boolean remove(Object o) {
104     throw new UnsupportedOperationException();
105   }
106 
107   @Override
108   public boolean addAll(Collection<? extends T> c) {
109     throw new UnsupportedOperationException();
110   }
111 
112   @Override
113   public boolean removeAll(Collection<?> c) {
114     throw new UnsupportedOperationException();
115   }
116 
117   @Override
118   public boolean retainAll(Collection<?> c) {
119     throw new UnsupportedOperationException();
120   }
121 
122   @Override
123   public void clear() {
124     throw new UnsupportedOperationException();
125   }
126 
127   @Override
128   public java.util.Iterator<T> iterator() {
129     return new Iterator();
130   }
131 
132   @edu.umd.cs.findbugs.annotations.SuppressWarnings(
133     value="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD",
134     justification="nextWasCalled is using by StripeStoreFileManager")
135   public class Iterator implements java.util.Iterator<T> {
136     protected int currentComponent = 0;
137     protected int indexWithinComponent = -1;
138     protected boolean nextWasCalled = false;
139 
140     @Override
141     public boolean hasNext() {
142       return (currentComponent + 1) < components.size()
143           || ((currentComponent + 1) == components.size()
144               && ((indexWithinComponent + 1) < components.get(currentComponent).size()));
145     }
146 
147     @Override
148     public T next() {
149       if (!components.isEmpty()) {
150         this.nextWasCalled = true;
151         List<T> src = components.get(currentComponent);
152         if (++indexWithinComponent < src.size()) return src.get(indexWithinComponent);
153         if (++currentComponent < components.size()) {
154           indexWithinComponent = 0;
155           src = components.get(currentComponent);
156           assert src.size() > 0;
157           return src.get(indexWithinComponent);
158         }
159       }
160       this.nextWasCalled = false;
161       throw new NoSuchElementException();
162     }
163 
164     @Override
165     public void remove() {
166       throw new UnsupportedOperationException();
167     }
168   }
169 }