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