Author: tchemit Date: 2008-02-02 15:48:17 +0000 (Sat, 02 Feb 2008) New Revision: 581 Added: trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/VersionGenerator.java Log: g?\195?\169n?\195?\169rateur de versions Added: trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/VersionGenerator.java =================================================================== --- trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/VersionGenerator.java (rev 0) +++ trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/VersionGenerator.java 2008-02-02 15:48:17 UTC (rev 581) @@ -0,0 +1,94 @@ +/* +* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais, Tony Chemit +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package fr.cemagref.simexplorer.is.storage; + +import fr.cemagref.simexplorer.is.entities.metadata.Version; + +import java.util.Random; + + +/** + * A Version generator + * + * @author chemit + */ +public class VersionGenerator { + + /** la taille maximum d'une version générée */ + static final int MAX_SIZE = 5; + + /** la valeur maximum d'un numéro de version à générer */ + static final int MAX_VERSION_NUMBER = 10; + + /** le nombre maximum de versions générées dans la méthode {@link #generateVersions()} */ + static final int MAX_NB_VERSIONS = 20; + + /** instance statique partagée */ + protected static VersionGenerator instance; + + public static VersionGenerator getInstance() { + if (instance == null) { + instance = new VersionGenerator(); + } + return instance; + } + + protected Random r; + + public Version generateVersion() { + StringBuilder sb = new StringBuilder(); + for (int i = 0, size = generateNotNullAbsInt(MAX_SIZE); i < size; i++) { + sb.append('.').append(generateAbsInt(MAX_VERSION_NUMBER)); + } + return Version.valueOf(sb.substring(1)); + } + + public Version[] generateVersions() { + int nb = generateNotNullAbsInt(MAX_NB_VERSIONS); + return generateVersions(nb); + } + + public Version[] generateVersions(int nb) { + Version[] result = new Version[nb]; + result[0] = generateVersion(); + System.out.println("nb versions :" + nb); + System.out.println("version 0 :" + result[0]); + for (int i = 1; i < nb; i++) { + result[i] = result[i - 1].incVersion(generateAbsInt(MAX_SIZE)); + System.out.println("version " + i + " :" + result[i]); + } + return result; + } + + protected int generateAbsInt(int max) { + return Math.abs(r.nextInt()) % max; + } + + protected int generateNotNullAbsInt(int max) { + int nb = 0; + while (nb == 0) { + nb = generateAbsInt(max); + } + return nb; + } + + protected VersionGenerator() { + this.r = new Random(); + } + +} \ No newline at end of file
participants (1)
-
tchemit@users.labs.libre-entreprise.org