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.spice.extension;
9 import java.util.StringTokenizer;
10 /***
11 * Utility class to contain version numbers in "Dewey Decimal"
12 * syntax. Numbers in the "Dewey Decimal" syntax consist of positive
13 * decimal integers separated by periods ".". For example, "2.0" or
14 * "1.2.3.4.5.6.7". This allows an extensible number to be used to
15 * represent major, minor, micro, etc versions. The version number
16 * must begin with a number.
17 *
18 * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
19 * @version $Revision: 1.1 $ $Date: 2003/12/02 07:56:59 $
20 */
21 public final class DeweyDecimal
22 {
23 ///Array of components that make up DeweyDecimal
24 private int[] m_components;
25 /***
26 * Construct a DeweyDecimal from an array of integer components.
27 *
28 * @param components an array of integer components.
29 */
30 public DeweyDecimal( final int[] components )
31 {
32 m_components = new int[ components.length ];
33 for( int i = 0; i < m_components.length; i++ )
34 {
35 m_components[ i ] = components[ i ];
36 }
37 }
38 /***
39 * Construct a DeweyDecimal from string in DeweyDecimal format.
40 *
41 * @param string the string in dewey decimal format
42 * @throws java.lang.NumberFormatException if string is malformed
43 */
44 public DeweyDecimal( final String string )
45 throws NumberFormatException
46 {
47 final StringTokenizer tokenizer = new StringTokenizer( string, ".", true );
48 final int size = tokenizer.countTokens();
49 m_components = new int[ ( size + 1 ) / 2 ];
50 for( int i = 0; i < m_components.length; i++ )
51 {
52 final String component = tokenizer.nextToken();
53 if( component.equals( "" ) )
54 {
55 throw new NumberFormatException( "Empty component in string" );
56 }
57 m_components[ i ] = Integer.parseInt( component );
58 //Strip '.' token
59 if( tokenizer.hasMoreTokens() )
60 {
61 tokenizer.nextToken();
62 //If it ended in a dot, throw an exception
63 if( !tokenizer.hasMoreTokens() )
64 {
65 final String message = "DeweyDecimal ended in a '.'";
66 throw new NumberFormatException( message );
67 }
68 }
69 }
70 }
71 /***
72 * Return number of components in <code>DeweyDecimal</code>.
73 *
74 * @return the number of components in dewey decimal
75 */
76 public int getSize()
77 {
78 return m_components.length;
79 }
80 /***
81 * Return the component at specified index.
82 *
83 * @param index the index of components
84 * @return the value of component at index
85 */
86 public int get( final int index )
87 {
88 return m_components[ index ];
89 }
90 /***
91 * Return <code>true</code> if this <code>DeweyDecimal</code> is
92 * equal to the other <code>DeweyDecimal</code>.
93 *
94 * @param other the other DeweyDecimal
95 * @return true if equal to other DeweyDecimal, false otherwise
96 */
97 public boolean isEqual( final DeweyDecimal other )
98 {
99 final int max = Math.max( other.m_components.length, m_components.length );
100 for( int i = 0; i < max; i++ )
101 {
102 final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
103 final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
104 if( component2 != component1 )
105 {
106 return false;
107 }
108 }
109 return true; // Exact match
110 }
111 /***
112 * Return <code>true</code> if this <code>DeweyDecimal</code> is
113 * less than the other <code>DeweyDecimal</code>.
114 *
115 * @param other the other DeweyDecimal
116 * @return true if less than other DeweyDecimal, false otherwise
117 */
118 public boolean isLessThan( final DeweyDecimal other )
119 {
120 return !isGreaterThanOrEqual( other );
121 }
122 /***
123 * Return <code>true</code> if this <code>DeweyDecimal</code> is
124 * less than or equal to the other <code>DeweyDecimal</code>.
125 *
126 * @param other the other DeweyDecimal
127 * @return true if less than or equal to other DeweyDecimal, false otherwise
128 */
129 public boolean isLessThanOrEqual( final DeweyDecimal other )
130 {
131 return !isGreaterThan( other );
132 }
133 /***
134 * Return <code>true</code> if this <code>DeweyDecimal</code> is
135 * greater than the other <code>DeweyDecimal</code>.
136 *
137 * @param other the other DeweyDecimal
138 * @return true if greater than other DeweyDecimal, false otherwise
139 */
140 public boolean isGreaterThan( final DeweyDecimal other )
141 {
142 final int max = Math.max( other.m_components.length, m_components.length );
143 for( int i = 0; i < max; i++ )
144 {
145 final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
146 final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
147 if( component2 > component1 )
148 {
149 return false;
150 }
151 if( component2 < component1 )
152 {
153 return true;
154 }
155 }
156 return false; // Exact match
157 }
158 /***
159 * Return <code>true</code> if this <code>DeweyDecimal</code> is
160 * greater than or equal to the other <code>DeweyDecimal</code>.
161 *
162 * @param other the other DeweyDecimal
163 * @return true if greater than or equal to other DeweyDecimal, false otherwise
164 */
165 public boolean isGreaterThanOrEqual( final DeweyDecimal other )
166 {
167 final int max = Math.max( other.m_components.length, m_components.length );
168 for( int i = 0; i < max; i++ )
169 {
170 final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
171 final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
172 if( component2 > component1 )
173 {
174 return false;
175 }
176 if( component2 < component1 )
177 {
178 return true;
179 }
180 }
181 return true; // Exact match
182 }
183 /***
184 * Return string representation of <code>DeweyDecimal</code>.
185 *
186 * @return the string representation of DeweyDecimal.
187 */
188 public String toString()
189 {
190 final StringBuffer sb = new StringBuffer();
191 for( int i = 0; i < m_components.length; i++ )
192 {
193 if( i != 0 )
194 {
195 sb.append( '.' );
196 }
197 sb.append( m_components[ i ] );
198 }
199 return sb.toString();
200 }
201 }
This page was automatically generated by Maven