Example usage for java.util AbstractCollection AbstractCollection

List of usage examples for java.util AbstractCollection AbstractCollection

Introduction

In this page you can find the example usage for java.util AbstractCollection AbstractCollection.

Prototype

protected AbstractCollection() 

Source Link

Document

Sole constructor.

Usage

From source file:Main.java

private static <E> Collection<E> asCollection(final E[] elements) {
    return new AbstractCollection<E>() {

        public Iterator<E> iterator() {
            return new Iterator<E>() {

                // Object field
                private int index = 0;

                // Interface methods
                public boolean hasNext() {
                    return index < elements.length;
                }//from w  ww  . j  av  a2  s .  com

                public E next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return elements[index++];
                }

                public void remove() {
                    throw new UnsupportedOperationException("remove");
                }

            };
        }

        public int size() {
            return elements.length;
        }

    };
}

From source file:Main.java

public static <T1, T2> Collection<T2> lazyMap(final Collection<T1> lst, final Function<T1, T2> mapper) {
    return new AbstractCollection<T2>() {
        @Override//from www  .  j a v a2  s .  com
        public Iterator<T2> iterator() {
            return new Iterator<T2>() {
                Iterator<T1> impl = lst.iterator();

                @Override
                public boolean hasNext() {
                    return impl.hasNext();
                }

                @Override
                public T2 next() {
                    return mapper.apply(impl.next());
                }

                @Override
                public void remove() {
                    impl.remove();
                }
            };
        }

        @Override
        public int size() {
            return lst.size();
        }
    };
}

From source file:edu.byu.nlp.util.Collections3.java

/**
 * Only if list://ww w.j a v  a 2s.  c  o m
 * The semantics of the collection returned by this method become undefined if the backing collection (i.e., this
 * collection) is structurally modified in any way other than via the returned collection. (Structural modifications
 * are those that change the size of this collection, or otherwise perturb it in such a fashion that iterations in
 * progress may yield incorrect results.)
 */
public static <E> Collection<E> limit(final Collection<E> coll, final int limitSize) {
    if (coll instanceof List) {
        return ((List<E>) coll).subList(0, limitSize);
    }

    return new AbstractCollection<E>() {

        @Override
        public Iterator<E> iterator() {
            return Iterators.limit(coll.iterator(), limitSize);
        }

        @Override
        public int size() {
            return Math.min(limitSize, coll.size());
        }

    };
}

From source file:com.tussle.hitbox.HitboxComponent.java

public Collection<Hitbox> getHitboxes() {
    if (hitboxValues == null) {
        hitboxValues = new AbstractCollection<Hitbox>() {
            public Iterator<Hitbox> iterator() {
                return IteratorUtils
                        .chainedIterator(CollectionUtils.collect(hitboxes.values(), HashSet::iterator));
            }/*from  ww  w . ja va 2  s  .c om*/

            public int size() {
                //WHEE JAVA FUNCTIONAL STREAMS
                return hitboxes.values().stream().mapToInt(HashSet::size).sum();
            }
        };
    }
    return hitboxValues;
}

From source file:com.tussle.hitbox.HurtboxComponent.java

public Collection<Hurtbox> getHurtboxes() {
    if (hurtboxValues == null) {
        hurtboxValues = new AbstractCollection<Hurtbox>() {
            public Iterator<Hurtbox> iterator() {
                return IteratorUtils
                        .chainedIterator(CollectionUtils.collect(hurtboxes.values(), HashSet::iterator));
            }//from   w  w  w  .j  av a 2s  .  co m

            public int size() {
                //WHEE JAVA FUNCTIONAL STREAMS
                return hurtboxes.values().stream().mapToInt(HashSet::size).sum();
            }
        };
    }
    return hurtboxValues;
}

From source file:com.tussle.collision.StageElementComponent.java

public Collection<StageElement> getStageElements() {
    if (hitboxValues == null) {
        hitboxValues = new AbstractCollection<StageElement>() {
            public Iterator<StageElement> iterator() {
                return IteratorUtils
                        .chainedIterator(CollectionUtils.collect(surfaces.values(), HashSet::iterator));
            }/*from w  w w .  j a  va2 s.c o m*/

            public int size() {
                //WHEE JAVA FUNCTIONAL STREAMS
                return surfaces.values().stream().mapToInt(HashSet::size).sum();
            }
        };
    }
    return hitboxValues;
}

From source file:com.tussle.collision.ECBComponent.java

public Collection<StageElement<CollisionStadium>> getCollisionBoxes() {
    if (hitboxValues == null) {
        hitboxValues = new AbstractCollection<StageElement<CollisionStadium>>() {
            public Iterator<StageElement<CollisionStadium>> iterator() {
                return IteratorUtils.chainedIterator(CollectionUtils.collect(ecbs.values(), HashSet::iterator));
            }/* w w  w .j  a  v a  2s .co  m*/

            public int size() {
                //WHEE JAVA FUNCTIONAL STREAMS
                return ecbs.values().stream().mapToInt(HashSet::size).sum();
            }
        };
    }
    return hitboxValues;
}

From source file:edu.byu.nlp.util.Collections3.java

/**
 * Only if list://from  w w  w  .  j  a va 2 s.co  m
 * The semantics of the collection returned by this method become undefined if the backing collection (i.e., this
 * collection) is structurally modified in any way other than via the returned collection. (Structural modifications
 * are those that change the size of this collection, or otherwise perturb it in such a fashion that iterations in
 * progress may yield incorrect results.)
 */
public static <E> Collection<E> skip(final Collection<E> coll, final int numberToSkip) {
    if (coll instanceof List) {
        return ((List<E>) coll).subList(numberToSkip, coll.size());
    }

    Preconditions.checkElementIndex(numberToSkip, coll.size());

    return new AbstractCollection<E>() {

        @Override
        public Iterator<E> iterator() {
            Iterator<E> it = coll.iterator();
            Iterators.advance(it, numberToSkip);
            return it;
        }

        @Override
        public int size() {
            return Math.max(0, coll.size() - numberToSkip);
        }

    };
}

From source file:edu.byu.nlp.util.Collections3.java

/**
 * Creates a new collection that is logically the concatenation of the provided collections. The returned collection
 * is a view over the original two.//w w w.  j a  v a2 s  .  c  o  m
 */
public static <E> Collection<E> concat(final Collection<E> coll1, final Collection<E> coll2) {
    return new AbstractCollection<E>() {

        @Override
        public Iterator<E> iterator() {
            return Iterators.concat(coll1.iterator(), coll2.iterator());
        }

        @Override
        public int size() {
            return coll1.size() + coll2.size();
        }

    };
}

From source file:ListOrderedMap.java

public Collection values() {
    return new AbstractCollection() {
        public int size() {
            return map.size();
        }//from  w  w w . j a  va  2 s. c  om

        public boolean contains(Object value) {
            return map.containsValue(value);
        }

        public void clear() {
            ListOrderedMap.this.clear();
        }

        public Iterator iterator() {
            return new Iterator() {
                Object last = null;
                Iterator keys = lst.iterator();

                public Object next() {
                    return map.get(last = keys.next());
                }

                public boolean hasNext() {
                    return keys.hasNext();
                }

                public void remove() {
                    keys.remove();
                    map.remove(last);
                }
            };
        }
    };
}