View Javadoc
1   package org.apache.maven.plugins.ear.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  /**
26   * Represents the supported JavaEE version.
27   * 
28   * @author Stephane Nicoll
29   */
30  public class JavaEEVersion
31      implements Comparable<JavaEEVersion>
32  {
33  
34      private static final String VERSION_1_3 = "1.3";
35  
36      private static final String VERSION_1_4 = "1.4";
37  
38      private static final String VERSION_5 = "5";
39  
40      private static final String VERSION_6 = "6";
41  
42      private static final String VERSION_7 = "7";
43      
44      private static final String VERSION_8 = "8";
45  
46      private static final String VERSION_9 = "9";
47  
48      private static final String VERSION_10 = "10";
49  
50      private static final Map<String, JavaEEVersion> VERSION_MAP = new HashMap<String, JavaEEVersion>();
51  
52      /**
53       * Represents the J2EE 1.3 version.
54       */
55      public static final JavaEEVersion ONE_DOT_THREE = new JavaEEVersion( Integer.valueOf( 0 ), VERSION_1_3 );
56  
57      /**
58       * Represents the J2EE 1.4 version.
59       */
60      public static final JavaEEVersion ONE_DOT_FOUR = new JavaEEVersion( Integer.valueOf( 1 ), VERSION_1_4 );
61  
62      /**
63       * Represents the JavaEE 5 version.
64       */
65      public static final JavaEEVersion FIVE = new JavaEEVersion( Integer.valueOf( 2 ), VERSION_5 );
66  
67      /**
68       * Represents the JavaEE 6 version.
69       */
70      public static final JavaEEVersion SIX = new JavaEEVersion( Integer.valueOf( 3 ), VERSION_6 );
71  
72      /**
73       * Represents the JavaEE 7 version.
74       */
75      public static final JavaEEVersion SEVEN = new JavaEEVersion( Integer.valueOf( 4 ), VERSION_7 );
76  
77      /**
78       * Represents the JavaEE 8 version.
79       */
80      public static final JavaEEVersion EIGHT = new JavaEEVersion( Integer.valueOf( 5 ), VERSION_8 );
81  
82      /**
83       * Represents the JakartaEE 9 version.
84       */
85      public static final JavaEEVersion NINE = new JavaEEVersion( Integer.valueOf( 6 ), VERSION_9 );
86  
87      /**
88       * Represents the JakartaEE 10 version.
89       */
90      public static final JavaEEVersion TEN = new JavaEEVersion( Integer.valueOf( 7 ), VERSION_10 );
91  
92      private final Integer index;
93  
94      private final String version;
95  
96      private JavaEEVersion( Integer index, String version )
97      {
98          this.index = index;
99          this.version = version;
100         VERSION_MAP.put( version, this );
101     }
102 
103     /**
104      * @param paramVersion The version.
105      * @return {@link JavaEEVersion}
106      * @throws InvalidJavaEEVersion in case of a wrong version.
107      */
108     public static JavaEEVersion getJavaEEVersion( String paramVersion )
109         throws InvalidJavaEEVersion
110     {
111         if ( !isValid( paramVersion ) )
112         {
113             throw new InvalidJavaEEVersion( "Invalid version [" + paramVersion + "]", paramVersion );
114         }
115         return VERSION_MAP.get( paramVersion );
116     }
117 
118     /**
119      * Returns the version as a string.
120      * 
121      * @return the version string
122      */
123     public String getVersion()
124     {
125         return version;
126     }
127 
128     /**
129      * Specifies if this version is greater or equal to the specified version.
130      * 
131      * @param paramVersion the version to check
132      * @return true if this version is greater or equal to {@code version}
133      */
134     public boolean ge( JavaEEVersion paramVersion )
135     {
136         return this.compareTo( paramVersion ) >= 0;
137     }
138 
139     /**
140      * Specifies if this version is greater than the specified version.
141      * 
142      * @param paramVersion the version to check
143      * @return true if this version is greater to {@code version}
144      */
145     public boolean gt( JavaEEVersion paramVersion )
146     {
147         return this.compareTo( paramVersion ) > 0;
148     }
149 
150     /**
151      * Specifies if this version is equal to the specified version.
152      * 
153      * @param paramVersion the version to check
154      * @return true if this version is equal to {@code version}
155      */
156     public boolean eq( JavaEEVersion paramVersion )
157     {
158         return this.compareTo( paramVersion ) == 0;
159     }
160 
161     /**
162      * Specifies if this version is less or equal to the specified version.
163      * 
164      * @param paramVersion the version to check
165      * @return true if this version is less or equal to {@code version}
166      */
167     public boolean le( JavaEEVersion paramVersion )
168     {
169         return this.compareTo( paramVersion ) <= 0;
170     }
171 
172     /**
173      * Specifies if this version is less than the specified version.
174      * 
175      * @param paramVersion the version to check
176      * @return true if this version is less or equal to {@code version}
177      */
178     public boolean lt( JavaEEVersion paramVersion )
179     {
180         return this.compareTo( paramVersion ) < 0;
181     }
182 
183     /**
184      * Checks if the specified version string is valid.
185      * 
186      * @param paramVersion the version string to check
187      * @return {@code true} if the version is valid
188      */
189     private static boolean isValid( String paramVersion )
190     {
191         if ( paramVersion == null )
192         {
193             throw new NullPointerException( "version cannot be null." );
194         }
195         // @formatter:off
196         return VERSION_1_3.equals( paramVersion ) 
197             || VERSION_1_4.equals( paramVersion )
198             || VERSION_5.equals( paramVersion ) 
199             || VERSION_6.equals( paramVersion ) 
200             || VERSION_7.equals( paramVersion )
201             || VERSION_8.equals( paramVersion )
202             || VERSION_9.equals( paramVersion )
203             || VERSION_10.equals( paramVersion );
204         // @formatter:on
205     }
206 
207     /** {@inheritDoc} */
208     public int compareTo( JavaEEVersion otherVersion )
209     {
210         if ( otherVersion == null )
211         {
212             throw new NullPointerException( "other object to compare to could not be null." );
213         }
214         return index.compareTo( otherVersion.index );
215     }
216 }