001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.util; 020 021import java.util.AbstractCollection; 022import java.util.ArrayList; 023import java.util.List; 024import java.util.NoSuchElementException; 025 026import org.apache.yetus.audience.InterfaceAudience; 027 028/** 029 * A collection class that contains multiple sub-lists, which allows us to not copy lists. 030 * This class does not support modification. The derived classes that add modifications are 031 * not thread-safe. 032 * NOTE: Doesn't implement list as it is not necessary for current usage, feel free to add. 033 */ 034@InterfaceAudience.Private 035public class ConcatenatedLists<T> extends AbstractCollection<T> { 036 protected final ArrayList<List<T>> components = new ArrayList<>(); 037 protected int size = 0; 038 039 public void addAllSublists(List<? extends List<T>> items) { 040 for (List<T> list : items) { 041 addSublist(list); 042 } 043 } 044 045 public void addSublist(List<T> items) { 046 if (!items.isEmpty()) { 047 this.components.add(items); 048 this.size += items.size(); 049 } 050 } 051 052 @Override 053 public int size() { 054 return this.size; 055 } 056 057 @Override 058 public java.util.Iterator<T> iterator() { 059 return new Iterator(); 060 } 061 062 @edu.umd.cs.findbugs.annotations.SuppressWarnings( 063 value="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD", 064 justification="nextWasCalled is using by StripeStoreFileManager") 065 public class Iterator implements java.util.Iterator<T> { 066 protected int currentComponent = 0; 067 protected int indexWithinComponent = -1; 068 protected boolean nextWasCalled = false; 069 070 @Override 071 public boolean hasNext() { 072 return (currentComponent + 1) < components.size() 073 || ((currentComponent + 1) == components.size() 074 && ((indexWithinComponent + 1) < components.get(currentComponent).size())); 075 } 076 077 @Override 078 public T next() { 079 if (!components.isEmpty()) { 080 this.nextWasCalled = true; 081 List<T> src = components.get(currentComponent); 082 if (++indexWithinComponent < src.size()) { 083 return src.get(indexWithinComponent); 084 } 085 086 if (++currentComponent < components.size()) { 087 indexWithinComponent = 0; 088 src = components.get(currentComponent); 089 assert src.size() > 0; 090 return src.get(indexWithinComponent); 091 } 092 } 093 this.nextWasCalled = false; 094 throw new NoSuchElementException(); 095 } 096 097 @Override 098 public void remove() { 099 throw new UnsupportedOperationException(); 100 } 101 } 102}