src/core/net/sf/basedb/util/bfs/SequenceFilenameGenerator.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 /**
5212 12 Jan 10 nicklas 25   File name generator implementation that generates file names using
5212 12 Jan 10 nicklas 26   a simple numerical counter. It is possible to give a prexix and/or
5212 12 Jan 10 nicklas 27   suffix. For example, with prefix="file-" and suffix=".txt", the following
5212 12 Jan 10 nicklas 28   file sequence is generated: file-1.txt, file-2.txt, file-3.txt
5212 12 Jan 10 nicklas 29   This generator completely ignores the owner and suggested file names.
5212 12 Jan 10 nicklas 30   
5212 12 Jan 10 nicklas 31   @author Nicklas
5212 12 Jan 10 nicklas 32   @version 2.15
5212 12 Jan 10 nicklas 33   @base.modified $Date$
5212 12 Jan 10 nicklas 34 */
5212 12 Jan 10 nicklas 35 public class SequenceFilenameGenerator
5212 12 Jan 10 nicklas 36   implements FilenameGenerator<Object>
5212 12 Jan 10 nicklas 37 {
5212 12 Jan 10 nicklas 38   private String prefix;
5212 12 Jan 10 nicklas 39   private String suffix;
5212 12 Jan 10 nicklas 40   private int nextFileNum;
5212 12 Jan 10 nicklas 41
5212 12 Jan 10 nicklas 42   /**
5212 12 Jan 10 nicklas 43     Create a new sequence file name generator. No prefix or suffix are
5212 12 Jan 10 nicklas 44     used and the sequence starts at 1.
5212 12 Jan 10 nicklas 45   */
5212 12 Jan 10 nicklas 46   public SequenceFilenameGenerator()
5212 12 Jan 10 nicklas 47   {
5212 12 Jan 10 nicklas 48     this.nextFileNum = 1;
5212 12 Jan 10 nicklas 49   }
5212 12 Jan 10 nicklas 50   
5212 12 Jan 10 nicklas 51   /**
5212 12 Jan 10 nicklas 52     Create a new sequence file name generator.
5212 12 Jan 10 nicklas 53     @param prefix The prefix to use
5212 12 Jan 10 nicklas 54     @param suffix The suffix to use
5212 12 Jan 10 nicklas 55     @param firstFileNum The sequence number of the first file
5212 12 Jan 10 nicklas 56   */
5212 12 Jan 10 nicklas 57   public SequenceFilenameGenerator(String prefix, String suffix, int firstFileNum)
5212 12 Jan 10 nicklas 58   {
5212 12 Jan 10 nicklas 59     this.prefix = prefix;
5212 12 Jan 10 nicklas 60     this.suffix = suffix;
5212 12 Jan 10 nicklas 61     this.nextFileNum = firstFileNum;
5212 12 Jan 10 nicklas 62   }
5212 12 Jan 10 nicklas 63   
5212 12 Jan 10 nicklas 64   /*
5212 12 Jan 10 nicklas 65     From the FilenameGenerator interface
5212 12 Jan 10 nicklas 66     ------------------------------------
5212 12 Jan 10 nicklas 67   */
5212 12 Jan 10 nicklas 68   @Override
5212 12 Jan 10 nicklas 69   public String getNextFilename(Object owner, String suggestedFilename)
5212 12 Jan 10 nicklas 70   {
5212 12 Jan 10 nicklas 71     StringBuilder sb = new StringBuilder();
5212 12 Jan 10 nicklas 72     String prefix = getPrefix();
5212 12 Jan 10 nicklas 73     String suffix = getSuffix();
5212 12 Jan 10 nicklas 74     if (prefix != null) sb.append(prefix);
5212 12 Jan 10 nicklas 75     sb.append(nextFileNum);
5212 12 Jan 10 nicklas 76     nextFileNum++;
5212 12 Jan 10 nicklas 77     if (suffix != null) sb.append(suffix);
5212 12 Jan 10 nicklas 78     return sb.toString();
5212 12 Jan 10 nicklas 79   }
5212 12 Jan 10 nicklas 80   // -----------------------------------
5212 12 Jan 10 nicklas 81   
5212 12 Jan 10 nicklas 82   /**
5212 12 Jan 10 nicklas 83     Get the prefix that is used for file name  generation. 
5212 12 Jan 10 nicklas 84     @return The prefix or null if no prefix was given
5212 12 Jan 10 nicklas 85   */
5212 12 Jan 10 nicklas 86   public String getPrefix()
5212 12 Jan 10 nicklas 87   {
5212 12 Jan 10 nicklas 88     return prefix;
5212 12 Jan 10 nicklas 89   }
5212 12 Jan 10 nicklas 90   
5212 12 Jan 10 nicklas 91   /**
5212 12 Jan 10 nicklas 92     Set the prefix that is used for file name generation.
5212 12 Jan 10 nicklas 93     @param prefix The prefix or null to not use a prefix
5212 12 Jan 10 nicklas 94   */
5212 12 Jan 10 nicklas 95   public void setPrefix(String prefix)
5212 12 Jan 10 nicklas 96   {
5212 12 Jan 10 nicklas 97     this.prefix = prefix;
5212 12 Jan 10 nicklas 98   }
5212 12 Jan 10 nicklas 99   
5212 12 Jan 10 nicklas 100   /**
5212 12 Jan 10 nicklas 101     Get the suffix that is used for file name  generation. 
5212 12 Jan 10 nicklas 102     @return The suffix or null if no suffix was given
5212 12 Jan 10 nicklas 103   */
5212 12 Jan 10 nicklas 104   public String getSuffix()
5212 12 Jan 10 nicklas 105   {
5212 12 Jan 10 nicklas 106     return suffix;
5212 12 Jan 10 nicklas 107   }
5212 12 Jan 10 nicklas 108   
5212 12 Jan 10 nicklas 109   /**
5212 12 Jan 10 nicklas 110     Set the suffix that is used for file name generation.
5212 12 Jan 10 nicklas 111     @param suffix The suffix or null to not use a suffix
5212 12 Jan 10 nicklas 112   */
5212 12 Jan 10 nicklas 113   public void setSuffix(String suffix)
5212 12 Jan 10 nicklas 114   {
5212 12 Jan 10 nicklas 115     this.suffix = suffix;
5212 12 Jan 10 nicklas 116   }
5212 12 Jan 10 nicklas 117
5212 12 Jan 10 nicklas 118   /**
5212 12 Jan 10 nicklas 119     Get the next file number. This counter is increased for 
5212 12 Jan 10 nicklas 120     each file that is generated (eg. each time {@link #getNextFilename(Object, String)}
5212 12 Jan 10 nicklas 121     is called.
5212 12 Jan 10 nicklas 122   */
5212 12 Jan 10 nicklas 123   public int getNextFileNumber()
5212 12 Jan 10 nicklas 124   {
5212 12 Jan 10 nicklas 125     return nextFileNum;
5212 12 Jan 10 nicklas 126   }
5212 12 Jan 10 nicklas 127
5212 12 Jan 10 nicklas 128   
5212 12 Jan 10 nicklas 129 }