VersionSet.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.base.io;
017 
018 import java.util.Iterator;
019 import java.util.Set;
020 import java.util.TreeSet;
021 
022 
023 /**
024  * A set of version numbers.
025  *
026  @author  Michael Meyling
027  */
028 public final class VersionSet {
029 
030     /** Set that contains our versions. */
031     private final Set set;
032 
033     /**
034      * Constructs version set object. You must give a version string in a form like
035      *  <em>a</em>.<em>b</em>.<em>c</em> where a, b and c are non negative integers.
036      * These numbers are called <em>Major</em> <em>Minor</em> and <em>Patch</em>.
037      *
038      @param   version     Version string.
039      @throws  IllegalArgumentException    Version string has wrong format.
040      @throws  NullPointerException        No null pointer as argument accepted.
041      */
042     public VersionSet(final String version) {
043         set = new TreeSet();
044         add(version);
045     }
046 
047     /**
048      * Constructs version set object.
049      */
050     public VersionSet() {
051         set = new TreeSet();
052     }
053 
054     /**
055      * Does the set contain given version number.
056      *
057      @param   version Version string. If it has not the correct form, false is returned.
058      @return  Is given version in set?
059      */
060     public boolean contains(final String version) {
061         Version v = null;
062         try {
063             v = new Version(version);
064         catch (RuntimeException e) {
065             return false;
066         }
067         return set.contains(v);
068     }
069 
070     /**
071      * Does the set contain given version number.
072      *
073      @param   version Version to check for.
074      @return  Is given version in set?
075      */
076     public boolean contains(final Version version) {
077         return set.contains(version);
078     }
079 
080     /**
081      * Add version number.
082      *
083      @param   version     Version string.
084      @throws  IllegalArgumentException    Version string has wrong format.
085      @throws  NullPointerException        No null pointer as argument accepted.
086      */
087     public void add(final String version) {
088         add(new Version(version));
089     }
090 
091     /**
092      * Add version number.
093      *
094      @param   version     Version string.
095      */
096     public void add(final Version version) {
097         set.add(version);
098     }
099 
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 (!(instanceof VersionSet)) {
142             return false;
143         }
144         return set.equals(((VersionSeto).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 }