01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.wiki;
04:
05: import java.util.regex.*;
06: import java.util.*;
07: import java.io.Serializable;
08: import java.text.*;
09:
10: public class VersionInfo implements Comparable, Serializable {
11:
12: private static final long serialVersionUID = 1L;
13:
14: public static final Pattern COMPEX_NAME_PATTERN = Pattern
15: .compile("(?:([a-zA-Z][^\\-]*)-)?(?:\\d+-)?(\\d{14})");
16:
17: private static int counter = 0;
18:
19: public static SimpleDateFormat makeVersionTimeFormat() {
20: // SimpleDateFormat is not thread safe, so we need to create each
21: // instance independently.
22: return new SimpleDateFormat("yyyyMMddHHmmss");
23: }
24:
25: public static int nextId() {
26: return counter++;
27: }
28:
29: private String name;
30:
31: private String author;
32:
33: private Date creationTime;
34:
35: public VersionInfo(String name, String author, Date creationTime) {
36: this .name = name;
37: this .author = author;
38: this .creationTime = creationTime;
39: }
40:
41: public VersionInfo(String complexName) throws Exception {
42: this (complexName, "", new Date());
43: Matcher match = COMPEX_NAME_PATTERN.matcher(complexName);
44: if (match.find()) {
45: author = match.group(1);
46: if (author == null)
47: author = "";
48: creationTime = makeVersionTimeFormat()
49: .parse(match.group(2));
50: }
51: }
52:
53: public String getAuthor() {
54: return author;
55: }
56:
57: public Date getCreationTime() {
58: return creationTime;
59: }
60:
61: public String getName() {
62: return name;
63: }
64:
65: public static String getVersionNumber(String complexName) {
66: Matcher match = COMPEX_NAME_PATTERN.matcher(complexName);
67: match.find();
68: return match.group(2);
69: }
70:
71: public int compareTo(Object o) {
72: VersionInfo otherVersion;
73: if (o instanceof VersionInfo) {
74: otherVersion = ((VersionInfo) o);
75: return getCreationTime().compareTo(
76: otherVersion.getCreationTime());
77: } else
78: return 0;
79: }
80:
81: public String toString() {
82: return getName();
83: }
84:
85: public boolean equals(Object o) {
86: if (o != null && o instanceof VersionInfo) {
87: VersionInfo otherVersion = (VersionInfo) o;
88: return getName().equals(otherVersion.getName());
89: } else
90: return false;
91: }
92:
93: public int hashCode() {
94: return getName().hashCode();
95: }
96: }
|