KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > extension > manager > impl > OptionalPackageComparator


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.extension.manager.impl;
19
20 import java.util.Comparator JavaDoc;
21
22 import org.apache.avalon.extension.DeweyDecimal;
23 import org.apache.avalon.extension.Extension;
24 import org.apache.avalon.extension.manager.OptionalPackage;
25
26 /**
27  * A simple class to compare two extensions and sort them
28  * on spec version and then on impl version. Unspecified
29  * versions rate lower than specified versions.
30  *
31  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
32  * @version $Revision: 1.2 $ $Date: 2004/02/24 22:39:31 $
33  */

34 class OptionalPackageComparator
35     implements Comparator JavaDoc
36 {
37     /**
38      * The name of extension the comparator is working with.
39      */

40     private final String JavaDoc m_name;
41
42     public OptionalPackageComparator( final String JavaDoc name )
43     {
44         if( null == name )
45         {
46             throw new NullPointerException JavaDoc( "name" );
47         }
48
49         m_name = name;
50     }
51
52     public int compare( final Object JavaDoc o1,
53                         final Object JavaDoc o2 )
54     {
55         final OptionalPackage pkg1 = (OptionalPackage)o1;
56         final OptionalPackage pkg2 = (OptionalPackage)o2;
57         final Extension e1 = getMatchingExtension( pkg1 );
58         final Extension e2 = getMatchingExtension( pkg2 );
59         int result = compareSpecVersion( e1, e2 );
60         if( 0 != result )
61         {
62             return result;
63         }
64         else
65         {
66             return compareImplVersion( e1, e2 );
67         }
68     }
69
70     private Extension getMatchingExtension( final OptionalPackage pkg )
71     {
72         final Extension[] extensions = pkg.getAvailableExtensions();
73         for( int i = 0; i < extensions.length; i++ )
74         {
75             final Extension extension = extensions[ i ];
76             if( extension.getExtensionName().equals( m_name ) )
77             {
78                 return extension;
79             }
80         }
81
82         final String JavaDoc message = "Unable to locate extension " +
83             m_name + " in package " + pkg;
84         throw new IllegalStateException JavaDoc( message );
85     }
86
87     private int compareImplVersion( final Extension e1, final Extension e2 )
88     {
89         final String JavaDoc implVersion1 = e1.getImplementationVersion();
90         final String JavaDoc implVersion2 = e2.getImplementationVersion();
91         if( null == implVersion1 && null == implVersion2 )
92         {
93             return 0;
94         }
95         else if( null != implVersion1 && null == implVersion2 )
96         {
97             return -1;
98         }
99         else if( null == implVersion1 && null != implVersion2 )
100         {
101             return 1;
102         }
103         else
104         {
105             return -implVersion1.compareTo( implVersion2 );
106         }
107     }
108
109     private int compareSpecVersion( final Extension e1,
110                                     final Extension e2 )
111     {
112         final DeweyDecimal specVersion1 = e1.getSpecificationVersion();
113         final DeweyDecimal specVersion2 = e2.getSpecificationVersion();
114         if( null == specVersion1 && null == specVersion2 )
115         {
116             return 0;
117         }
118         else if( null != specVersion1 && null == specVersion2 )
119         {
120             return -1;
121         }
122         else if( null == specVersion1 && null != specVersion2 )
123         {
124             return 1;
125         }
126         else
127         {
128             if( specVersion1.isEqual( specVersion2 ) )
129             {
130                 return 0;
131             }
132             else if( specVersion1.isGreaterThan( specVersion2 ) )
133             {
134                 return -1;
135             }
136             else
137             {
138                 return 1;
139             }
140         }
141     }
142 }
143
Popular Tags