File: Version.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (408 lines) | stat: -rw-r--r-- 11,680 bytes parent folder | download | duplicates (4)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*

Derby - Class org.apache.derbyTesting.functionTests.tests.upgradeTests.Version

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.

*/
package org.apache.derbyTesting.functionTests.tests.upgradeTests;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import org.apache.derby.iapi.services.info.ProductVersionHolder;

/**
 * <p>
 * A Derby version.
 * </p>
 */
public class Version implements Comparable<Version>
{
    ///////////////////////////////////////////////////////////////////////////////////
    //
    // CONSTANTS
    //
    ///////////////////////////////////////////////////////////////////////////////////

    private static final int EXPECTED_LEG_COUNT = 4;

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // STATE
    //
    ///////////////////////////////////////////////////////////////////////////////////

    private int[] _legs;
    private String _key;
    private String _branchID;

    // we keep one class loader per version so that we don't have an explosion
    // of class loaders for redundant versions
    private static HashMap<String, ClassLoader> _classLoaders =
            new HashMap<String, ClassLoader>();
    
    ///////////////////////////////////////////////////////////////////////////////////
    //
    // CONSTRUCTOR
    //
    ///////////////////////////////////////////////////////////////////////////////////

    /** Construct a version from four legs */
    public Version( int major, int minor, int fixpack, int bugversion )
    {
        this( new int[] { major, minor, fixpack, bugversion } );
    }
    
    /** Construct a version from its legs */
    public Version( int[] legs )
    {
        constructorMinion( legs );
    }

    /** Construct from a Derby ProductVersionHolder  */
    public Version( ProductVersionHolder pvh )
    {
        constructorMinion( getLegs( pvh ) );
    }
    private void constructorMinion( int[] legs )
    {
        if ( legs == null ) { legs = new int[] {}; }
        int count = legs.length;

        if ( count != EXPECTED_LEG_COUNT )
        {
            throw new IllegalArgumentException( "Expected " + EXPECTED_LEG_COUNT + " legs but only saw " + count );
        }

        _legs = new int[ count ];
        for ( int i = 0; i < count; i++ ) { _legs[ i ] = legs[ i ]; }

        makeKey();
    }
    private int[] getLegs( ProductVersionHolder pvh )
    {
        int[] result = new int[ EXPECTED_LEG_COUNT ];
        int   idx = 0;

        result[ idx++ ] = pvh.getMajorVersion();
        result[ idx++ ] = pvh.getMinorVersion();
        result[ idx++ ] = pvh.getMaintVersion() / ProductVersionHolder.MAINT_ENCODING;
        result[ idx++ ] = pvh.getMaintVersion() % ProductVersionHolder.MAINT_ENCODING;

        return result;
     }

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // PUBLIC BEHAVIOR
    //
    ///////////////////////////////////////////////////////////////////////////////////

    /**
     * <p>
     * Pretty-print this version.
     * </p>
     */
    public String toString()
    {
        return _key;
    }

    /**
     * <p>
     * Pretty-print the branch id, that is, the major + minor legs of the Version.
     * </p>
     */
    public String getBranchID()
    {
        if ( _branchID == null )
        {
            _branchID = Integer.toString(_legs[ 0 ]) + '.' + Integer.toString(_legs[ 1 ]);
        }

        return _branchID;
    }

    /**
     * <p>
     * Get a class loader for this version.
     * </p>
     */
    public ClassLoader getClassLoader()
    {
        ClassLoader retval = (ClassLoader) _classLoaders.get( _key );
        if ( retval != null ) { return retval; }
        
        addClassLoader( );
        
        return (ClassLoader) _classLoaders.get( _key );
    }
    
    ///////////////////////////////////////////////////////////////////////////////////
    //
    // Comparable BEHAVIOR
    //
    ///////////////////////////////////////////////////////////////////////////////////

    public int compareTo( Version other )
    {
        if ( other == null ) { return 1; }

        for ( int i = 0; i < EXPECTED_LEG_COUNT; i++ )
        {
            int result = this._legs[ i ] - other._legs[ i ];

            if ( result != 0 ) { return result; }
        }

        return 0;
    }

    public boolean equals( Object other ) {
        return (other instanceof Version) && (compareTo((Version) other) == 0);
    }
    public int hashCode() { return toString().hashCode(); }
    
    ///////////////////////////////////////////////////////////////////////////////////
    //
    // MINIONS
    //
    ///////////////////////////////////////////////////////////////////////////////////

    /**
     * <p>
     * Add the class loader for this version if it doesn't already exist.
     * </p>
     */
    private void addClassLoader( )
    {
        ClassLoader classLoader = UpgradeClassLoader.makeClassLoader( _legs );

        _classLoaders.put( _key, classLoader );
    }

