PageRenderTime 27ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/hudson-utils/src/main/java/org/hudsonci/utils/common/VersionFormatter.java

http://github.com/hudson/hudson
Java | 83 lines | 36 code | 9 blank | 38 comment | 9 complexity | e2bc5f07be3e92b0719aec250f094afe MD5 | raw file
 1/**
 2 * The MIT License
 3 *
 4 * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
 5 *
 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
 7 * of this software and associated documentation files (the "Software"), to deal
 8 * in the Software without restriction, including without limitation the rights
 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25package org.hudsonci.utils.common;
26
27/**
28 * Utility supplying different ways to format a version number.
29 *  
30 * @author Jamie Whitehouse
31 * @since 2.1.0
32 */
33class VersionFormatter
34{
35    /**
36     * @return a formatted version number containing the raw information
37     */
38    public static String asRawVersion( String version, String timestamp, String sequence )
39    {
40        StringBuilder buff = new StringBuilder();
41
42        if ( version == null )
43        {
44            version = VersionSupport.UNKNOWN;
45        }
46        buff.append( version );
47
48        if ( timestamp != null )
49        {
50            buff.append( "," ).append( timestamp );
51        }
52
53        if ( sequence != null )
54        {
55            buff.append( "#" ).append( sequence );
56        }
57
58        return buff.toString();
59    }
60
61    /**
62     * Generates an appropriate format based on the project being a release or development version. Example:
63     * 0.2.1,201009291818#1
64     * 
65     * @return a formatted version number in the simplest and most significant form
66     */
67    public static String asCanonicalVersion( String version, String timestamp, String sequence )
68    {
69        String derivedVersion = version;
70        if ( derivedVersion == null )
71        {
72            derivedVersion = VersionSupport.UNKNOWN;
73        }
74
75        String snapshot = "-SNAPSHOT";
76        if ( derivedVersion.contains( snapshot ) )
77        {
78            derivedVersion = asRawVersion( version.replace( snapshot, "" ), timestamp, sequence );
79        }
80
81        return derivedVersion;
82    }
83}