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: public static final Pattern COMPEX_NAME_PATTERN = Pattern
12: .compile("(?:([a-zA-Z][^\\-]*)-)?(?:\\d+-)?(\\d{14})");
13: private static int counter = 0;
14:
15: public static SimpleDateFormat makeVersionTimeFormat() {
16: //SimpleDateFormat is not thread safe, so we need to create each instance independently.
17: return new SimpleDateFormat("yyyyMMddHHmmss");
18: }
19:
20: public static int nextId() {
21: return counter++;
22: }
23:
24: private String name;
25: private String author;
26: private Date creationTime;
27:
28: public VersionInfo(String name, String author, Date creationTime) {
29: this .name = name;
30: this .author = author;
31: this .creationTime = creationTime;
32: }
33:
34: public VersionInfo(String complexName) throws Exception {
35: this (complexName, "", new Date());
36: Matcher match = COMPEX_NAME_PATTERN.matcher(complexName);
37: if (match.find()) {
38: author = match.group(1);
39: if (author == null)
40: author = "";
41: creationTime = makeVersionTimeFormat()
42: .parse(match.group(2));
43: }
44: }
45:
46: public String getAuthor() {
47: return author;
48: }
49:
50: public Date getCreationTime() {
51: return creationTime;
52: }
53:
54: public String getName() {
55: return name;
56: }
57:
58: public static String getVersionNumber(String complexName) {
59: Matcher match = COMPEX_NAME_PATTERN.matcher(complexName);
60: match.find();
61: return match.group(2);
62: }
63:
64: public int compareTo(Object o) {
65: VersionInfo otherVersion;
66: if (o instanceof VersionInfo) {
67: otherVersion = ((VersionInfo) o);
68: return getCreationTime().compareTo(
69: otherVersion.getCreationTime());
70: } else
71: return 0;
72: }
73:
74: public String toString() {
75: return getName();
76: }
77:
78: public boolean equals(Object o) {
79: if (o != null && o instanceof VersionInfo) {
80: VersionInfo otherVersion = (VersionInfo) o;
81: return getName().equals(otherVersion.getName());
82: } else
83: return false;
84: }
85:
86: public int hashCode() {
87: return getName().hashCode();
88: }
89: }
|