    /**
     * <p>
     * Make the key for looking up our class loader.
     * </p>
     */
    private void makeKey()
    {
        StringBuffer buffer = new StringBuffer();
        int          legCount = _legs.length;

        for ( int i = 0; i < legCount; i++ )
        {
            if ( i > 0 ) { buffer.append( '.' ); }
            buffer.append( _legs[ i ] );
        }

        _key = buffer.toString();
    }

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // INNER CLASSES
    //
    ///////////////////////////////////////////////////////////////////////////////////

    /**
     * <p>
     * This is a sequence of Versions. It is the caller's responsibility to
     * determine whether the Versions are in sort order.
     * </p>
     */
    public static final class Trajectory implements Comparable
    {
        private Version[] _versions;

        /**
         * <p>
         * Construct from a list of Versions.
         * </p>
         */
        public Trajectory( ArrayList<Version> versionList )
        {
            if (versionList == null) {
                versionList = new ArrayList<Version>();
            }

            Version[] versions = new Version[ versionList.size() ];
            versionList.toArray( versions );

            constructorMinion( versions );
        }

        /**
         * <p>
         * Construct from an array of Versions.
         * </p>
         */
        public Trajectory( Version[] versions )
        {
            if ( versions == null ) { versions = new Version[ 0 ]; }

            constructorMinion( versions );
        }

        private void constructorMinion( Version[] versions )
        {
            int count = versions .length;
            _versions = new Version[ count ];

            for ( int i = 0; i < count; i++ ) { _versions[ i ] = versions[ i ]; }
        }

        /**
         * <p>
         * Sort this Trajectory so that the Versions are arranged in ascending
         * order. Returns this Trajectory after the sort.
         * </p>
         */
        public Trajectory sort()
        {
            Arrays.sort( _versions );

            return this;
        }

        public int getVersionCount() { return _versions.length; }
        public Version getVersion( int idx ) { return _versions[ idx ]; }

        /**
         * <p>
         * Return the ending Version of this Trajectory.
         * </p>
         */
        public Version getEndingVersion()
        {
            return getVersion( getVersionCount() -1 );
        }
        
        /**
         * <p>
         * Return true if this Trajectory starts at the desired Version.
         * </p>
         */
        public boolean startsAt( Version candidate )
        {
            return ( getVersion( 0 ).equals( candidate ) );
        }
        
        /**
         * <p>
         * Return true if this Trajectory starts at the desired branch.
         * </p>
         */
        public boolean startsAt( String branchID )
        {
            return ( getVersion( 0 ).getBranchID().equals( branchID ) );
        }
        
        /**
         * <p>
         * Return true if this Trajectory contains the desired Version.
         * </p>
         */
        public boolean contains( Version candidate )
        {
            int count = getVersionCount();
            for ( int i = 0; i < count; i++ )
            {
                if ( getVersion( i ).equals( candidate ) ) { return true; }
            }
            
            return false;
        }
        
        /**
         * <p>
         * Return true if this Trajectory contains a version from the desired branch.
         * </p>
         */
        public boolean contains( String branchID )
        {
            int count = getVersionCount();
            for ( int i = 0; i < count; i++ )
            {
                if ( getVersion( i ).getBranchID().equals( branchID ) ) { return true; }
            }
            
            return false;
        }
        
        /**
         * <p>
         * Return true if this Trajectory ends at the desired Version.
         * </p>
         */
        public boolean endsAt( Version candidate )
        {
            return ( getVersion( getVersionCount() - 1 ).equals( candidate ) );
        }
        
        /**
         * <p>
         * Return true if this Trajectory ends at the desired branch.
         * </p>
         */
        public boolean endsAt( String branchID )
        {
            return ( getVersion( getVersionCount() - 1 ).getBranchID().equals( branchID ) );
        }
        
        public String toString()
        {
            StringBuffer buffer = new StringBuffer();
            int          count = _versions.length;

            for ( int i = 0; i < count; i++ )
            {
                if ( i > 0 ) { buffer.append( " -> " ); }
                buffer.append( _versions[ i ].toString() );
            }

            return buffer.toString();
        }

        public int compareTo( Object other )
        {
            if ( other == null ) { return -1; }
            if ( !(other instanceof Trajectory) ) { return -1; }

            Trajectory that = (Trajectory) other;
            int           thisLength = this.getVersionCount();
            int           thatLength = that.getVersionCount();
            int           minLength = thisLength < thatLength ? thisLength : thatLength;

            for ( int i = 0; i < minLength; i++ )
            {
                int result = this.getVersion( i ).compareTo( that.getVersion( i ) );
                if ( result != 0 ) { return result; }
            }

            return thisLength - thatLength;
        }

        public boolean equals( Object other ) { return ( compareTo( other ) == 0 ); }

        public int hashCode() { return toString().hashCode(); }
    }


}