KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > util > DeweyDecimal


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.util;
9
10 import java.util.StringTokenizer JavaDoc;
11
12 /**
13  * <p>Utility class to contain version numbers in "Dewey Decimal" syntax
14  * that consists of positive decimal integers separated by periods ".",
15  * for example, "2.0" or "1.2.3.4.5.6.7". This allows an extensible number
16  * to be used to represent major, minor, micro, etc versions. The version
17  * number must begin with a number.</p>
18  *
19  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
20  * @version $Revision: 1.3 $ $Date: 2001/12/11 09:53:37 $
21  */

22 public final class DeweyDecimal
23 {
24     ///Array of components that make up DeweyDecimal
25
private int[] m_components;
26
27     /**
28      * Construct a DeweyDecimal from an array of integer components.
29      *
30      * @param components an array of integer components.
31      */

32     public DeweyDecimal( final int[] components )
33     {
34         m_components = new int[ components.length ];
35
36         for( int i = 0; i < m_components.length; i++ )
37         {
38             m_components[ i ] = components[ i ];
39         }
40     }
41
42     /**
43      * Construct a DeweyDecimal from string in DeweyDecimal format.
44      *
45      * @param string the string in dewey decimal format
46      * @exception NumberFormatException if string is malformed
47      */

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

85     public int getSize()
86     {
87         return m_components.length;
88     }
89
90     /**
91      * Return the component at specified index.
92      *
93      * @param index the index of components
94      * @return the value of component at index
95      */

96     public int get( final int index )
97     {
98         return m_components[ index ];
99     }
100
101     /**
102      * Return <code>true</code> if this <code>DeweyDecimal</code> is
103      * equal to the other <code>DeweyDecimal</code>.
104      *
105      * @param other the other DeweyDecimal
106      * @return true if equal to other DeweyDecimal, false otherwise
107      */

108     public boolean isEqual( final DeweyDecimal other )
109     {
110         final int max = Math.max( other.m_components.length, m_components.length );
111
112         for( int i = 0; i < max; i++ )
113         {
114             final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
115             final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
116
117             if( component2 != component1 ) return false;
118         }
119
120         return true; // Exact match
121
}
122
123     /**
124      * Return <code>true</code> if this <code>DeweyDecimal</code> is
125      * less than the other <code>DeweyDecimal</code>.
126      *
127      * @param other the other DeweyDecimal
128      * @return true if less than other DeweyDecimal, false otherwise
129      */

130     public boolean isLessThan( final DeweyDecimal other )
131     {
132         return !isGreaterThanOrEqual( other );
133     }
134
135     /**
136      * Return <code>true</code> if this <code>DeweyDecimal</code> is
137      * less than or equal to the other <code>DeweyDecimal</code>.
138      *
139      * @param other the other DeweyDecimal
140      * @return true if less than or equal to other DeweyDecimal, false otherwise
141      */

142     public boolean isLessThanOrEqual( final DeweyDecimal other )
143     {
144         return !isGreaterThan( other );
145     }
146
147     /**
148      * Return <code>true</code> if this <code>DeweyDecimal</code> is
149      * greater than the other <code>DeweyDecimal</code>.
150      *
151      * @param other the other DeweyDecimal
152      * @return true if greater than other DeweyDecimal, false otherwise
153      */

154     public boolean isGreaterThan( final DeweyDecimal other )
155     {
156         final int max = Math.max( other.m_components.length, m_components.length );
157
158         for( int i = 0; i < max; i++ )
159         {
160             final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
161             final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
162
163             if( component2 > component1 ) return false;
164             if( component2 < component1 ) return true;
165         }
166
167         return false; // Exact match
168
}
169
170     /**
171      * Return <code>true</code> if this <code>DeweyDecimal</code> is
172      * greater than or equal to the other <code>DeweyDecimal</code>.
173      *
174      * @param other the other DeweyDecimal
175      * @return true if greater than or equal to other DeweyDecimal, false otherwise
176      */

177     public boolean isGreaterThanOrEqual( final DeweyDecimal other )
178     {
179         final int max = Math.max( other.m_components.length, m_components.length );
180
181         for( int i = 0; i < max; i++ )
182         {
183             final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
184             final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
185
186             if( component2 > component1 ) return false;
187             if( component2 < component1 ) return true;
188         }
189
190         return true; // Exact match
191
}
192
193     /**
194      * Return string representation of <code>DeweyDecimal</code>.
195      *
196      * @return the string representation of DeweyDecimal.
197      */

198     public String JavaDoc toString()
199     {
200         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
201
202         for( int i = 0; i < m_components.length; i++ )
203         {
204             if( i != 0 ) sb.append( '.' );
205             sb.append( m_components[ i ] );
206         }
207
208         return sb.toString();
209     }
210 }
211
Popular Tags