package net.coobird.thumbnailator.name; import java.io.File; import java.io.IOException; import java.util.Formatter; import java.util.Iterator; /** * This class is used to produce file names based on a given format string * and an internal counter which increments every time a new file name * is produced. * * @author coobird * */ public class ConsecutivelyNumberedFilenames implements Iterable { /** * The iterator to return upon the {@link #iterator()} method being called. */ private final Iterator iter; /** * Instantiates an {@code ConsecutivelyNumberedFilenames} object which * returns {@link File}s with file names which are consecutively numbered * beginning from {@code 0}. *

*

File name sequence

*
    *
  1. 0
  2. *
  3. 1
  4. *
  5. 2
  6. *
  7. 3
  8. *
* and so on. */ public ConsecutivelyNumberedFilenames() { this.iter = new ConsecutivelyNumberedFilenamesIterator(new File("").getParentFile(), "%d", 0); } /** * Instantiates an {@code ConsecutivelyNumberedFilenames} object which * returns {@link File}s with file names which are consecutively numbered * beginning from the given value. *

*

File name sequence

* For a case where the given value is {@code 5}: *
    *
  1. 5
  2. *
  3. 6
  4. *
  5. 7
  6. *
  7. 8
  8. *
* and so on. * * @param start The value from which to start counting. */ public ConsecutivelyNumberedFilenames(int start) { this.iter = new ConsecutivelyNumberedFilenamesIterator(new File("").getParentFile(), "%d", start); } /** * Instantiates an {@code ConsecutivelyNumberedFilenames} object which * returns {@link File}s with file names which are consecutively numbered * beginning from {@code 0}, with the directory specified. *

*

File name sequence

* For a case where the parent directory is {@code /foo/bar/}: *
    *
  1. /foo/bar/0
  2. *
  3. /foo/bar/1
  4. *
  5. /foo/bar/2
  6. *
  7. /foo/bar/3
  8. *
* and so on. * * @param dir The directory in which the files are to be located. * @throws IOException If the specified directory path is not a directory, * or if does not exist. */ public ConsecutivelyNumberedFilenames(File dir) throws IOException { checkDirectory(dir); this.iter = new ConsecutivelyNumberedFilenamesIterator(dir, "%d", 0); } /** * Instantiates an {@code ConsecutivelyNumberedFilenames} object which * returns {@link File}s with file names which are based on a format string. * The numbering will be consecutive from {@code 0}. *

* The format string should contain the string {@code %d} which will be * replaced with a consecutively counted number. Additional formatting * can be applied. For more details, please refer to the section on * Numeric formatting in the Java API specification for the * {@link Formatter} class. *

*

File name sequence

* For a case where the format string is {@code image-%d}: *
    *
  1. image-0
  2. *
  3. image-1
  4. *
  5. image-2
  6. *
  7. image-3
  8. *
* and so on. * * @param format The format string to use. */ public ConsecutivelyNumberedFilenames(String format) { this.iter = new ConsecutivelyNumberedFilenamesIterator(new File("").getParentFile(), format, 0); } /** * Instantiates an {@code ConsecutivelyNumberedFilenames} object which * returns {@link File}s with file names which are consecutively numbered * beginning from from the given value, with the directory specified. *

*

File name sequence

* For a case where the parent directory is {@code /foo/bar/}, and the * specified value is {@code 5}: *
    *
  1. /foo/bar/5
  2. *
  3. /foo/bar/6
  4. *
  5. /foo/bar/7
  6. *
  7. /foo/bar/8
  8. *
* and so on. * * @param dir The directory in which the files are to be located. * @param start The value from which to start counting. * @throws IOException If the specified directory path is not a directory, * or if does not exist. */ public ConsecutivelyNumberedFilenames(File dir, int start) throws IOException { checkDirectory(dir); this.iter = new ConsecutivelyNumberedFilenamesIterator(dir, "%d", start); } /** * Instantiates an {@code ConsecutivelyNumberedFilenames} object which * returns {@link File}s with file names which are based on a format string, * located in the directory specified. The numbering will be consecutively * counted from {@code 0}. *

* The format string should contain the string {@code %d} which will be * replaced with a consecutively counted number. Additional formatting * can be applied. For more details, please refer to the section on * Numeric formatting in the Java API specification for the * {@link Formatter} class. *

*

File name sequence

* For a case where the parent directory is {@code /foo/bar/}, * with the format string {@code image-%d}: *
    *
  1. /foo/bar/image-0
  2. *
  3. /foo/bar/image-1
  4. *
  5. /foo/bar/image-2
  6. *
  7. /foo/bar/image-3
  8. *
* and so on. * * @param dir The directory in which the files are to be located. * @param format The format string to use. * @throws IOException If the specified directory path is not a directory, * or if does not exist. */ public ConsecutivelyNumberedFilenames(File dir, String format) throws IOException { checkDirectory(dir); this.iter = new ConsecutivelyNumberedFilenamesIterator(dir, format, 0); } /** * Instantiates an {@code ConsecutivelyNumberedFilenames} object which * returns {@link File}s with file names which are based on a format string. * The numbering will be consecutive from the specified value. *

* The format string should contain the string {@code %d} which will be * replaced with a consecutively counted number. Additional formatting * can be applied. For more details, please refer to the section on * Numeric formatting in the Java API specification for the * {@link Formatter} class. *

*

File name sequence

* For a case where the parent directory is {@code /foo/bar/}, and the * specified value is {@code 5}, with the format string {@code image-%d}: *
    *
  1. image-5
  2. *
  3. image-6
  4. *
  5. image-7
  6. *
  7. image-8
  8. *
* and so on. * * @param format The format string to use. * @param start The value from which to start counting. */ public ConsecutivelyNumberedFilenames(String format, int start) { this.iter = new ConsecutivelyNumberedFilenamesIterator(new File("").getParentFile(), format, start); } /** * Instantiates an {@code ConsecutivelyNumberedFilenames} object which * returns {@link File}s with file names which are based on a format string, * located in the directory specified. The numbering will be consecutive * from the specified value. *

* The format string should contain the string {@code %d} which will be * replaced with a consecutively counted number. Additional formatting * can be applied. For more details, please refer to the section on * Numeric formatting in the Java API specification for the * {@link Formatter} class. *

*

File name sequence

* For a case where the parent directory is {@code /foo/bar/}, and the * specified value is {@code 5}, with format string {@code image-%d}: *
    *
  1. /foo/bar/image-5
  2. *
  3. /foo/bar/image-6
  4. *
  5. /foo/bar/image-7
  6. *
  7. /foo/bar/image-8
  8. *
* and so on. * * @param dir The directory in which the files are to be located. * @param format The format string to use. * @param start The value from which to start counting. * @throws IOException If the specified directory path is not a directory, * or if does not exist. */ public ConsecutivelyNumberedFilenames(File dir, String format, int start) throws IOException { checkDirectory(dir); this.iter = new ConsecutivelyNumberedFilenamesIterator(dir, format, start); } private static void checkDirectory(File dir) throws IOException { if (!dir.isDirectory()) { throw new IOException( "Specified path is not a directory or does not exist." ); } } private static class ConsecutivelyNumberedFilenamesIterator implements Iterator { private final File dir; private final String format; private int count; public ConsecutivelyNumberedFilenamesIterator(File dir, String format, int start) { super(); this.dir = dir; this.format = format; this.count = start; } public boolean hasNext() { return true; } public File next() { File f = new File(dir, String.format(format, count)); count++; return f; } public void remove() { throw new UnsupportedOperationException( "Cannot remove elements from this iterator." ); } } /** * Returns an iterator which generates file names according to the rules * specified by this object. * * @return An iterator which generates file names. */ public Iterator iterator() { return iter; } }