javolution.util.internal.collection.MappedCollectionImpl 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.Iterator;
import javolution.util.function.Equalities;
import javolution.util.function.Equality;
import javolution.util.function.Function;
import javolution.util.service.CollectionService;
/**
* A mapped view over a collection.
*/
public class MappedCollectionImpl extends CollectionView {
/** Mapping iterator. */
private class IteratorImpl implements Iterator {
private final Iterator targetIterator;
@SuppressWarnings("unchecked")
public IteratorImpl() {
targetIterator = (Iterator) target().iterator();
}
@Override
public boolean hasNext() {
return targetIterator.hasNext();
}
@Override
public R next() {
return function.apply(targetIterator.next());
}
@Override
public void remove() {
targetIterator.remove();
}
}
private static final long serialVersionUID = 0x600L; // Version.
protected final Function super E, ? extends R> function;
@SuppressWarnings("unchecked")
public MappedCollectionImpl(CollectionService target,
Function super E, ? extends R> function) {
super((CollectionService) target); // Beware target is of type
this.function = function;
}
@Override
public boolean add(R element) {
throw new UnsupportedOperationException(
"New elements cannot be added to mapped views");
}
@Override
public void clear() {
target().clear();
}
@Override
public Equality super R> comparator() {
return Equalities.STANDARD;
}
@Override
public boolean isEmpty() {
return target().isEmpty();
}
@Override
public Iterator iterator() {
return new IteratorImpl();
}
@Override
public int size() {
return target().size();
}
@SuppressWarnings("unchecked")
@Override
public CollectionService[] split(int n, boolean updateable) {
CollectionService[] subTargets = (CollectionService[]) target().split(n, updateable);
CollectionService[] result = new CollectionService[subTargets.length];
for (int i = 0; i < subTargets.length; i++) {
result[i] = new MappedCollectionImpl(subTargets[i], function);
}
return result;
}
}