001package com.box.sdk; 002 003import java.util.ArrayList; 004import java.util.Collection; 005import java.util.Iterator; 006 007/** 008 * A collection that contains a subset of items that are a part of a larger collection. The items within a partial 009 * collection begin at an offset within the full collection and end at a specified limit. Note that the actual size of a 010 * partial collection may be less than its limit since the limit only specifies the maximum size. For example, if 011 * there's a full collection with a size of 3, then a partial collection with offset 0 and limit 3 would be equal to a 012 * partial collection with offset 0 and limit 100. 013 * 014 * @param <E> the type of elements in this partial collection. 015 */ 016public class PartialCollection<E> implements Collection<E> { 017 private final Collection<E> collection; 018 private final long offset; 019 private final long limit; 020 private final long fullSize; 021 022 /** 023 * Constructs a PartialCollection with a specified offset, limit, and full size. 024 * 025 * @param offset the offset within in the full collection. 026 * @param limit the maximum number of items after the offset. 027 * @param fullSize the total number of items in the full collection. 028 */ 029 public PartialCollection(long offset, long limit, long fullSize) { 030 this.collection = new ArrayList<E>(); 031 this.offset = offset; 032 this.limit = limit; 033 this.fullSize = fullSize; 034 } 035 036 /** 037 * Gets the offset within the full collection where this collection's items begin. 038 * 039 * @return the offset within the full collection where this collection's items begin. 040 */ 041 public long offset() { 042 return this.offset; 043 } 044 045 /** 046 * Gets the maximum number of items within the full collection that begin at {@link #offset}. 047 * 048 * @return the maximum number of items within the full collection that begin at the offset. 049 */ 050 public long limit() { 051 return this.limit; 052 } 053 054 /** 055 * Gets the size of the full collection that this partial collection is based off of. 056 * 057 * @return the size of the full collection that this partial collection is based off of. 058 */ 059 public long fullSize() { 060 return this.fullSize; 061 } 062 063 @Override 064 public boolean add(E e) { 065 return this.collection.add(e); 066 } 067 068 @Override 069 public boolean addAll(Collection<? extends E> c) { 070 return this.collection.addAll(c); 071 } 072 073 @Override 074 public void clear() { 075 this.collection.clear(); 076 } 077 078 @Override 079 public boolean contains(Object o) { 080 return this.collection.contains(o); 081 } 082 083 @Override 084 public boolean containsAll(Collection<?> c) { 085 return this.collection.containsAll(c); 086 } 087 088 @Override 089 public boolean equals(Object o) { 090 return this.collection.equals(o); 091 } 092 093 @Override 094 public int hashCode() { 095 return this.collection.hashCode(); 096 } 097 098 @Override 099 public boolean isEmpty() { 100 return this.collection.isEmpty(); 101 } 102 103 @Override 104 public Iterator<E> iterator() { 105 return this.collection.iterator(); 106 } 107 108 @Override 109 public boolean remove(Object o) { 110 return this.collection.remove(o); 111 } 112 113 @Override 114 public boolean removeAll(Collection<?> c) { 115 return this.collection.removeAll(c); 116 } 117 118 @Override 119 public boolean retainAll(Collection<?> c) { 120 return this.collection.retainAll(c); 121 } 122 123 @Override 124 public int size() { 125 return this.collection.size(); 126 } 127 128 @Override 129 public Object[] toArray() { 130 return this.collection.toArray(); 131 } 132 133 @Override 134 public <T> T[] toArray(T[] a) { 135 return this.collection.toArray(a); 136 } 137}