javacommons/utils/javasrc/com/nokia/mj/impl/utils/Version.java
branchRCL_3
changeset 19 04becd199f91
child 67 63b81d807542
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.mj.impl.utils;
       
    20 
       
    21 /**
       
    22  * Class for holding version information.
       
    23  */
       
    24 public class Version
       
    25 {
       
    26     private int iMajor = 0;
       
    27     private int iMinor = 0;
       
    28     private int iMicro = 0;
       
    29 
       
    30     /**
       
    31      * Private default constructor.
       
    32      */
       
    33     private Version()
       
    34     {
       
    35     }
       
    36 
       
    37     /**
       
    38      * Constructor for version object.
       
    39      *
       
    40      * @param aMajor major part of version
       
    41      * @param aMinor minor part of version
       
    42      * @param aMicro micro part of version
       
    43      */
       
    44     public Version(int aMajor, int aMinor, int aMicro)
       
    45     {
       
    46         iMajor = aMajor;
       
    47         iMinor = aMinor;
       
    48         iMicro = aMicro;
       
    49     }
       
    50 
       
    51     /**
       
    52      * Parses version from given string. If string is not
       
    53      * valid version this method returns null.
       
    54      */
       
    55     public static Version getVersion(String aStr)
       
    56     {
       
    57         if (aStr == null || aStr.length() == 0)
       
    58         {
       
    59             return null;
       
    60         }
       
    61         String str = aStr.trim();
       
    62 
       
    63         int major = 0;
       
    64         int minor = 0;
       
    65         int micro = 0;
       
    66 
       
    67         Version version = null;
       
    68         try
       
    69         {
       
    70             if (str != null && str.length() > 0)
       
    71             {
       
    72                 // Parse major part.
       
    73                 int i = str.indexOf(".");
       
    74                 if (i == -1)
       
    75                 {
       
    76                     major = Integer.parseInt(str);
       
    77                     str = null;
       
    78                 }
       
    79                 else
       
    80                 {
       
    81                     major = Integer.parseInt(str.substring(0, i));
       
    82                     str = str.substring(i+1);
       
    83                 }
       
    84             }
       
    85             if (str != null && str.length() > 0)
       
    86             {
       
    87                 // Parse minor part.
       
    88                 int i = str.indexOf(".");
       
    89                 if (i == -1)
       
    90                 {
       
    91                     minor = Integer.parseInt(str);
       
    92                     str = null;
       
    93                 }
       
    94                 else
       
    95                 {
       
    96                     minor = Integer.parseInt(str.substring(0, i));
       
    97                     str = str.substring(i+1);
       
    98                 }
       
    99             }
       
   100             if (str != null && str.length() > 0)
       
   101             {
       
   102                 // Parse micro part.
       
   103                 int i = str.indexOf(".");
       
   104                 if (i == -1)
       
   105                 {
       
   106                     micro = Integer.parseInt(str);
       
   107                     str = null;
       
   108                 }
       
   109                 else
       
   110                 {
       
   111                     throw new NumberFormatException(
       
   112                         "more than three parts in version number");
       
   113                 }
       
   114             }
       
   115             version = new Version(major, minor, micro);
       
   116         }
       
   117         catch (NumberFormatException nfe)
       
   118         {
       
   119             //Log.logError("Version parsing failed: " + aStr, nfe);
       
   120             throw nfe;
       
   121         }
       
   122         return version;
       
   123     }
       
   124 
       
   125     /**
       
   126      * Get major part of the version.
       
   127      */
       
   128     public int getMajor()
       
   129     {
       
   130         return iMajor;
       
   131     }
       
   132 
       
   133     /**
       
   134      * Get minor part of the version.
       
   135      */
       
   136     public int getMinor()
       
   137     {
       
   138         return iMinor;
       
   139     }
       
   140 
       
   141     /**
       
   142      * Get micro part of the version.
       
   143      */
       
   144     public int getMicro()
       
   145     {
       
   146         return iMicro;
       
   147     }
       
   148 
       
   149     /**
       
   150      * Compare this version to given one.
       
   151      * @return a negative integer, zero, or a positive integer
       
   152     * if this version is less than, equal to, or greater than
       
   153     * the given version.
       
   154      */
       
   155     public int compareTo(Version aVersion)
       
   156     {
       
   157         int result = 0;
       
   158 
       
   159         if (getMajor() < aVersion.getMajor())
       
   160         {
       
   161             result = -1;
       
   162         }
       
   163         else if (getMajor() > aVersion.getMajor())
       
   164         {
       
   165             result = 1;
       
   166         }
       
   167 
       
   168         if (result == 0)
       
   169         {
       
   170             if (getMinor() < aVersion.getMinor())
       
   171             {
       
   172                 result = -1;
       
   173             }
       
   174             else if (getMinor() > aVersion.getMinor())
       
   175             {
       
   176                 result = 1;
       
   177             }
       
   178         }
       
   179 
       
   180         if (result == 0)
       
   181         {
       
   182             if (getMicro() < aVersion.getMicro())
       
   183             {
       
   184                 result = -1;
       
   185             }
       
   186             else if (getMicro() > aVersion.getMicro())
       
   187             {
       
   188                 result = 1;
       
   189             }
       
   190         }
       
   191 
       
   192         return result;
       
   193     }
       
   194 
       
   195     /**
       
   196      * Returns a hash code value for this object.
       
   197      */
       
   198     public int hashCode()
       
   199     {
       
   200         return iMajor + iMinor + iMicro;
       
   201     }
       
   202 
       
   203     /**
       
   204      * Indicates whether given object is equal to this one.
       
   205      */
       
   206     public boolean equals(Object aObj)
       
   207     {
       
   208         if (!(aObj instanceof Version))
       
   209         {
       
   210             return false;
       
   211         }
       
   212         Version v = (Version)aObj;
       
   213         if (this.getMajor() == v.getMajor() &&
       
   214                 this.getMinor() == v.getMinor() &&
       
   215                 this.getMicro() == v.getMicro())
       
   216         {
       
   217             return true;
       
   218         }
       
   219         return false;
       
   220     }
       
   221 
       
   222     /**
       
   223      * Returns string representation of this object.
       
   224      */
       
   225     public String toString()
       
   226     {
       
   227         StringBuffer buf = new StringBuffer();
       
   228         buf.append(getMajor()).append(".").append(getMinor());
       
   229         if (iMicro != 0)
       
   230         {
       
   231             buf.append(".").append(getMicro());
       
   232         }
       
   233         return buf.toString();
       
   234     }
       
   235 }