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 Map<String, JavaEEVersion> VERSION_MAP = new HashMap<String, JavaEEVersion>();
47
48 /**
49 * Represents the J2EE 1.3 version.
50 */
51 public static final JavaEEVersion ONE_DOT_THREE = new JavaEEVersion( Integer.valueOf( 0 ), VERSION_1_3 );
52
53 /**
54 * Represents the J2EE 1.4 version.
55 */
56 public static final JavaEEVersion ONE_DOT_FOUR = new JavaEEVersion( Integer.valueOf( 1 ), VERSION_1_4 );
57
58 /**
59 * Represents the JavaEE 5 version.
60 */
61 public static final JavaEEVersion FIVE = new JavaEEVersion( Integer.valueOf( 2 ), VERSION_5 );
62
63 /**
64 * Represents the JavaEE 6 version.
65 */
66 public static final JavaEEVersion SIX = new JavaEEVersion( Integer.valueOf( 3 ), VERSION_6 );
67
68 /**
69 * Represents the JavaEE 7 version.
70 */
71 public static final JavaEEVersion SEVEN = new JavaEEVersion( Integer.valueOf( 4 ), VERSION_7 );
72
73 /**
74 * Represents the JavaEE 8 version.
75 */
76 public static final JavaEEVersion EIGHT = new JavaEEVersion( Integer.valueOf( 5 ), VERSION_8 );
77
78 private final Integer index;
79
80 private final String version;
81
82 private JavaEEVersion( Integer index, String version )
83 {
84 this.index = index;
85 this.version = version;
86 VERSION_MAP.put( version, this );
87 }
88
89 /**
90 * @param paramVersion The version.
91 * @return {@link JavaEEVersion}
92 * @throws InvalidJavaEEVersion in case of a wrong version.
93 */
94 public static JavaEEVersion getJavaEEVersion( String paramVersion )
95 throws InvalidJavaEEVersion
96 {
97 if ( !isValid( paramVersion ) )
98 {
99 throw new InvalidJavaEEVersion( "Invalid version [" + paramVersion + "]", paramVersion );
100 }
101 return VERSION_MAP.get( paramVersion );
102 }
103
104 /**
105 * Returns the version as a string.
106 *
107 * @return the version string
108 */
109 public String getVersion()
110 {
111 return version;
112 }
113
114 /**
115 * Specifies if this version is greater or equal to the specified version.
116 *
117 * @param parmVersion the version to check
118 * @return true if this version is greater or equal to <tt>version</tt>
119 */
120 public boolean ge( JavaEEVersion parmVersion )
121 {
122 return this.compareTo( parmVersion ) >= 0;
123 }
124
125 /**
126 * Specifies if this version is greater than the specified version.
127 *
128 * @param paramVersion the version to check
129 * @return true if this version is greater to <tt>version</tt>
130 */
131 public boolean gt( JavaEEVersion paramVersion )
132 {
133 return this.compareTo( paramVersion ) > 0;
134 }
135
136 /**
137 * Specifies if this version is equal to the specified version.
138 *
139 * @param paramVersion the version to check
140 * @return true if this version is equal to <tt>version</tt>
141 */
142 public boolean eq( JavaEEVersion paramVersion )
143 {
144 return this.compareTo( paramVersion ) == 0;
145 }
146
147 /**
148 * Specifies if this version is less or equal to the specified version.
149 *
150 * @param paramVersion the version to check
151 * @return true if this version is less or equal to <tt>version</tt>
152 */
153 public boolean le( JavaEEVersion paramVersion )
154 {
155 return this.compareTo( paramVersion ) <= 0;
156 }
157
158 /**
159 * Specifies if this version is less than the specified version.
160 *
161 * @param paramVersion the version to check
162 * @return true if this version is less or equal to <tt>version</tt>
163 */
164 public boolean lt( JavaEEVersion paramVersion )
165 {
166 return this.compareTo( paramVersion ) < 0;
167 }
168
169 /**
170 * Checks if the specified version string is valid.
171 *
172 * @param paramVersion the version string to check
173 * @return <tt>true</tt> if the version is valid
174 */
175 private static boolean isValid( String paramVersion )
176 {
177 if ( paramVersion == null )
178 {
179 throw new IllegalArgumentException( "version could not be null." );
180 }
181 // @formatter:off
182 return VERSION_1_3.equals( paramVersion )
183 || VERSION_1_4.equals( paramVersion )
184 || VERSION_5.equals( paramVersion )
185 || VERSION_6.equals( paramVersion )
186 || VERSION_7.equals( paramVersion )
187 || VERSION_8.equals( paramVersion );
188 // @formatter:on
189 }
190
191 /** {@inheritDoc} */
192 public int compareTo( JavaEEVersion otherVersion )
193 {
194 if ( otherVersion == null )
195 {
196 throw new IllegalArgumentException( "other object to compare to could not be null." );
197 }
198 return index.compareTo( otherVersion.index );
199 }
200 }