1 11 package org.eclipse.team.internal.ccvs.core; 12 13 14 import java.text.ParseException ; 15 import java.text.SimpleDateFormat ; 16 import java.util.*; 17 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.team.core.history.ITag; 20 21 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); 35 protected String name; 36 protected int type; 37 38 private static final String DATE_TAG_NAME_FORMAT = "dd MMM yyyy HH:mm:ss Z"; private static final SimpleDateFormat tagNameFormat = new SimpleDateFormat (DATE_TAG_NAME_FORMAT, Locale.US); 40 protected static synchronized String dateToTagName(Date date){ 41 tagNameFormat.setTimeZone(TimeZone.getTimeZone("GMT")); return tagNameFormat.format(date); 43 } 44 protected static synchronized Date tagNameToDate(String name){ 45 if (name == null) return null; 46 try { 47 return tagNameFormat.parse(name); 48 } catch (ParseException e) { 49 IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR,"Tag name " + name + " is not of the expected format " + DATE_TAG_NAME_FORMAT,e ); CVSProviderPlugin.log(new CVSException(status)); 51 return null; 52 } 53 } 54 55 public CVSTag() { 56 this("HEAD", HEAD); } 58 59 public CVSTag(String name, int type) { 60 this.name = name; 61 this.type = type; 62 } 63 public CVSTag(Date date) { 65 this(dateToTagName(date), DATE); 66 } 67 68 public boolean equals(Object 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 getName() { 79 return name; 80 } 81 82 public int getType() { 83 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 tagName) { 108 if (tagName == null) 109 return new CVSStatus(IStatus.ERROR, CVSMessages.CVSTag_nullName); 110 if (tagName.equals("")) 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 128 public Date asDate(){ 129 return tagNameToDate(name); 130 } 131 132 } 133 | Popular Tags |