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