1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
2 | * |
3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. |
4 | * |
5 | * "Hilbert II" is free software; you can redistribute |
6 | * it and/or modify it under the terms of the GNU General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This program 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 |
13 | * GNU General Public License for more details. |
14 | */ |
15 | |
16 | package org.qedeq.base.io; |
17 | |
18 | import java.util.Iterator; |
19 | import java.util.Set; |
20 | import java.util.TreeSet; |
21 | |
22 | |
23 | /** |
24 | * A set of version numbers. |
25 | * |
26 | * @author Michael Meyling |
27 | */ |
28 | public final class VersionSet { |
29 | |
30 | /** Set that contains our versions. */ |
31 | private final Set set; |
32 | |
33 | /** |
34 | * Constructs version set object. You must give a version string in a form like |
35 | * <em>a</em>.<em>b</em>.<em>c</em> where a, b and c are non negative integers. |
36 | * These numbers are called <em>Major</em> <em>Minor</em> and <em>Patch</em>. |
37 | * |
38 | * @param version Version string. |
39 | * @throws IllegalArgumentException Version string has wrong format. |
40 | * @throws NullPointerException No null pointer as argument accepted. |
41 | */ |
42 | public VersionSet(final String version) { |
43 | set = new TreeSet(); |
44 | add(version); |
45 | } |
46 | |
47 | /** |
48 | * Constructs version set object. |
49 | */ |
50 | public VersionSet() { |
51 | set = new TreeSet(); |
52 | } |
53 | |
54 | /** |
55 | * Does the set contain given version number. |
56 | * |
57 | * @param version Version string. If it has not the correct form, false is returned. |
58 | * @return Is given version in set? |
59 | */ |
60 | public boolean contains(final String version) { |
61 | Version v = null; |
62 | try { |
63 | v = new Version(version); |
64 | } catch (RuntimeException e) { |
65 | return false; |
66 | } |
67 | return set.contains(v); |
68 | } |
69 | |
70 | /** |
71 | * Does the set contain given version number. |
72 | * |
73 | * @param version Version to check for. |
74 | * @return Is given version in set? |
75 | */ |
76 | public boolean contains(final Version version) { |
77 | return set.contains(version); |
78 | } |
79 | |
80 | /** |
81 | * Add version number. |
82 | * |
83 | * @param version Version string. |
84 | * @throws IllegalArgumentException Version string has wrong format. |
85 | * @throws NullPointerException No null pointer as argument accepted. |
86 | */ |
87 | public void add(final String version) { |
88 | add(new Version(version)); |
89 | } |
90 | |
91 | /** |
92 | * Add version number. |
93 | * |
94 | * @param version Version string. |
95 | */ |
96 | public void add(final Version version) { |
97 | set.add(version); |
98 | } |
99 | |
100 | /** |
101 | * Add contents of another version set. |
102 | * |
103 | * @param versions Other version set. |
104 | * @return Did current set change? |
105 | */ |
106 | public boolean addAll(final VersionSet versions) { |
107 | return set.addAll(versions.set); |
108 | } |
109 | |
110 | /** |
111 | * Is this set empty? |
112 | * |
113 | * @return Is set empty? |
114 | */ |
115 | public boolean isEmpty() { |
116 | return set.isEmpty(); |
117 | } |
118 | |
119 | /** |
120 | * Removes all of the elements from this set. |
121 | */ |
122 | public void clear() { |
123 | set.clear(); |
124 | } |
125 | |
126 | /** |
127 | * Returns an iterator over the elements in this set. The elements are |
128 | * returned in ascending order. |
129 | * |
130 | * @return Iterator. |
131 | */ |
132 | public Iterator iterator() { |
133 | return set.iterator(); |
134 | } |
135 | |
136 | public int hashCode() { |
137 | return set.hashCode(); |
138 | } |
139 | |
140 | public boolean equals(final Object o) { |
141 | if (!(o instanceof VersionSet)) { |
142 | return false; |
143 | } |
144 | return set.equals(((VersionSet) o).set); |
145 | } |
146 | |
147 | public String toString() { |
148 | final StringBuffer buffer = new StringBuffer(30); |
149 | buffer.append("{"); |
150 | Iterator e = set.iterator(); |
151 | boolean notFirst = false; |
152 | while (e.hasNext()) { |
153 | if (notFirst) { |
154 | buffer.append(", "); |
155 | } else { |
156 | notFirst = true; |
157 | } |
158 | buffer.append(String.valueOf(e.next())); |
159 | } |
160 | buffer.append("}"); |
161 | return buffer.toString(); |
162 | } |
163 | |
164 | } |