Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / util / FileNameGenerator.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  */
18                 
19 package peersim.util;
20
21 import java.io.*;
22
23
24 /**
25 * Generates a series of filenames for classes that have to save e.g.
26 * snapshots regularly.
27 */
28 public class FileNameGenerator {
29
30
31 /**
32 * The number of filenames already returned.
33 */
34 private long counter = 0;
35
36 /** The prefix of the filename */
37 public final String prefix;
38
39 /** The extension of the filename */
40 public final String ext;
41
42
43 // ==================== initialization ==============================
44 // ==================================================================
45
46
47 /**
48 * @param prefix all returned names will be prefixed by this
49 * @param ext will be appended to all returned names
50 */
51 public FileNameGenerator(String prefix, String ext) {
52         
53         this.prefix=prefix;
54         this.ext=ext;
55 }
56
57
58 // ===================== methods ====================================
59 // ==================================================================
60
61
62 /**
63 * Generates a name based on a counter.
64 * The format of the name returned is {@link #prefix} followed by
65 * an 8 digit zero padded number, followed by {@link #ext}.
66 * The first number used is zero.
67 * @return the next filename after increasing the counter
68 */
69 public String nextCounterName() {
70         
71         ByteArrayOutputStream baos = new ByteArrayOutputStream();
72         (new PrintStream(baos)).printf("%08d",counter);
73         counter++;
74         return prefix+baos.toString()+ext;
75 }
76
77 // ------------------------------------------------------------------
78
79 /*
80 public static void main(String args[]) {
81         
82         FileNameGenerator fng = new FileNameGenerator(args[0],args[1]);
83         for(int i=0; i<100; ++i) System.err.println(fng.nextCounterName()); 
84 }
85 */
86 }
87