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