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.util.AbstractCollection;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.NoSuchElementException;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
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  @InterfaceAudience.Private
35  public class ConcatenatedLists<T> extends AbstractCollection<T> {
36    protected final ArrayList<List<T>> components = new ArrayList<List<T>>();
37    protected int size = 0;
38  
39    public void addAllSublists(List<? extends List<T>> items) {
40      for (List<T> list : items) {
41        addSublist(list);
42      }
43    }
44  
45    public void addSublist(List<T> items) {
46      if (!items.isEmpty()) {
47        this.components.add(items);
48        this.size += items.size();
49      }
50    }
51  
52    @Override
53    public int size() {
54      return this.size;
55    }
56  
57    @Override
58    public java.util.Iterator<T> iterator() {
59      return new Iterator();
60    }
61  
62    @edu.umd.cs.findbugs.annotations.SuppressWarnings(
63      value="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD",
64      justification="nextWasCalled is using by StripeStoreFileManager")
65    public class Iterator implements java.util.Iterator<T> {
66      protected int currentComponent = 0;
67      protected int indexWithinComponent = -1;
68      protected boolean nextWasCalled = false;
69  
70      @Override
71      public boolean hasNext() {
72        return (currentComponent + 1) < components.size()
73            || ((currentComponent + 1) == components.size()
74                && ((indexWithinComponent + 1) < components.get(currentComponent).size()));
75      }
76  
77      @Override
78      public T next() {
79        if (!components.isEmpty()) {
80          this.nextWasCalled = true;
81          List<T> src = components.get(currentComponent);
82          if (++indexWithinComponent < src.size()) return src.get(indexWithinComponent);
83          if (++currentComponent < components.size()) {
84            indexWithinComponent = 0;
85            src = components.get(currentComponent);
86            assert src.size() > 0;
87            return src.get(indexWithinComponent);
88          }
89        }
90        this.nextWasCalled = false;
91        throw new NoSuchElementException();
92      }
93  
94      @Override
95      public void remove() {
96        throw new UnsupportedOperationException();
97      }
98    }
99  }