File: GobyVersion.java

package info (click to toggle)
libgoby-java 3.3.1%2Bdfsg2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 58,104 kB
  • sloc: java: 78,105; cpp: 5,011; xml: 3,170; python: 2,108; sh: 1,575; ansic: 277; makefile: 114
file content (111 lines) | stat: -rw-r--r-- 4,079 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2009-2010 Institute for Computational Biomedicine,
 *                    Weill Medical College of Cornell University
 *
 *  This file is part of the Goby IO API.
 *
 *     The Goby IO API is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     The Goby IO API is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with the Goby IO API.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.campagnelab.goby;

import org.campagnelab.goby.util.WarningCounter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

/**
 * Helper class to compare Goby versions.
 *
 * @author Fabien Campagne
 *         Date: May 6, 2011
 *         Time: 6:29:16 PM
 */
public class GobyVersion {
    private static String[] versionPairs = {
            "1.9.5-", "20110101000000",
            "goby_1.9.6", "20110510111716",
            "goby_1.9.8.1", "20111217111716",
            "goby_1.9.8.2", "20120120000000",
            "3.0.0","20160725144635"
    };

    private static String now() {
        Date d = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyymmddHHmmss");

        return formatter.format(d);
    }

    /**
     * Return true when version is more recent or as recent as toBeComparedTo, false otherwise.
     *
     * @param version       The goby version under test.
     * @param toBeCompareTo A specific version we wish to compare version to.
     * @return rrue if version is more or as recent as toBeComparedTo.
     */
    public static boolean isMoreRecent(String version, String toBeCompareTo) {
        return !isOlder(version, toBeCompareTo);
    }

    /**
     * Return true when version is strictly older than toBeComparedTo, false otherwise.
     *
     * @param version       The goby version under test.
     * @param toBeCompareTo A specific version we wish to compare version to.
     * @return Return true when version is strictly older than toBeComparedTo, false otherwise.
     */
    public static boolean isOlder(String version, String toBeCompareTo) {

        if (version.equals(toBeCompareTo)) {
            // same version, the second one is not older.
            return false;
        }

        long time1 = Long.parseLong(reduce(version));
        long time2 = Long.parseLong(reduce(toBeCompareTo));
        return time1 < time2;

    }
    private static WarningCounter versionWarning = new WarningCounter(1);

    /**
     * Reduce a version string to a date in the format yyyymmddHHmmss.
     * @param version String obtained from the Goby jar manifest.
     * @return Date the version was packaged.
     */
    private static String reduce(String version) {
        if (version.indexOf('(')>=0 && version.indexOf(')')>=0)  {

            // format "development (DATE)"  OR "goby_1.9.6 (DATE)
            String tokens[] = version.split("[()]");
            final String token = tokens[1];
            return token;
        } else {
            for (int i = 0; i < versionPairs.length; i++) {
                if (version.equals(versionPairs[i])) return versionPairs[i + 1];
            }
        } // the version number was not recognized, assume we are dealing with a more recent version.
        versionWarning.warn(LOG,String.format("Version number %s not recognized. Assuming this version is the most recent.", version));
        return now();
    }

    /**
     * Used to log debug and informational messages.
     */
    private static final Log LOG = LogFactory.getLog(GobyVersion.class);

}