5212 |
12 Jan 10 |
nicklas |
1 |
/** |
5212 |
12 Jan 10 |
nicklas |
$Id$ |
5212 |
12 Jan 10 |
nicklas |
3 |
|
5212 |
12 Jan 10 |
nicklas |
Copyright (C) 2009 Nicklas Nordborg |
5212 |
12 Jan 10 |
nicklas |
5 |
|
5212 |
12 Jan 10 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5212 |
12 Jan 10 |
nicklas |
Available at http://base.thep.lu.se/ |
5212 |
12 Jan 10 |
nicklas |
8 |
|
5212 |
12 Jan 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
5212 |
12 Jan 10 |
nicklas |
modify it under the terms of the GNU General Public License |
5212 |
12 Jan 10 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5212 |
12 Jan 10 |
nicklas |
of the License, or (at your option) any later version. |
5212 |
12 Jan 10 |
nicklas |
13 |
|
5212 |
12 Jan 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5212 |
12 Jan 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5212 |
12 Jan 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5212 |
12 Jan 10 |
nicklas |
GNU General Public License for more details. |
5212 |
12 Jan 10 |
nicklas |
18 |
|
5212 |
12 Jan 10 |
nicklas |
You should have received a copy of the GNU General Public License |
5212 |
12 Jan 10 |
nicklas |
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 |
Abstract superclass for file names generator implementions that will help |
5212 |
12 Jan 10 |
nicklas |
with ensuring that a generator creates unique file names. Subclasses may simply |
5212 |
12 Jan 10 |
nicklas |
call {@link #makeUnique(String)} to ensure that no duplicate file names are |
5212 |
12 Jan 10 |
nicklas |
generated. |
5212 |
12 Jan 10 |
nicklas |
32 |
|
5212 |
12 Jan 10 |
nicklas |
@author Nicklas |
5212 |
12 Jan 10 |
nicklas |
@version 2.15 |
5212 |
12 Jan 10 |
nicklas |
@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 |
Checks if the suggested file name has already been generated by this |
5212 |
12 Jan 10 |
nicklas |
file name generator or not. |
5212 |
12 Jan 10 |
nicklas |
51 |
|
5212 |
12 Jan 10 |
nicklas |
@param filename The filename to check |
5212 |
12 Jan 10 |
nicklas |
@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 |
Add the file name to the list of already generated file names. |
5212 |
12 Jan 10 |
nicklas |
@param filename The file name to add |
5212 |
12 Jan 10 |
nicklas |
@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 |
Make sure that a file name is unique. If the suggested file name is already |
5212 |
12 Jan 10 |
nicklas |
unique the input parameter is returned. Otherwise a new filename is generated |
5212 |
12 Jan 10 |
nicklas |
by appending a counter value to it until a unique name is found. The counter |
5212 |
12 Jan 10 |
nicklas |
value is added before any extension. Eg. example.txt, example-2.txt, example-3.txt |
5212 |
12 Jan 10 |
nicklas |
would be generated if this method was called with "example.txt" as a parameter |
5212 |
12 Jan 10 |
nicklas |
three times. The returned filename is automatically added to the list of known |
5212 |
12 Jan 10 |
nicklas |
generated file names. |
5212 |
12 Jan 10 |
nicklas |
78 |
|
5212 |
12 Jan 10 |
nicklas |
@param filename The file name to check |
5212 |
12 Jan 10 |
nicklas |
@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 |
} |