javolution.util.internal.collection.CollectionView Maven / Gradle / Ivy
/*
* Javolution - Java(TM) Solution for Real-Time and Embedded Systems
* Copyright (C) 2012 - Javolution (http://javolution.org/)
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software is
* freely granted, provided that this notice is preserved.
*/
package javolution.util.internal.collection;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javolution.util.FastCollection;
import javolution.util.function.Consumer;
import javolution.util.function.Equalities;
import javolution.util.function.Equality;
import javolution.util.service.CollectionService;
/**
* Collection view implementation; can be used as root class for implementations
* if target is {@code null}.
* When possible sub-classes should forward to the actual target for the methods
* clear, remove, contains, size and isEmpty rather than using the default
* implementation.
*/
public abstract class CollectionView extends FastCollection implements CollectionService {
//public abstract class CollectionView implements CollectionService {
private static final long serialVersionUID = 0x600L; // Version.
private CollectionService target;
/**
* The view constructor or root class constructor if target is {@code null}.
*/
public CollectionView(CollectionService target) {
this.target = target;
}
@Override
public abstract boolean add(E element);
@Override
public boolean addAll(Collection extends E> c) {
boolean changed = false;
Iterator extends E> it = c.iterator();
while (it.hasNext()) {
if (add(it.next())) changed = true;
}
return changed;
}
@Override
public void clear() {
Iterator extends E> it = iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
}
@SuppressWarnings("unchecked")
@Override
public CollectionView clone() {
try {
CollectionView copy = (CollectionView) super.clone();
if (target != null) { // Not a root class.
copy.target = target.clone();
}
return copy;
} catch (CloneNotSupportedException e) {
throw new Error("Should not happen since target is cloneable");
}
}
@Override
public abstract Equality super E> comparator();
@Override
@SuppressWarnings("unchecked")
public boolean contains(Object obj) {
Iterator extends E> it = iterator();
Equality