KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > CVSTag


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core;
12
13
14 import java.text.ParseException JavaDoc;
15 import java.text.SimpleDateFormat JavaDoc;
16 import java.util.*;
17
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.team.core.history.ITag;
20
21 /**
22  * A tag in CVS gives a label to a collection of revisions. The labels can represent a version, a branch,
23  * or a date.
24  */

25 public class CVSTag implements ITag {
26
27     public final static int HEAD = 0;
28     public final static int BRANCH = 1;
29     public final static int VERSION = 2;
30     public final static int DATE = 3;
31     
32     public static final CVSTag DEFAULT = new CVSTag();
33     public static final CVSTag BASE = new CVSTag("BASE", VERSION); //$NON-NLS-1$
34

35     protected String JavaDoc name;
36     protected int type;
37     
38     private static final String JavaDoc DATE_TAG_NAME_FORMAT = "dd MMM yyyy HH:mm:ss Z";//$NON-NLS-1$
39
private static final SimpleDateFormat JavaDoc tagNameFormat = new SimpleDateFormat JavaDoc(DATE_TAG_NAME_FORMAT, Locale.US);
40     protected static synchronized String JavaDoc dateToTagName(Date date){
41         tagNameFormat.setTimeZone(TimeZone.getTimeZone("GMT"));//$NON-NLS-1$
42
return tagNameFormat.format(date);
43     }
44     protected static synchronized Date tagNameToDate(String JavaDoc name){
45         if (name == null) return null;
46         try {
47             return tagNameFormat.parse(name);
48         } catch (ParseException JavaDoc e) {
49             IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR,"Tag name " + name + " is not of the expected format " + DATE_TAG_NAME_FORMAT,e ); //$NON-NLS-1$ //$NON-NLS-2$
50
CVSProviderPlugin.log(new CVSException(status));
51             return null;
52         }
53     }
54     
55     public CVSTag() {
56         this("HEAD", HEAD); //$NON-NLS-1$
57
}
58
59     public CVSTag(String JavaDoc name, int type) {
60         this.name = name;
61         this.type = type;
62     }
63     //Write a date in local date tag format
64
public CVSTag(Date date) {
65         this(dateToTagName(date), DATE);
66     }
67
68     public boolean equals(Object JavaDoc other) {
69         if(other == this) return true;
70         if (!(other instanceof CVSTag)) return false;
71             
72         CVSTag tag = ((CVSTag)other);
73         if (getType() != tag.getType()) return false;
74         if (!getName().equals(tag.getName())) return false;
75         return true;
76     }
77     
78     public String JavaDoc getName() {
79         return name;
80     }
81
82     public int getType() {
83         // TODO: getType() will not return accurate types for Tags retrieved from the local CVS Entries file. See Bug: 36758
84
return type;
85     }
86     
87     public int hashCode() {
88         return name.hashCode();
89     }
90     
91     public int compareTo(CVSTag other) {
92         if(getType() == DATE && other.getType()== DATE){
93             Date date1 = asDate();
94             Date date2 = other.asDate();
95             if(date1 == null || date2 == null)return -1;
96             return date1.compareTo(date2);
97         }
98         return getName().compareToIgnoreCase(other.getName());
99     }
100     
101     public static boolean equalTags(CVSTag tag1, CVSTag tag2) {
102         if (tag1 == null) tag1 = CVSTag.DEFAULT;
103         if (tag2 == null) tag2 = CVSTag.DEFAULT;
104         return tag1.equals(tag2);
105     }
106     
107     public static IStatus validateTagName(String JavaDoc tagName) {
108         if (tagName == null)
109             return new CVSStatus(IStatus.ERROR, CVSMessages.CVSTag_nullName);
110         if (tagName.equals("")) //$NON-NLS-1$
111
return new CVSStatus(IStatus.ERROR, CVSMessages.CVSTag_emptyName);
112         if (!Character. isLetter(tagName.charAt(0)))
113             return new CVSStatus(IStatus.ERROR, CVSMessages.CVSTag_beginName);
114         
115         for (int i = 0; i < tagName.length(); i++) {
116             char c = tagName.charAt(i);
117             if ( Character.isSpaceChar(c) || c == '$' || c == ',' || c == '.' || c == ':' || c == ';' || c == '@' || c == '|')
118                 return new CVSStatus(IStatus.ERROR, CVSMessages.CVSTag_badCharName);
119         }
120         return new CVSStatus(IStatus.OK, CVSMessages.ok);
121     }
122     
123     /**
124      * Return the date this tag represents or <code>null</code>
125      * if the tag is not of type DATE.
126      * @return the date of the tag or <code>null</code>
127      */

128     public Date asDate(){
129         return tagNameToDate(name);
130     }
131
132 }
133
Popular Tags