Java Source Code: org.tigris.subversion.javahl.Revision


   1: /**
   2:  * @copyright
   3:  * ====================================================================
   4:  * Copyright (c) 2003-2004 CollabNet.  All rights reserved.
   5:  *
   6:  * This software is licensed as described in the file COPYING, which
   7:  * you should have received as part of this distribution.  The terms
   8:  * are also available at http://subversion.tigris.org/license-1.html.
   9:  * If newer versions of this license are posted there, you may use a
  10:  * newer version instead, at your option.
  11:  *
  12:  * This software consists of voluntary contributions made by many
  13:  * individuals.  For exact contribution history, see the revision
  14:  * history and logs, available at http://subversion.tigris.org/.
  15:  * ====================================================================
  16:  * @endcopyright
  17:  */
  18: package org.tigris.subversion.javahl;
  19: 
  20: import java.text.SimpleDateFormat;
  21: import java.util.Date;
  22: import java.util.Locale;
  23: 
  24: /**
  25:  * Class to specify a revision in a svn command.
  26:  */
  27: public class Revision
  28:	  {
  29:    /**
  30:     * kind of revision specified
  31:     */
  32:    protected int revKind;
  33:
  34:    /**
  35:     * Create a new revision
  36:     * @deprecated
  37:     * @param kind  kind of revision
  38:     */
  39:    public Revision(int kind)
  40:	      {
  41:        if(kind < RevisionKind.unspecified || kind > RevisionKind.head)
  42:            throw new IllegalArgumentException(
  43:                    kind+" is not a legel revision kind");
  44:        revKind = kind;
  45:    }
  46:
  47:    /**
  48:     * Internally create a new revision
  49:     * @param kind      kind of revision
  50:     * @param marker    marker to differtiate from the public deprecated version
  51:     */
  52:    protected Revision(int kind, boolean marker)
  53:	      {
  54:        if(kind < RevisionKind.unspecified || kind > RevisionKind.head)
  55:            throw new IllegalArgumentException(
  56:                    kind+" is not a legel revision kind");
  57:        revKind = kind;
  58:    }
  59:
  60:    /**
  61:     * Returns the kind of the Revsion
  62:     * @return kind
  63:     */
  64:    public int getKind()
  65:	      {
  66:        return revKind;
  67:    }
  68:
  69:    /**
  70:     * return the textual representation of the revision
  71:     * @return english text
  72:     */
  73:    public String toString()
  74:	      {
  75:	          switch(revKind) {
  76:            case Kind.base : return "BASE";
  77:            case Kind.committed : return "COMMITTED";
  78:            case Kind.head : return "HEAD";
  79:            case Kind.previous : return "PREV";
  80:            case Kind.working : return "WORKING";
  81:        }
  82:        return super.toString();
  83:    }
  84:
  85:    /**
  86:     * compare to revision objects
  87:     * @param target
  88:     * @return if both object have equal content
  89:     */
  90:	      public boolean equals(Object target) {
  91:        if (this == target)
  92:            return true;
  93:        if (!(target instanceof Revision))
  94:            return false;
  95:
  96:        return ((Revision)target).revKind == revKind;        
  97:    }
  98:
  99:    /**
 100:     * Creates a Revision.Number object
 101:     * @param revisionNumber    the revision number of the new object
 102:     * @return  the new object
 103:     * @throws IllegalArgumentException If the specified revision
 104:     * number is invalid.
 105:     */
 106:    public static Revision getInstance(long revisionNumber)
 107:	      {
 108:        return new Revision.Number(revisionNumber);
 109:    }
 110:
 111:    /**
 112:     * Factory which creates {@link #Number} objects for valid
 113:     * revision numbers only (those greater than zero).  For internal
 114:     * usage to avoid an IllegalArgumentException, where no external
 115:     * consumer of the javahl API passed an invalid revision number.
 116:     *
 117:     * @param revNumber The revision number to create an object for.
 118:     * @return An object representing <code>revNumber</code>, or
 119:     * <code>null</code> if the revision number was invalid.
 120:     * @since 1.2
 121:     */
 122:    static Number createNumber(long revNumber)
 123:	      {
 124:        return (revNumber < 0 ? null : new Number(revNumber));
 125:    }
 126:
 127:    /**
 128:     * Creates a Revision.DateSpec objet
 129:     * @param revisionDate  the date of the new object
 130:     * @return  the new object
 131:     */
 132:    public static Revision getInstance(Date revisionDate)
 133:	      {
 134:        return new Revision.DateSpec(revisionDate);
 135:    }
 136:
 137:    /**
 138:     * last commited revision
 139:     */
 140:    public static final Revision HEAD = new Revision(Kind.head, true);
 141:    /**
 142:     * first existing revision
 143:     */
 144:    public static final Revision START = new Revision(Kind.unspecified, true);
 145:    /**
 146:     * last committed revision, needs working copy
 147:     */
 148:    public static final Revision COMMITTED = new Revision(Kind.committed, true);
 149:    /**
 150:     * previous committed revision, needs working copy
 151:     */
 152:    public static final Revision PREVIOUS = new Revision(Kind.previous, true);
 153:    /**
 154:     * base revision of working copy
 155:     */
 156:    public static final Revision BASE = new Revision(Kind.base, true);
 157:    /**
 158:     * working version in working copy
 159:     */
 160:    public static final Revision WORKING = new Revision(Kind.working, 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:        /**
 172:         * the revision number
 173:         */
 174:        protected long revNumber;
 175:
 176:        /**
 177:         * create a revision by number object
 178:         * @param number the number
 179:         * @throws IllegalArgumentException If the specified revision
 180:         * number is invalid.
 181:         */
 182:        public Number(long number)
 183:	          {
 184:            super(Kind.number, true);
 185:            if(number < 0)
 186:                throw new IllegalArgumentException(
 187:                        "negative revision numbers are not allowed");
 188:            revNumber = number;
 189:        }
 190:
 191:        /**
 192:         * Returns the revision number
 193:         * @return number
 194:         */
 195:        public long getNumber()
 196:	          {
 197:            return revNumber;
 198:        }
 199:
 200:        /**
 201:         * return the textual representation of the revision
 202:         * @return english text
 203:         */
 204:	          public String toString() {
 205:            return Long.toString(revNumber);
 206:        }
 207:
 208:        /**
 209:         * compare to revision objects
 210:         * @param target
 211:         * @return if both object have equal content
 212:         */
 213:	          public boolean equals(Object target) {
 214:            if (!super.equals(target))
 215:                return false;
 216:
 217:            return ((Revision.Number)target).revNumber == revNumber;        
 218:        }
 219:    }
 220:
 221:    /**
 222:     * class to specify a revision by a date
 223:     */
 224:    public static class DateSpec extends Revision
 225:	      {
 226:        /**
 227:         * the date
 228:         */
 229:        protected Date revDate;
 230:
 231:        /**
 232:         * create a revision by date object
 233:         * @param date
 234:         */
 235:        public DateSpec(Date date)
 236:	          {
 237:            super(Kind.date, true);
 238:            if(date == null)
 239:                throw new IllegalArgumentException("a date must be specified");
 240:            revDate = date;
 241:        }
 242:        /**
 243:         * Returns the date of the revision
 244:         * @return the date
 245:         */
 246:        public Date getDate()
 247:	          {
 248:            return revDate;
 249:        }
 250:
 251:        /**
 252:         * return the textual representation of the revision
 253:         * @return english text
 254:         */
 255:	          public String toString() {
 256:            
 257:            SimpleDateFormat dateFormat =
 258:                    new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z",
 259:                            Locale.US);
 260:            return '{'+dateFormat.format(revDate)+'}';
 261:        }
 262:
 263:        /**
 264:         * compare to revision objects
 265:         * @param target
 266:         * @return if both object have equal content
 267:         */
 268:	          public boolean equals(Object target) {
 269:            if (!super.equals(target))
 270:                return false;
 271:
 272:            return ((Revision.DateSpec)target).revDate.equals(revDate);        
 273:        }
 274:        
 275:    }
 276:
 277:    /**
 278:     * Various ways of specifying revisions.
 279:     *
 280:     * Note:
 281:     * In contexts where local mods are relevant, the `working' kind
 282:     * refers to the uncommitted "working" revision, which may be modified
 283:     * with respect to its base revision.  In other contexts, `working'
 284:     * should behave the same as `committed' or `current'.
 285:     *
 286:     * the values are defined in RevisionKind because of building reasons
 287:     */
 288:    public static final class Kind implements RevisionKind
 289:	      {
 290:    }
 291:}