001: /**
002: * @copyright
003: * ====================================================================
004: * Copyright (c) 2003-2004 CollabNet. All rights reserved.
005: *
006: * This software is licensed as described in the file COPYING, which
007: * you should have received as part of this distribution. The terms
008: * are also available at http://subversion.tigris.org/license-1.html.
009: * If newer versions of this license are posted there, you may use a
010: * newer version instead, at your option.
011: *
012: * This software consists of voluntary contributions made by many
013: * individuals. For exact contribution history, see the revision
014: * history and logs, available at http://subversion.tigris.org/.
015: * ====================================================================
016: * @endcopyright
017: */package org.tigris.subversion.javahl;
018:
019: import java.text.SimpleDateFormat;
020: import java.util.Date;
021: import java.util.Locale;
022:
023: /**
024: * Class to specify a revision in a svn command.
025: */
026: public class Revision {
027: /**
028: * kind of revision specified
029: */
030: protected int revKind;
031:
032: /**
033: * Create a new revision
034: * @deprecated
035: * @param kind kind of revision
036: */
037: public Revision(int kind) {
038: if (kind < RevisionKind.unspecified || kind > RevisionKind.head)
039: throw new IllegalArgumentException(kind
040: + " is not a legel revision kind");
041: revKind = kind;
042: }
043:
044: /**
045: * Internally create a new revision
046: * @param kind kind of revision
047: * @param marker marker to differtiate from the public deprecated version
048: */
049: protected Revision(int kind, boolean marker) {
050: if (kind < RevisionKind.unspecified || kind > RevisionKind.head)
051: throw new IllegalArgumentException(kind
052: + " is not a legel revision kind");
053: revKind = kind;
054: }
055:
056: /**
057: * Returns the kind of the Revsion
058: * @return kind
059: */
060: public int getKind() {
061: return revKind;
062: }
063:
064: /**
065: * return the textual representation of the revision
066: * @return english text
067: */
068: public String toString() {
069: switch (revKind) {
070: case Kind.base:
071: return "BASE";
072: case Kind.committed:
073: return "COMMITTED";
074: case Kind.head:
075: return "HEAD";
076: case Kind.previous:
077: return "PREV";
078: case Kind.working:
079: return "WORKING";
080: }
081: return super .toString();
082: }
083:
084: /**
085: * compare to revision objects
086: * @param target
087: * @return if both object have equal content
088: */
089: public boolean equals(Object target) {
090: if (this == target)
091: return true;
092: if (!(target instanceof Revision))
093: return false;
094:
095: return ((Revision) target).revKind == revKind;
096: }
097:
098: /**
099: * Creates a Revision.Number object
100: * @param revisionNumber the revision number of the new object
101: * @return the new object
102: * @throws IllegalArgumentException If the specified revision
103: * number is invalid.
104: */
105: public static Revision getInstance(long revisionNumber) {
106: return new Revision.Number(revisionNumber);
107: }
108:
109: /**
110: * Factory which creates {@link #Number} objects for valid
111: * revision numbers only (those greater than zero). For internal
112: * usage to avoid an IllegalArgumentException, where no external
113: * consumer of the javahl API passed an invalid revision number.
114: *
115: * @param revNumber The revision number to create an object for.
116: * @return An object representing <code>revNumber</code>, or
117: * <code>null</code> if the revision number was invalid.
118: * @since 1.2
119: */
120: static Number createNumber(long revNumber) {
121: return (revNumber < 0 ? null : new Number(revNumber));
122: }
123:
124: /**
125: * Creates a Revision.DateSpec objet
126: * @param revisionDate the date of the new object
127: * @return the new object
128: */
129: public static Revision getInstance(Date revisionDate) {
130: return new Revision.DateSpec(revisionDate);
131: }
132:
133: /**
134: * last commited revision
135: */
136: public static final Revision HEAD = new Revision(Kind.head, true);
137: /**
138: * first existing revision
139: */
140: public static final Revision START = new Revision(Kind.unspecified,
141: true);
142: /**
143: * last committed revision, needs working copy
144: */
145: public static final Revision COMMITTED = new Revision(
146: Kind.committed, true);
147: /**
148: * previous committed revision, needs working copy
149: */
150: public static final Revision PREVIOUS = new Revision(Kind.previous,
151: true);
152: /**
153: * base revision of working copy
154: */
155: public static final Revision BASE = new Revision(Kind.base, true);
156: /**
157: * working version in working copy
158: */
159: public static final Revision WORKING = new Revision(Kind.working,
160: true);
161: /**
162: * Marker revision number for no real revision
163: */
164: public static final int SVN_INVALID_REVNUM = -1;
165:
166: /**
167: * class to specify a Revision by number
168: */
169: public static class Number extends Revision {
170: /**
171: * the revision number
172: */
173: protected long revNumber;
174:
175: /**
176: * create a revision by number object
177: * @param number the number
178: * @throws IllegalArgumentException If the specified revision
179: * number is invalid.
180: */
181: public Number(long number) {
182: super (Kind.number, true);
183: if (number < 0)
184: throw new IllegalArgumentException(
185: "negative revision numbers are not allowed");
186: revNumber = number;
187: }
188:
189: /**
190: * Returns the revision number
191: * @return number
192: */
193: public long getNumber() {
194: return revNumber;
195: }
196:
197: /**
198: * return the textual representation of the revision
199: * @return english text
200: */
201: public String toString() {
202: return Long.toString(revNumber);
203: }
204:
205: /**
206: * compare to revision objects
207: * @param target
208: * @return if both object have equal content
209: */
210: public boolean equals(Object target) {
211: if (!super .equals(target))
212: return false;
213:
214: return ((Revision.Number) target).revNumber == revNumber;
215: }
216: }
217:
218: /**
219: * class to specify a revision by a date
220: */
221: public static class DateSpec extends Revision {
222: /**
223: * the date
224: */
225: protected Date revDate;
226:
227: /**
228: * create a revision by date object
229: * @param date
230: */
231: public DateSpec(Date date) {
232: super (Kind.date, true);
233: if (date == null)
234: throw new IllegalArgumentException(
235: "a date must be specified");
236: revDate = date;
237: }
238:
239: /**
240: * Returns the date of the revision
241: * @return the date
242: */
243: public Date getDate() {
244: return revDate;
245: }
246:
247: /**
248: * return the textual representation of the revision
249: * @return english text
250: */
251: public String toString() {
252:
253: SimpleDateFormat dateFormat = new SimpleDateFormat(
254: "EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
255: return '{' + dateFormat.format(revDate) + '}';
256: }
257:
258: /**
259: * compare to revision objects
260: * @param target
261: * @return if both object have equal content
262: */
263: public boolean equals(Object target) {
264: if (!super .equals(target))
265: return false;
266:
267: return ((Revision.DateSpec) target).revDate.equals(revDate);
268: }
269:
270: }
271:
272: /**
273: * Various ways of specifying revisions.
274: *
275: * Note:
276: * In contexts where local mods are relevant, the `working' kind
277: * refers to the uncommitted "working" revision, which may be modified
278: * with respect to its base revision. In other contexts, `working'
279: * should behave the same as `committed' or `current'.
280: *
281: * the values are defined in RevisionKind because of building reasons
282: */
283: public static final class Kind implements RevisionKind {
284: }
285: }
|