KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > extension > DeweyDecimal


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;
19
20 import java.util.StringTokenizer JavaDoc;
21
22 /**
23  * Utility class to contain version numbers in "Dewey Decimal"
24  * syntax. Numbers in the "Dewey Decimal" syntax consist of positive
25  * decimal integers separated by periods ".". For example, "2.0" or
26  * "1.2.3.4.5.6.7". This allows an extensible number to be used to
27  * represent major, minor, micro, etc versions. The version number
28  * must begin with a number.
29  *
30  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
31  * @version $Revision: 1.2 $ $Date: 2004/02/24 22:39:31 $
32  */

33 public final class DeweyDecimal
34 {
35     ///Array of components that make up DeweyDecimal
36
private int[] m_components;
37
38     /**
39      * Construct a DeweyDecimal from an array of integer components.
40      *
41      * @param components an array of integer components.
42      */

43     public DeweyDecimal( final int[] components )
44     {
45         m_components = new int[ components.length ];
46
47         for( int i = 0; i < m_components.length; i++ )
48         {
49             m_components[ i ] = components[ i ];
50         }
51     }
52
53     /**
54      * Construct a DeweyDecimal from string in DeweyDecimal format.
55      *
56      * @param string the string in dewey decimal format
57      * @throws NumberFormatException if string is malformed
58      */

59     public DeweyDecimal( final String JavaDoc string )
60         throws NumberFormatException JavaDoc
61     {
62         final StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( string, ".", true );
63         final int size = tokenizer.countTokens();
64
65         m_components = new int[ ( size + 1 ) / 2 ];
66
67         for( int i = 0; i < m_components.length; i++ )
68         {
69             final String JavaDoc component = tokenizer.nextToken();
70             if( component.equals( "" ) )
71             {
72                 throw new NumberFormatException JavaDoc( "Empty component in string" );
73             }
74
75             m_components[ i ] = Integer.parseInt( component );
76
77             //Strip '.' token
78
if( tokenizer.hasMoreTokens() )
79             {
80                 tokenizer.nextToken();
81
82                 //If it ended in a dot, throw an exception
83
if( !tokenizer.hasMoreTokens() )
84                 {
85                     throw new NumberFormatException JavaDoc( "DeweyDecimal ended in a '.'" );
86                 }
87             }
88         }
89     }
90
91     /**
92      * Return number of components in <code>DeweyDecimal</code>.
93      *
94      * @return the number of components in dewey decimal
95      */

96     public int getSize()
97     {
98         return m_components.length;
99     }
100
101     /**
102      * Return the component at specified index.
103      *
104      * @param index the index of components
105      * @return the value of component at index
106      */

107     public int get( final int index )
108     {
109         return m_components[ index ];
110     }
111
112     /**
113      * Return <code>true</code> if this <code>DeweyDecimal</code> is
114      * equal to the other <code>DeweyDecimal</code>.
115      *
116      * @param other the other DeweyDecimal
117      * @return true if equal to other DeweyDecimal, false otherwise
118      */

119     public boolean isEqual( final DeweyDecimal other )
120     {
121         final int max = Math.max( other.m_components.length, m_components.length );
122
123         for( int i = 0; i < max; i++ )
124         {
125             final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
126             final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
127
128             if( component2 != component1 )
129             {
130                 return false;
131             }
132         }
133
134         return true; // Exact match
135
}
136
137     /**
138      * Return <code>true</code> if this <code>DeweyDecimal</code> is
139      * less than the other <code>DeweyDecimal</code>.
140      *
141      * @param other the other DeweyDecimal
142      * @return true if less than other DeweyDecimal, false otherwise
143      */

144     public boolean isLessThan( final DeweyDecimal other )
145     {
146         return !isGreaterThanOrEqual( other );
147     }
148
149     /**
150      * Return <code>true</code> if this <code>DeweyDecimal</code> is
151      * less than or equal to the other <code>DeweyDecimal</code>.
152      *
153      * @param other the other DeweyDecimal
154      * @return true if less than or equal to other DeweyDecimal, false otherwise
155      */

156     public boolean isLessThanOrEqual( final DeweyDecimal other )
157     {
158         return !isGreaterThan( other );
159     }
160
161     /**
162      * Return <code>true</code> if this <code>DeweyDecimal</code> is
163      * greater than the other <code>DeweyDecimal</code>.
164      *
165      * @param other the other DeweyDecimal
166      * @return true if greater than other DeweyDecimal, false otherwise
167      */

168     public boolean isGreaterThan( final DeweyDecimal other )
169     {
170         final int max = Math.max( other.m_components.length, m_components.length );
171
172         for( int i = 0; i < max; i++ )
173         {
174             final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
175             final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
176
177             if( component2 > component1 )
178             {
179                 return false;
180             }
181             if( component2 < component1 )
182             {
183                 return true;
184             }
185         }
186
187         return false; // Exact match
188
}
189
190     /**
191      * Return <code>true</code> if this <code>DeweyDecimal</code> is
192      * greater than or equal to the other <code>DeweyDecimal</code>.
193      *
194      * @param other the other DeweyDecimal
195      * @return true if greater than or equal to other DeweyDecimal, false otherwise
196      */

197     public boolean isGreaterThanOrEqual( final DeweyDecimal other )
198     {
199         final int max = Math.max( other.m_components.length, m_components.length );
200
201         for( int i = 0; i < max; i++ )
202         {
203             final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
204             final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
205
206             if( component2 > component1 )
207             {
208                 return false;
209             }
210             if( component2 < component1 )
211             {
212                 return true;
213             }
214         }
215
216         return true; // Exact match
217
}
218
219     /**
220      * Return string representation of <code>DeweyDecimal</code>.
221      *
222      * @return the string representation of DeweyDecimal.
223      */

224     public String JavaDoc toString()
225     {
226         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
227
228         for( int i = 0; i < m_components.length; i++ )
229         {
230             if( i != 0 )
231             {
232                 sb.append( '.' );
233             }
234             sb.append( m_components[ i ] );
235         }
236
237         return sb.toString();
238     }
239 }
240
Popular Tags