001/*
002 * #%L
003 * Netarchivesuite - common
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.common.utils;
025
026import java.io.File;
027import java.util.Arrays;
028
029/**
030 * An iterator that iterates over elements that can be read from files, given an array of files. It is robust against
031 * disappearing files, but does not try to find new ones that appear while iterating. It keeps the Iterator contract
032 * that next() returns an element if hasNext() returned true since last next(). This may mean that the underlying file
033 * has disappeared by the time next() is called, but the object is returned anyway.
034 *
035 * @param <T> The type returned by the FileArrayIterator
036 */
037public abstract class FileArrayIterator<T> extends FilterIterator<File, T> {
038
039    protected FileArrayIterator(File[] files) {
040        super(Arrays.asList(files).iterator());
041    }
042
043    /**
044     * Returns the T object corresponding to the given file, or null if that object is to be skipped.
045     *
046     * @param f A given file
047     * @return An object in the T domain, or null
048     */
049    protected T filter(File f) {
050        return getNext(f);
051    }
052
053    /**
054     * Gives an object created from the given file, or null.
055     *
056     * @param file The file to read
057     * @return An object of the type iterated over by the list, or null if the file does not exist or cannot be used to
058     * create an appropriate object.
059     */
060    protected abstract T getNext(final File file);
061
062}