View Javadoc
1   /*
2    * This file is a part of the SchemaSpy project (http://schemaspy.sourceforge.net).
3    * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 John Currier
4    *
5    * SchemaSpy is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * SchemaSpy is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package net.sourceforge.schemaspy.util;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.StringTokenizer;
24  
25  /**
26   * Simple class that allows logical comparisons between "dotted" versions of products.
27   *
28   * e.g. version 2.1.4 should be less than version 2.1.10.
29   *
30   * @author John Currier
31   * @version 1.0
32   */
33  public class Version implements Comparable<Version> {
34      private final List<Integer> segments = new ArrayList<Integer>();
35      private final String asString;
36      private final int hashCode;
37  
38      public Version(String version) {
39          asString = version;
40          int hash = 0;
41          if (version != null) {
42              StringTokenizer tokenizer = new StringTokenizer(version, ". -_");
43  
44              while (tokenizer.hasMoreTokens()) {
45                  Integer segment = new Integer(tokenizer.nextToken());
46                  segments.add(segment);
47                  hash += segment.intValue();
48              }
49          }
50  
51          hashCode = hash;
52      }
53  
54      /**
55       * Compares this object with the specified object for order.  Returns a
56       * negative integer, zero, or a positive integer as this object is less
57       * than, equal to, or greater than the specified object.
58       */
59      public int compareTo(Version other) {
60          int size = Math.min(segments.size(), other.segments.size());
61          for (int i = 0; i < size; ++i) {
62              Integer thisSegment = segments.get(i);
63              Integer otherSegment = other.segments.get(i);
64              int result = thisSegment.compareTo(otherSegment);
65              if (result != 0)
66                  return result;
67          }
68  
69          if (segments.size() == other.segments.size())
70              return 0;
71          if (segments.size() > other.segments.size())
72              return 1;
73          return -1;
74      }
75  
76      @Override
77      public boolean equals(Object other) {
78          if (other == null || !(other instanceof Version))
79              return false;
80          return compareTo((Version)other) == 0;
81      }
82  
83      @Override
84      public int hashCode() {
85          return hashCode;
86      }
87  
88      @Override
89      public String toString() {
90          return asString;
91      }
92  }