View Javadoc

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> versionsMap = new HashMap<String, JavaEEVersion>();
45  
46      /**
47       * Represents the J2EE 1.3 version.
48       */
49      public static final JavaEEVersion OneDotThree = new JavaEEVersion( Integer.valueOf( 0 ), VERSION_1_3 );
50  
51      /**
52       * Represents the J2EE 1.4 version.
53       */
54      public static final JavaEEVersion OneDotFour = 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          versionsMap.put( version, this );
80      }
81  
82      public static JavaEEVersion getJavaEEVersion( String version )
83          throws InvalidJavaEEVersion
84      {
85          if ( !isValid( version ) )
86          {
87              throw new InvalidJavaEEVersion( "Invalid version [" + version + "]", version );
88          }
89          return versionsMap.get( version );
90      }
91  
92      /**
93       * Returns the version as a string.
94       * 
95       * @return the version string
96       */
97      public String getVersion()
98      {
99          return version;
100     }
101 
102     /**
103      * Specifies if this version is greater or equal to the specified version.
104      * 
105      * @param version the version to check
106      * @return true if this version is greater or equal to <tt>version</tt>
107      */
108     public boolean ge( JavaEEVersion version )
109     {
110         return this.compareTo( version ) >= 0;
111     }
112 
113     /**
114      * Specifies if this version is greater than the specified version.
115      * 
116      * @param version the version to check
117      * @return true if this version is greater to <tt>version</tt>
118      */
119     public boolean gt( JavaEEVersion version )
120     {
121         return this.compareTo( version ) > 0;
122     }
123 
124     /**
125      * Specifies if this version is equal to the specified version.
126      * 
127      * @param version the version to check
128      * @return true if this version is equal to <tt>version</tt>
129      */
130     public boolean eq( JavaEEVersion version )
131     {
132         return this.compareTo( version ) == 0;
133     }
134 
135     /**
136      * Specifies if this version is less or equal to the specified version.
137      * 
138      * @param version the version to check
139      * @return true if this version is less or equal to <tt>version</tt>
140      */
141     public boolean le( JavaEEVersion version )
142     {
143         return this.compareTo( version ) <= 0;
144     }
145 
146     /**
147      * Specifies if this version is less than the specified version.
148      * 
149      * @param version the version to check
150      * @return true if this version is less or equal to <tt>version</tt>
151      */
152     public boolean lt( JavaEEVersion version )
153     {
154         return this.compareTo( version ) < 0;
155     }
156 
157     /**
158      * Checks if the specified version string is valid.
159      * 
160      * @param version the version string to check
161      * @return <tt>true</tt> if the version is valid
162      */
163     private static boolean isValid( String version )
164     {
165         if ( version == null )
166         {
167             throw new IllegalArgumentException( "version could not be null." );
168         }
169         return VERSION_1_3.equals( version ) || VERSION_1_4.equals( version ) || VERSION_5.equals( version )
170             || VERSION_6.equals( version ) || VERSION_7.equals( version );
171     }
172 
173     public int compareTo( JavaEEVersion otherVersion )
174     {
175         if ( otherVersion == null )
176         {
177             throw new IllegalArgumentException( "other object to compare to could not be null." );
178         }
179         return index.compareTo( otherVersion.index );
180     }
181 }