Author: tchemit Date: 2008-02-02 15:03:53 +0000 (Sat, 02 Feb 2008) New Revision: 578 Modified: trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/entities/metadata/Version.java Log: l'objet Version revisit?\195?\169... Il s'agit d'un bean classique : ajout du comportement Cloneable implantation des m?\195?\169thodes d'?\195?\169galit?\195?\169s Ajout d'une fabrique statique de Version + factorisation code + javadoc TODO : passer en protected le constructeur ?, il serait mieux d'utiliser la factory afin de pouvoir controler les instances de Version car chaque version contient une liste et c'est un peu dommage de dupliquer les listes... TODO : la m?\195?\169thode getLength devrait s'appeler size ? Modified: trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/entities/metadata/Version.java =================================================================== --- trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/entities/metadata/Version.java 2008-02-02 14:33:14 UTC (rev 577) +++ trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/entities/metadata/Version.java 2008-02-02 15:03:53 UTC (rev 578) @@ -1,5 +1,5 @@ /* -* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais +* ##% 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 @@ -24,43 +24,50 @@ /** * Pointed version class - * + * * @author glandais - * */ -public class Version implements Comparable<Version>, Serializable { +public class Version implements Comparable<Version>, Serializable, Cloneable { - private static final long serialVersionUID = 8982527036997002451L; + /** Inner storage */ + protected List<Integer> pointedVersion; + + private static final String EMPTY_VERSION = ""; + + private static final long serialVersionUID = 8931989369617566434L; + /** - * Inner storage + * Static public factory of Version from his string representation + * + * @param version the string representation of the version + * @return a new instance of version */ - private List<Integer> pointedVersion; + public static Version valueOf(String version) { + return new Version(version); + } /** * Default constructor - * - * @param version - * String version + * + * @param version String version */ public Version(String version) { - super(); // Initialize list pointedVersion = new ArrayList<Integer>(); if (version != null) { // tokenize on dot StringTokenizer st = new StringTokenizer(version, "."); while (st.hasMoreTokens()) { - pointedVersion.add(new Integer(st.nextToken())); + addSubVersion(Integer.valueOf(st.nextToken())); } } } /** * Retrieve sub version - * - * @param position - * Sub version wanted - * @return + * + * @param position Sub version wanted + * @return the sub version number */ public Integer getVersion(int position) { if (position < pointedVersion.size()) { @@ -72,11 +79,9 @@ /** * Set a sub version - * - * @param position - * Sub version to modify - * @param subVersion - * New value + * + * @param position Sub version to modify + * @param subVersion New value */ public void setVersion(int position, int subVersion) { while (getVersion(position) == null) { @@ -87,33 +92,34 @@ /** * Add a new sub version - * - * @param subVersion - * Sub version value + * + * @param subVersion Sub version value */ public void addSubVersion(int subVersion) { pointedVersion.add(subVersion); } /** - * Increment a sub version - * - * @param position - * Sub version position - * @return + * Clone current instance and Increment a sub version. + * <p/> + * Note : <b>The current instance is NOT modified</b> + * + * @param position Sub version position + * @return the new instance of version object updated */ public Version incVersion(int position) { - Version result = new Version(toString()); - while (result.getVersion(position) == null) { - result.addSubVersion(0); + Version result = safeClone(); + Integer oldValue = result.getVersion(position); + if (oldValue == null) { + oldValue = 0; } - result.setVersion(position, result.getVersion(position) + 1); + result.setVersion(position, oldValue + 1); return result; } /** * Length of version - * + * * @return Number of subversions */ public int getLength() { @@ -122,14 +128,14 @@ @Override public String toString() { + if (getLength() == 0) { + return EMPTY_VERSION; + } StringBuffer result = new StringBuffer(); for (Integer i : pointedVersion) { - result.append(i).append("."); + result.append('.').append(i); } - if (result.length() > 0) { - result.setLength(result.length() - 1); - } - return result.toString(); + return result.substring(1); } /* (non-Javadoc) @@ -163,4 +169,31 @@ return result; } -} + @Override + public Version clone() throws CloneNotSupportedException { + Version version = (Version) super.clone(); + // must defined a new instance of list(otherwise it will use the same + // instance than the original Version instance, and we don not want this + version.pointedVersion=new ArrayList<Integer>(pointedVersion); + return version; + } + + @Override + public boolean equals(Object o) { + return this == o || (o instanceof Version && + pointedVersion.equals(((Version) o).pointedVersion)); + } + + @Override + public int hashCode() { + return pointedVersion.hashCode(); + } + + public Version safeClone() throws RuntimeException { + try { + return clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file