summaryrefslogtreecommitdiff
path: root/subversion/bindings/javahl/src/org/apache/subversion/javahl/types/Revision.java
blob: 675737db561f19c9654fa0ae14b4909b56eaf50f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
 * @copyright
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 * @endcopyright
 */

package org.apache.subversion.javahl.types;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Class to specify a revision in a svn command.
 */
public class Revision implements java.io.Serializable
{
    // Update the serialVersionUID when there is a incompatible change made to
    // this class.  See the java documentation for when a change is incompatible.
    // http://java.sun.com/javase/7/docs/platform/serialization/spec/version.html#6678
    private static final long serialVersionUID = 1L;

    /**
     * kind of revision specified
     */
    protected Kind revKind;

    /**
     * Internally create a new revision.  Public for backward compat reasons.
     * Callers should use getInstance() instead.
     * @param kind    kind of revision
     */
    public Revision(Kind kind)
    {
        if (kind.ordinal() < Kind.unspecified.ordinal()
               || kind.ordinal() > Kind.head.ordinal())
            throw new IllegalArgumentException(
                    kind+" is not a legal revision kind");
        revKind = kind;
    }

    /**
     * Returns the kind of the Revsion
     * @return kind
     */
    public Kind getKind()
    {
        return revKind;
    }

    /**
     * return the textual representation of the revision
     * @return english text
     */
    public String toString()
    {
        switch(revKind) {
            case base : return "BASE";
            case committed : return "COMMITTED";
            case head : return "HEAD";
            case previous : return "PREV";
            case working : return "WORKING";
            case unspecified: return "UNSPECIFIED";
        }
        return super.toString();
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    public int hashCode()
    {
        return revKind.ordinal() * -1;
    }

    /**
     * compare to revision objects
     * @param target
     * @return if both object have equal content
     */
    public boolean equals(Object target) {
        if (this == target)
            return true;
        if (!(target instanceof Revision))
            return false;

        return ((Revision)target).revKind == revKind;
    }

    /**
     * Creates a Revision.Number object
     * @param revisionNumber    the revision number of the new object
     * @return  the new object
     * @throws IllegalArgumentException If the specified revision
     * number is invalid.
     */
    public static Revision getInstance(long revisionNumber)
    {
        return new Revision.Number(revisionNumber);
    }

    /**
     * Factory which creates {@link #Number} objects for valid
     * revision numbers only (those greater than zero).  For internal
     * usage to avoid an IllegalArgumentException, where no external
     * consumer of the javahl API passed an invalid revision number.
     *
     * @param revNumber The revision number to create an object for.
     * @return An object representing <code>revNumber</code>, or
     * <code>null</code> if the revision number was invalid.
     */
    static Number createNumber(long revNumber)
    {
        return (revNumber < 0 ? null : new Number(revNumber));
    }

    /**
     * Creates a Revision.DateSpec objet
     * @param revisionDate  the date of the new object
     * @return  the new object
     */
    public static Revision getInstance(Date revisionDate)
    {
        return new Revision.DateSpec(revisionDate);
    }

    /**
     * last committed revision
     */
    public static final Revision HEAD = new Revision(Kind.head);

    /**
     * first existing revision
     */
    public static final Revision START = new Revision(Kind.unspecified);

    /**
     * unspecified revision
     */
    public static final Revision UNSPECIFIED = START;

    /**
     * last committed revision, needs working copy
     */
    public static final Revision COMMITTED = new Revision(Kind.committed);

    /**
     * previous committed revision, needs working copy
     */
    public static final Revision PREVIOUS = new Revision(Kind.previous);

    /**
     * base revision of working copy
     */
    public static final Revision BASE = new Revision(Kind.base);

    /**
     * working version in working copy
     */
    public static final Revision WORKING = new Revision(Kind.working);

    /**
     * Marker revision number for no real revision
     */
    public static final int SVN_INVALID_REVNUM = -1;

    /**
     * class to specify a Revision by number
     */
    public static class Number extends Revision
    {
        // Update the serialVersionUID when there is a incompatible
        // change made to this class.
        private static final long serialVersionUID = 1L;

        /**
         * the revision number
         */
        protected long revNumber;

        /**
         * create a revision by number object
         * @param number the number
         * @throws IllegalArgumentException If the specified revision
         * number is invalid.
         */
        public Number(long number)
        {
            super(Kind.number);
            if (number < 0)
                throw new IllegalArgumentException
                    ("Invalid (negative) revision number: " + number);
            revNumber = number;
        }

        /**
         * Returns the revision number
         * @return number
         */
        public long getNumber()
        {
            return revNumber;
        }

        /**
         * return the textual representation of the revision
         * @return english text
         */
        public String toString() {
            return Long.toString(revNumber);
        }

        /**
         * compare to revision objects
         * @param target
         * @return if both object have equal content
         */
        public boolean equals(Object target) {
            if (!super.equals(target))
                return false;

            return ((Revision.Number)target).revNumber == revNumber;
        }

        /* (non-Javadoc)
         * @see org.apache.subversion.javahl.Revision#hashCode()
         */
        public int hashCode()
        {
            return (int)(revNumber ^ (revNumber >>> 32));
        }
    }

    /**
     * class to specify a revision by a date
     */
    public static class DateSpec extends Revision
    {
        // Update the serialVersionUID when there is a incompatible change
        // made to this class.
        private static final long serialVersionUID = 1L;

        /**
         * the date
         */
        protected Date revDate;

        /**
         * create a revision by date object
         * @param date
         */
        public DateSpec(Date date)
        {
            super(Kind.date);
            if (date == null)
                throw new IllegalArgumentException("a date must be specified");
            revDate = date;
        }

        /**
         * Create a revision from a timestamp in milliseconds.
         * Used by the native implementaiton.
         */
        private DateSpec(long milliseconds)
        {
            this(new Date(milliseconds));
        }

        /**
         * Returns the date of the revision
         * @return the date
         */
        public Date getDate()
        {
            return revDate;
        }

        /**
         * return the textual representation of the revision
         * @return english text
         */
        public String toString() {

            SimpleDateFormat dateFormat =
                    new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z",
                                         Locale.US);
            return '{'+dateFormat.format(revDate)+'}';
        }

        /**
         * compare to revision objects
         * @param target
         * @return if both object have equal content
         */
        public boolean equals(Object target) {
            if (!super.equals(target))
                return false;

            return ((Revision.DateSpec)target).revDate.equals(revDate);
        }
        /* (non-Javadoc)
         * @see org.apache.subversion.javahl.Revision#hashCode()
         */
        public int hashCode()
        {
            return revDate.hashCode();
        }
    }

    /**
     * Various ways of specifying revisions.
     *
     * Note:
     * In contexts where local mods are relevant, the `working' kind
     * refers to the uncommitted "working" revision, which may be modified
     * with respect to its base revision.  In other contexts, `working'
     * should behave the same as `committed' or `current'.
     */
    public enum Kind
    {
       /** No revision information given. */
        unspecified,

        /** revision given as number */
        number,

        /** revision given as date */
        date,

        /** rev of most recent change */
        committed,

        /** (rev of most recent change) - 1 */
        previous,

        /** .svn/entries current revision */
        base,

        /** current, plus local mods */
        working,

        /** repository youngest */
        head;
    }
}