View Javadoc

1   package org.apache.maven.plugin.ear.util;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   /**
7    * Represents the supported JavaEE version.
8    *
9    * @author Stephane Nicoll
10   */
11  public class JavaEEVersion
12      implements Comparable
13  {
14  
15      private static final String VERSION_1_3 = "1.3";
16  
17      private static final String VERSION_1_4 = "1.4";
18  
19      private static final String VERSION_5 = "5";
20  
21      private static final String VERSION_6 = "6";
22  
23      private static final Map versionsMap = new HashMap();
24  
25  
26      /**
27       * Represents the J2EE 1.3 version.
28       */
29      public static final JavaEEVersion OneDotThree = new JavaEEVersion( new Integer( 0 ), VERSION_1_3 );
30  
31      /**
32       * Represents the J2EE 1.4 version.
33       */
34      public static final JavaEEVersion OneDotFour = new JavaEEVersion( new Integer( 1 ), VERSION_1_4 );
35  
36      /**
37       * Represents the JavaEE 5 version.
38       */
39      public static final JavaEEVersion Five = new JavaEEVersion( new Integer( 2 ), VERSION_5 );
40  
41      /**
42       * Represents the JavaEE 7 version.
43       */
44      public static final JavaEEVersion Six = new JavaEEVersion( new Integer( 3 ), VERSION_6 );
45  
46  
47      private final Integer index;
48  
49      private final String version;
50  
51      private JavaEEVersion( Integer index, String version )
52      {
53          this.index = index;
54          this.version = version;
55          versionsMap.put( version, this );
56      }
57  
58      public static JavaEEVersion getJavaEEVersion( String version )
59          throws InvalidJavaEEVersion
60      {
61          if ( !isValid( version ) )
62          {
63              throw new InvalidJavaEEVersion( "Invalid version [" + version + "]", version );
64          }
65          return (JavaEEVersion) versionsMap.get( version );
66      }
67  
68      /**
69       * Returns the version as a string.
70       *
71       * @return the version string
72       */
73      public String getVersion()
74      {
75          return version;
76      }
77  
78      /**
79       * Specifies if this version is greater or equal to the specified version.
80       *
81       * @param version the version to check
82       * @return true if this version is greater or equal to <tt>version</tt>
83       */
84      public boolean ge( JavaEEVersion version )
85      {
86          return this.compareTo( version ) >= 0;
87      }
88  
89      /**
90       * Specifies if this version is greater than the specified version.
91       *
92       * @param version the version to check
93       * @return true if this version is greater to <tt>version</tt>
94       */
95      public boolean gt( JavaEEVersion version )
96      {
97          return this.compareTo( version ) > 0;
98      }
99  
100     /**
101      * Specifies if this version is equal to the specified version.
102      *
103      * @param version the version to check
104      * @return true if this version is equal to <tt>version</tt>
105      */
106     public boolean eq( JavaEEVersion version )
107     {
108         return this.compareTo( version ) == 0;
109     }
110 
111     /**
112      * Specifies if this version is less or equal to the specified version.
113      *
114      * @param version the version to check
115      * @return true if this version is less or equal to <tt>version</tt>
116      */
117     public boolean le( JavaEEVersion version )
118     {
119         return this.compareTo( version ) <= 0;
120     }
121 
122 
123     /**
124      * Specifies if this version is less than the specified version.
125      *
126      * @param version the version to check
127      * @return true if this version is less or equal to <tt>version</tt>
128      */
129     public boolean lt( JavaEEVersion version )
130     {
131         return this.compareTo( version ) < 0;
132     }
133 
134     /**
135      * Checks if the specified version string is valid.
136      *
137      * @param version the version string to check
138      * @return <tt>true</tt> if the version is valid
139      */
140     private static boolean isValid( String version )
141     {
142         if ( version == null )
143         {
144             throw new IllegalArgumentException( "version could not be null." );
145         }
146         return VERSION_1_3.equals( version ) || VERSION_1_4.equals( version ) || VERSION_5.equals( version ) ||
147             VERSION_6.equals( version );
148     }
149 
150     public int compareTo( Object other )
151     {
152         if ( other == null )
153         {
154             throw new IllegalArgumentException( "other object to compare to could not be null." );
155         }
156         if ( !( other instanceof JavaEEVersion ) )
157         {
158             throw new IllegalArgumentException(
159                 "other object to compare must be a JavaEEVersion but was [" + other.getClass().getName() + "]" );
160         }
161         final JavaEEVersion otherVersion = (JavaEEVersion) other;
162         return index.compareTo( otherVersion.index );
163     }
164 }