src/core/net/sf/basedb/util/bfs/AbstractFilenameGenerator.java

Code
Comments
Other
Rev Date Author Line
5212 12 Jan 10 nicklas 1 /**
5212 12 Jan 10 nicklas 2   $Id$
5212 12 Jan 10 nicklas 3
5212 12 Jan 10 nicklas 4   Copyright (C) 2009 Nicklas Nordborg
5212 12 Jan 10 nicklas 5
5212 12 Jan 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5212 12 Jan 10 nicklas 7   Available at http://base.thep.lu.se/
5212 12 Jan 10 nicklas 8
5212 12 Jan 10 nicklas 9   BASE is free software; you can redistribute it and/or
5212 12 Jan 10 nicklas 10   modify it under the terms of the GNU General Public License
5212 12 Jan 10 nicklas 11   as published by the Free Software Foundation; either version 3
5212 12 Jan 10 nicklas 12   of the License, or (at your option) any later version.
5212 12 Jan 10 nicklas 13
5212 12 Jan 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5212 12 Jan 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5212 12 Jan 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5212 12 Jan 10 nicklas 17   GNU General Public License for more details.
5212 12 Jan 10 nicklas 18
5212 12 Jan 10 nicklas 19   You should have received a copy of the GNU General Public License
5212 12 Jan 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5212 12 Jan 10 nicklas 21 */
5212 12 Jan 10 nicklas 22 package net.sf.basedb.util.bfs;
5212 12 Jan 10 nicklas 23
5212 12 Jan 10 nicklas 24 import java.util.HashSet;
5212 12 Jan 10 nicklas 25 import java.util.Set;
5212 12 Jan 10 nicklas 26
5212 12 Jan 10 nicklas 27 /**
5212 12 Jan 10 nicklas 28   Abstract superclass for file names generator implementions that will help
5212 12 Jan 10 nicklas 29   with ensuring that a generator creates unique file names. Subclasses may simply
5212 12 Jan 10 nicklas 30   call {@link #makeUnique(String)} to ensure that no duplicate file names are
5212 12 Jan 10 nicklas 31   generated.
5212 12 Jan 10 nicklas 32   
5212 12 Jan 10 nicklas 33   @author Nicklas
5212 12 Jan 10 nicklas 34   @version 2.15
5212 12 Jan 10 nicklas 35   @base.modified $Date$
5212 12 Jan 10 nicklas 36 */
5212 12 Jan 10 nicklas 37 public abstract class AbstractFilenameGenerator<T>
5212 12 Jan 10 nicklas 38   implements FilenameGenerator<T>
5212 12 Jan 10 nicklas 39 {
5212 12 Jan 10 nicklas 40   
5212 12 Jan 10 nicklas 41   private Set<String> generatedFiles;
5212 12 Jan 10 nicklas 42   
5212 12 Jan 10 nicklas 43   protected AbstractFilenameGenerator()
5212 12 Jan 10 nicklas 44   {
5212 12 Jan 10 nicklas 45     generatedFiles = new HashSet<String>();
5212 12 Jan 10 nicklas 46   }
5212 12 Jan 10 nicklas 47   
5212 12 Jan 10 nicklas 48   /**
5212 12 Jan 10 nicklas 49     Checks if the suggested file name has already been generated by this
5212 12 Jan 10 nicklas 50     file name generator or not.
5212 12 Jan 10 nicklas 51     
5212 12 Jan 10 nicklas 52     @param filename The filename to check
5212 12 Jan 10 nicklas 53     @return TRUE if the name is unique or FALSE if it has already been used
5212 12 Jan 10 nicklas 54   */
5212 12 Jan 10 nicklas 55   protected boolean isUnique(String filename)
5212 12 Jan 10 nicklas 56   {
5212 12 Jan 10 nicklas 57     return !generatedFiles.contains(filename);
5212 12 Jan 10 nicklas 58   }
5212 12 Jan 10 nicklas 59   
5212 12 Jan 10 nicklas 60   /**
5212 12 Jan 10 nicklas 61     Add the file name to the list of already generated file names.
5212 12 Jan 10 nicklas 62     @param filename The file name to add
5212 12 Jan 10 nicklas 63     @return TRUE if the file name has not been used before, FALSE otherwise
5212 12 Jan 10 nicklas 64   */
5212 12 Jan 10 nicklas 65   protected boolean addFilename(String filename)
5212 12 Jan 10 nicklas 66   {
5212 12 Jan 10 nicklas 67     return generatedFiles.add(filename);
5212 12 Jan 10 nicklas 68   }
5212 12 Jan 10 nicklas 69   
5212 12 Jan 10 nicklas 70   /**
5212 12 Jan 10 nicklas 71     Make sure that a file name is unique. If the suggested file name is already
5212 12 Jan 10 nicklas 72     unique the input parameter is returned. Otherwise a new filename is generated
5212 12 Jan 10 nicklas 73     by appending a counter value to it until a unique name is found. The counter 
5212 12 Jan 10 nicklas 74     value is added before any extension. Eg. example.txt, example-2.txt, example-3.txt
5212 12 Jan 10 nicklas 75     would be generated if this method was called with "example.txt" as a parameter
5212 12 Jan 10 nicklas 76     three times. The returned filename is automatically added to the list of known
5212 12 Jan 10 nicklas 77     generated file names.
5212 12 Jan 10 nicklas 78     
5212 12 Jan 10 nicklas 79     @param filename The file name to check
5212 12 Jan 10 nicklas 80     @return A file name that is sure to be unique
5212 12 Jan 10 nicklas 81   */
5212 12 Jan 10 nicklas 82   protected String makeUnique(String filename)
5212 12 Jan 10 nicklas 83   {
5212 12 Jan 10 nicklas 84     int counter = 2;
5212 12 Jan 10 nicklas 85     int dotIndex = filename.indexOf('.');
5212 12 Jan 10 nicklas 86     String prefix = dotIndex > 0 ? filename.substring(0, dotIndex) : filename;
5212 12 Jan 10 nicklas 87     String suffix = dotIndex > 0 ? filename.substring(dotIndex) : "";
5212 12 Jan 10 nicklas 88     while (!isUnique(filename))
5212 12 Jan 10 nicklas 89     {
5212 12 Jan 10 nicklas 90       filename = prefix + "-" + counter + suffix;
5212 12 Jan 10 nicklas 91       counter++;
5212 12 Jan 10 nicklas 92     }
5212 12 Jan 10 nicklas 93     addFilename(filename);
5212 12 Jan 10 nicklas 94     return filename;
5212 12 Jan 10 nicklas 95   }
5212 12 Jan 10 nicklas 96   
5212 12 Jan 10 nicklas 97   
5212 12 Jan 10 nicklas 98 }