View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.lutecetools.service.version;
35  
36  /**
37   * Version
38   */
39  public class Version implements Comparable
40  {
41      private int _nMajor;
42      private int _nMinor;
43      private int _nPatch;
44      private String _strQualifier;
45  
46      /**
47       * @return the nMajor
48       */
49      public int getMajor( )
50      {
51          return _nMajor;
52      }
53  
54      /**
55       * @param nMajor
56       *            the nMajor to set
57       */
58      public void setMajor( int nMajor )
59      {
60          _nMajor = nMajor;
61      }
62  
63      /**
64       * @return the nMinor
65       */
66      public int getMinor( )
67      {
68          return _nMinor;
69      }
70  
71      /**
72       * @param nMinor
73       *            the nMinor to set
74       */
75      public void setMinor( int nMinor )
76      {
77          _nMinor = nMinor;
78      }
79  
80      /**
81       * @return the nPatch
82       */
83      public int getPatch( )
84      {
85          return _nPatch;
86      }
87  
88      /**
89       * @param nPatch
90       *            the nPatch to set
91       */
92      public void setPatch( int nPatch )
93      {
94          _nPatch = nPatch;
95      }
96  
97      /**
98       * @return the Qualifier
99       */
100     public String getQualifier( )
101     {
102         return _strQualifier;
103     }
104 
105     /**
106      * @param strQualifier
107      *            the Qualifier to set
108      */
109     public void setQualifier( String strQualifier )
110     {
111         _strQualifier = strQualifier;
112     }
113 
114     @Override
115     public int compareTo( Object object )
116     {
117         Version/../../../../../../fr/paris/lutece/plugins/lutecetools/service/version/Version.html#Version">Version version = (Version) object;
118         int nDiff = _nMajor - version.getMajor( );
119         if ( nDiff != 0 )
120         {
121             return nDiff;
122         }
123         nDiff = _nMinor - version.getMinor( );
124         if ( nDiff != 0 )
125         {
126             return nDiff;
127         }
128         nDiff = _nPatch - version.getPatch( );
129         return nDiff;
130     }
131 
132     public String getVersion( )
133     {
134         StringBuilder sbVersion = new StringBuilder( );
135         sbVersion.append( _nMajor ).append( '.' ).append( _nMinor ).append( '.' ).append( _nPatch );
136         if ( _strQualifier != null )
137         {
138             sbVersion.append( '-' ).append( _strQualifier );
139         }
140         return sbVersion.toString( );
141     }
142 
143     public static Version parse( String strSource ) throws VersionParsingException
144     {
145         Versionns/lutecetools/service/version/Version.html#Version">Version version = new Version( );
146 
147         try
148         {
149             String strCurrent = strSource.trim( );
150 
151             // Search for qualifier
152             int nPos = strCurrent.indexOf( '-' );
153             if ( nPos != -1 )
154             {
155                 version.setQualifier( strCurrent.substring( nPos + 1 ) );
156                 strCurrent = strCurrent.substring( 0, nPos );
157             }
158 
159             // Search for major digits
160             nPos = strCurrent.indexOf( '.' );
161 
162             String strMajor = strCurrent.substring( 0, nPos );
163             version.setMajor( Integer.parseInt( strMajor ) );
164 
165             // Search for minor digits
166             strCurrent = strCurrent.substring( nPos + 1 );
167             nPos = strCurrent.indexOf( '.' );
168 
169             if ( nPos != -1 )
170             {
171                 String strMinor = strCurrent.substring( 0, nPos );
172                 version.setMinor( Integer.parseInt( strMinor ) );
173 
174                 strCurrent = strCurrent.substring( nPos + 1 );
175                 version.setPatch( Integer.parseInt( strCurrent ) );
176             }
177             else
178             {
179                 version.setMinor( Integer.parseInt( strCurrent ) );
180             }
181         }
182         catch( Exception e )
183         {
184             throw new VersionParsingException( "Error parsing version : '" + strSource + "' : " + e.getMessage( ), e );
185         }
186         return version;
187     }
188 
189 }