KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > extension > DeweyDecimal


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

8 package org.codehaus.loom.extension;
9
10 import java.util.StringTokenizer JavaDoc;
11
12 /**
13  * Utility class to contain version numbers in "Dewey Decimal" syntax. Numbers
14  * in the "Dewey Decimal" syntax consist of positive decimal integers separated
15  * by periods ".". For example, "2.0" or "1.2.3.4.5.6.7". This allows an
16  * extensible number to be used to represent major, minor, micro, etc versions.
17  * The version number must begin with a number.
18  *
19  * @author Peter Donald
20  * @version $Revision: 1.1 $ $Date: 2004/04/19 21:40:38 $
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         for( int i = 0; i < m_components.length; i++ )
36         {
37             m_components[ i ] = components[ i ];
38         }
39     }
40
41     /**
42      * Construct a DeweyDecimal from string in DeweyDecimal format.
43      *
44      * @param string the string in dewey decimal format
45      * @throws java.lang.NumberFormatException if string is malformed
46      */

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

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

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

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

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

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

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

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

218     public String JavaDoc toString()
219     {
220         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
221         for( int i = 0; i < m_components.length; i++ )
222         {
223             if( i != 0 )
224             {
225                 sb.append( '.' );
226             }
227             sb.append( m_components[ i ] );
228         }
229         return sb.toString();
230     }
231 }
232
Popular Tags