View Javadoc

1   /*
2    *  StatCvs-XML - XML output for StatCvs.
3    *
4    *  Copyright by Steffen Pingel, Tammo van Lessen.
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU General Public License
8    *  version 2 as published by the Free Software Foundation.
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   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  package net.sf.statcvs.model;
20  
21  import java.util.Date;
22  import java.util.SortedSet;
23  import java.util.TreeSet;
24  
25  /**
26   * Represents a symbolic name (tags).
27   * It is a container for {@link Revision}s.
28   * 
29   * @author Tammo van Lessen
30   * @version $Id: SymbolicName.java,v 1.6 2009/08/20 17:44:05 benoitx Exp $
31   */
32  public class SymbolicName implements Comparable {
33      private final String name;
34      private final SortedSet revisions = new TreeSet();
35      private Date date = null;
36  
37      /**
38       * Creates a new symbolic name.
39       * 
40       * @param name the symbolic name's name
41       * @param date of the symbolic name creation
42       */
43      public SymbolicName(final String name, final Date date) {
44          this.name = name;
45          this.date = date;
46      }
47  
48      /**
49       * Creates a new symbolic name. With this constructor,
50       * its creation date will be assumed to be the date
51       * of its latest revision.
52       * 
53       * @param name the symbolic name's name
54       */
55      public SymbolicName(final String name) {
56          this.name = name;
57      }
58  
59      /**
60       * Returns the symbolic name's name.
61       * 
62       * @return the symbolic name's name.
63       */
64      public String getName() {
65          return name;
66      }
67  
68      /**
69       * Adds a revision to this symbolic name.
70       * 
71       * @param rev the revision
72       */
73      protected void addRevision(final Revision rev) {
74          revisions.add(rev);
75      }
76  
77      /**
78       * Returns a set of {@link Revision}s contained in this symbolic name.
79       * 
80       * @return the revisions
81       */
82      public SortedSet getRevisions() {
83          return revisions;
84      }
85  
86      /**
87       * Returns the 'date' of this symbolic name.
88       *  
89       * @return the smbolic name's date
90       */
91      public Date getDate() {
92          if (date != null) {
93              return date;
94          }
95          if (revisions.isEmpty()) {
96              return null;
97          }
98          return ((Revision) revisions.last()).getDate();
99      }
100 
101     /**
102      * @see java.lang.Comparable#compareTo(java.lang.Object)
103      */
104     public int compareTo(final Object o) {
105         final SymbolicName other = (SymbolicName) o;
106         if (getDate() == null || other.getDate() == null) {
107             return getName().compareTo(other.getName());
108         }
109         final int dateComp = getDate().compareTo(other.getDate());
110         return (dateComp != 0) ? dateComp : getName().compareTo(other.getName());
111     }
112 
113     /**
114      * @see java.lang.Object#toString()
115      */
116     public String toString() {
117         return name + "[" + getDate() + "] (" + revisions.size() + " revisions)";
118     }
119 
120     /**
121      * @see java.lang.Object#equals(java.lang.Object)
122      */
123     public boolean equals(final Object obj) {
124         if (obj instanceof SymbolicName) {
125             final SymbolicName other = (SymbolicName) obj;
126             return ("" + getDate() + getName()).equals("" + other.getDate() + other.getName());
127         }
128         return false;
129     }
130 
131     /**
132      * @see java.lang.Object#hashCode()
133      */
134     public int hashCode() {
135         return ("" + getDate() + getName()).hashCode();
136     }
137 }