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.List;
25  import java.util.NoSuchElementException;
26  
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  
29  /**
30   * A collection class that contains multiple sub-lists, which allows us to not copy lists.
31   * This class does not support modification. The derived classes that add modifications are
32   * not thread-safe.
33   * NOTE: Doesn't implement list as it is not necessary for current usage, feel free to add.
34   */
35  @InterfaceAudience.Private
36  public class ConcatenatedLists<T> implements Collection<T> {
37    protected final ArrayList<List<T>> components = new ArrayList<List<T>>();
38    protected int size = 0;
39  
40    public void addAllSublists(List<? extends List<T>> items) {
41      for (List<T> list : items) {
42        addSublist(list);
43      }
44    }
45  
46    public void addSublist(List<T> items) {
47      if (!items.isEmpty()) {
48        this.components.add(items);
49        this.size += items.size();
50      }
51    }
52  
53    @Override
54    public int size() {
55      return this.size;
56    }
57  
58    @Override
59    public boolean isEmpty() {
60      return this.size == 0;
61    }
62  
63    @Override
64    public boolean contains(Object o) {
65      for (List<T> component : this.components) {
66        if (component.contains(o)) return true;
67      }
68      return false;
69    }
70  
71    @Override
72    public boolean containsAll(Collection<?> c) {
73      for (Object o : c) {
74        if (!contains(o)) return false;
75      }
76      return true;
77    }
78  
79    @Override
80    public Object[] toArray() {
81      return toArray((Object[])Array.newInstance(Object.class, this.size));
82    }
83  
84    @Override
85    @SuppressWarnings("unchecked")
86    public <U> U[] toArray(U[] a) {
87      U[] result = (a.length == this.size()) ? a
88          : (U[])Array.newInstance(a.getClass().getComponentType(), this.size);
89      int i = 0;
90      for (List<T> component : this.components) {
91        for (T t : component) {
92          result[i] = (U)t;
93          ++i;
94        }
95      }
96      return result;
97    }
98  
99    @Override
100   public boolean add(T e) {
101     throw new UnsupportedOperationException();
102   }
103 
104   @Override
105   public boolean remove(Object o) {
106     throw new UnsupportedOperationException();
107   }
108 
109   @Override
110   public boolean addAll(Collection<? extends T> c) {
111     throw new UnsupportedOperationException();
112   }
113 
114   @Override
115   public boolean removeAll(Collection<?> c) {
116     throw new UnsupportedOperationException();
117   }
118 
119   @Override
120   public boolean retainAll(Collection<?> c) {
121     throw new UnsupportedOperationException();
122   }
123 
124   @Override
125   public void clear() {
126     throw new UnsupportedOperationException();
127   }
128 
129   @Override
130   public java.util.Iterator<T> iterator() {
131     return new Iterator();
132   }
133 
134   @InterfaceAudience.Private
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 }