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