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  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  
30  /**
31   * A collection class that contains multiple sub-lists, which allows us to not copy lists.
32   * This class does not support modification. The derived classes that add modifications are
33   * not thread-safe.
34   * NOTE: Doesn't implement list as it is not necessary for current usage, feel free to add.
35   */
36  @InterfaceAudience.Private
37  public class ConcatenatedLists<T> implements Collection<T> {
38    protected final ArrayList<List<T>> components = new ArrayList<List<T>>();
39    protected int size = 0;
40  
41    public void addAllSublists(List<? extends List<T>> items) {
42      for (List<T> list : items) {
43        addSublist(list);
44      }
45    }
46  
47    public void addSublist(List<T> items) {
48      if (!items.isEmpty()) {
49        this.components.add(items);
50        this.size += items.size();
51      }
52    }
53  
54    @Override
55    public int size() {
56      return this.size;
57    }
58  
59    @Override
60    public boolean isEmpty() {
61      return this.size == 0;
62    }
63  
64    @Override
65    public boolean contains(Object o) {
66      for (List<T> component : this.components) {
67        if (component.contains(o)) return true;
68      }
69      return false;
70    }
71  
72    @Override
73    public boolean containsAll(Collection<?> c) {
74      for (Object o : c) {
75        if (!contains(o)) return false;
76      }
77      return true;
78    }
79  
80    @Override
81    public Object[] toArray() {
82      return toArray((Object[])Array.newInstance(Object.class, this.size));
83    }
84  
85    @Override
86    @SuppressWarnings("unchecked")
87    public <U> U[] toArray(U[] a) {
88      U[] result = (a.length == this.size()) ? a
89          : (U[])Array.newInstance(a.getClass().getComponentType(), this.size);
90      int i = 0;
91      for (List<T> component : this.components) {
92        for (T t : component) {
93          result[i] = (U)t;
94          ++i;
95        }
96      }
97      return result;
98    }
99  
100   @Override
101   public boolean add(T e) {
102     throw new UnsupportedOperationException();
103   }
104 
105   @Override
106   public boolean remove(Object o) {
107     throw new UnsupportedOperationException();
108   }
109 
110   @Override
111   public boolean addAll(Collection<? extends T> c) {
112     throw new UnsupportedOperationException();
113   }
114 
115   @Override
116   public boolean removeAll(Collection<?> c) {
117     throw new UnsupportedOperationException();
118   }
119 
120   @Override
121   public boolean retainAll(Collection<?> c) {
122     throw new UnsupportedOperationException();
123   }
124 
125   @Override
126   public void clear() {
127     throw new UnsupportedOperationException();
128   }
129 
130   @Override
131   public java.util.Iterator<T> iterator() {
132     return new Iterator();
133   }
134 
135   @edu.umd.cs.findbugs.annotations.SuppressWarnings(
136     value="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD",
137     justification="nextWasCalled is using by StripeStoreFileManager")
138   public class Iterator implements java.util.Iterator<T> {
139     protected int currentComponent = 0;
140     protected int indexWithinComponent = -1;
141     protected boolean nextWasCalled = false;
142 
143     @Override
144     public boolean hasNext() {
145       return (currentComponent + 1) < components.size()
146           || ((currentComponent + 1) == components.size()
147               && ((indexWithinComponent + 1) < components.get(currentComponent).size()));
148     }
149 
150     @Override
151     public T next() {
152       if (!components.isEmpty()) {
153         this.nextWasCalled = true;
154         List<T> src = components.get(currentComponent);
155         if (++indexWithinComponent < src.size()) return src.get(indexWithinComponent);
156         if (++currentComponent < components.size()) {
157           indexWithinComponent = 0;
158           src = components.get(currentComponent);
159           assert src.size() > 0;
160           return src.get(indexWithinComponent);
161         }
162       }
163       this.nextWasCalled = false;
164       throw new NoSuchElementException();
165     }
166 
167     @Override
168     public void remove() {
169       throw new UnsupportedOperationException();
170     }
171   }
172 }