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