001/**
002 * 
003 */
004package icy.type;
005
006import icy.image.ImageDataIterator;
007import icy.sequence.SequenceDataIterator;
008
009/**
010 * Utilities for {@link DataIterator} classes.
011 * 
012 * @author Stephane
013 */
014public class DataIteratorUtil
015{
016    /**
017     * Returns the number of element contained in the specified {@link DataIterator}.
018     */
019    public static long count(DataIterator it)
020    {
021        long result = 0;
022
023        it.reset();
024
025        while (!it.done())
026        {
027            it.next();
028            result++;
029        }
030
031        return result;
032    }
033
034    /**
035     * Sets the specified value to the specified {@link DataIterator}.
036     */
037    public static void set(DataIterator it, double value)
038    {
039        it.reset();
040
041        while (!it.done())
042        {
043            it.set(value);
044            it.next();
045        }
046
047        // not really nice to do that here, but it's to preserve backward compatibility
048        if (it instanceof SequenceDataIterator)
049            ((SequenceDataIterator) it).flush();
050        else if (it instanceof ImageDataIterator)
051            ((ImageDataIterator) it).flush();
052    }
053}