feisty meow concerns codebase  2.140
Version.java
Go to the documentation of this file.
1 package org.gffs.version;
2 
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.util.Properties;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
8 
9 public class Version implements Comparable<Version>
10 {
11  static final private Pattern VERSION_PATTERN = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)\\s+Build\\s+(\\d+)$");
12 
13  private int _majorVersion;
14  private int _minorVersion;
15  private int _subMinorVersion;
16  private int _buildNumber;
17 
18  public Version(int major, int minor, int subminor, int buildNumber)
19  {
20  _majorVersion = major;
21  _minorVersion = minor;
22  _subMinorVersion = subminor;
23  _buildNumber = buildNumber;
24  }
25 
26  public Version(String str)
27  {
28  parseString(str);
29  }
30 
31  public void parseString(String verString)
32  {
33  Matcher matcher = VERSION_PATTERN.matcher(verString);
34  if (!matcher.matches())
35  throw new IllegalArgumentException("Version string wasn't of the form ###.###.### Build ####");
36 
37  _majorVersion = Integer.parseInt(matcher.group(1));
38  _minorVersion = Integer.parseInt(matcher.group(2));
39  _subMinorVersion = Integer.parseInt(matcher.group(3));
40  _buildNumber = Integer.parseInt(matcher.group(4));
41  }
42 
43  public Version(File propFile)
44  {
45  Properties props = new Properties();
46  try {
47  FileInputStream fis = new FileInputStream(propFile);
48  props.load(fis);
49  String appVer = props.getProperty("genii.app-version");
50  String buildNum = props.getProperty("genii.build-number");
51  String formattedString = appVer + " Build " + buildNum;
52  parseString(formattedString);
53  } catch (Throwable t) {
54  throw new RuntimeException("failure to parse version from file: " + propFile, t);
55  }
56  }
57 
58  public boolean equals(Version other)
59  {
60  return compareTo(other) == 0;
61  }
62 
63  @Override
64  public boolean equals(Object other)
65  {
66  if (other instanceof Version)
67  return equals((Version) other);
68 
69  return false;
70  }
71 
72  @Override
73  public int hashCode()
74  {
75  return (_majorVersion << 3) ^ (_minorVersion << 2) ^ (_subMinorVersion << 1) ^ _buildNumber;
76  }
77 
78  @Override
79  public String toString()
80  {
81  return String.format("%d.%d.%d Build %d", _majorVersion, _minorVersion, _subMinorVersion, _buildNumber);
82  }
83 
84  @Override
85  public int compareTo(Version o)
86  {
87  int diff = _majorVersion - o._majorVersion;
88  if (diff != 0)
89  return diff;
90  diff = _minorVersion - o._minorVersion;
91  if (diff != 0)
92  return diff;
93  diff = _subMinorVersion - o._subMinorVersion;
94  if (diff != 0)
95  return diff;
96  return _buildNumber - o._buildNumber;
97  }
98 
99  static public Version EMPTY_VERSION = new Version(0, 0, 0, 0);
100 }
int compareTo(Version o)
Definition: Version.java:85
static Version EMPTY_VERSION
Definition: Version.java:99
boolean equals(Version other)
Definition: Version.java:58
boolean equals(Object other)
Definition: Version.java:64
Version(File propFile)
Definition: Version.java:43
Version(int major, int minor, int subminor, int buildNumber)
Definition: Version.java:18
void parseString(String verString)
Definition: Version.java:31