All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javolution.util.internal.collection.MappedCollectionImpl Maven / Gradle / Ivy

The newest version!
/*
 * 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 function;

    @SuppressWarnings("unchecked")
    public MappedCollectionImpl(CollectionService target,
            Function 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 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();
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy