|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DeweyDecimal.java | 13.3% | 15.7% | 10% | 14.3% |
|
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 | 0 |
public DeweyDecimal( final int[] components ) |
31 |
{ |
|
32 | 0 |
m_components = new int[ components.length ]; |
33 | 0 |
for( int i = 0; i < m_components.length; i++ ) |
34 |
{ |
|
35 | 0 |
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 | 1 |
public DeweyDecimal( final String string )
|
45 |
throws NumberFormatException
|
|
46 |
{ |
|
47 | 1 |
final StringTokenizer tokenizer = new StringTokenizer( string, ".", true ); |
48 | 1 |
final int size = tokenizer.countTokens();
|
49 | 1 |
m_components = new int[ ( size + 1 ) / 2 ]; |
50 | 1 |
for( int i = 0; i < m_components.length; i++ ) |
51 |
{ |
|
52 | 1 |
final String component = tokenizer.nextToken(); |
53 | 1 |
if( component.equals( "" ) ) |
54 |
{ |
|
55 | 0 |
throw new NumberFormatException( "Empty component in string" ); |
56 |
} |
|
57 | 1 |
m_components[ i ] = Integer.parseInt( component ); |
58 |
//Strip '.' token
|
|
59 | 1 |
if( tokenizer.hasMoreTokens() )
|
60 |
{ |
|
61 | 0 |
tokenizer.nextToken(); |
62 |
//If it ended in a dot, throw an exception
|
|
63 | 0 |
if( !tokenizer.hasMoreTokens() )
|
64 |
{ |
|
65 | 0 |
final String message = "DeweyDecimal ended in a '.'";
|
66 | 0 |
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 | 0 |
public int getSize() |
77 |
{ |
|
78 | 0 |
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 | 0 |
public int get( final int index ) |
87 |
{ |
|
88 | 0 |
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 | 0 |
public boolean isEqual( final DeweyDecimal other ) |
98 |
{ |
|
99 | 0 |
final int max = Math.max( other.m_components.length, m_components.length );
|
100 | 0 |
for( int i = 0; i < max; i++ ) |
101 |
{ |
|
102 | 0 |
final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
|
103 | 0 |
final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
|
104 | 0 |
if( component2 != component1 )
|
105 |
{ |
|
106 | 0 |
return false; |
107 |
} |
|
108 |
} |
|
109 | 0 |
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 | 0 |
public boolean isLessThan( final DeweyDecimal other ) |
119 |
{ |
|
120 | 0 |
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 | 0 |
public boolean isLessThanOrEqual( final DeweyDecimal other ) |
130 |
{ |
|
131 | 0 |
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 | 0 |
public boolean isGreaterThan( final DeweyDecimal other ) |
141 |
{ |
|
142 | 0 |
final int max = Math.max( other.m_components.length, m_components.length );
|
143 | 0 |
for( int i = 0; i < max; i++ ) |
144 |
{ |
|
145 | 0 |
final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
|
146 | 0 |
final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
|
147 | 0 |
if( component2 > component1 )
|
148 |
{ |
|
149 | 0 |
return false; |
150 |
} |
|
151 | 0 |
if( component2 < component1 )
|
152 |
{ |
|
153 | 0 |
return true; |
154 |
} |
|
155 |
} |
|
156 | 0 |
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 | 0 |
public boolean isGreaterThanOrEqual( final DeweyDecimal other ) |
166 |
{ |
|
167 | 0 |
final int max = Math.max( other.m_components.length, m_components.length );
|
168 | 0 |
for( int i = 0; i < max; i++ ) |
169 |
{ |
|
170 | 0 |
final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0;
|
171 | 0 |
final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0;
|
172 | 0 |
if( component2 > component1 )
|
173 |
{ |
|
174 | 0 |
return false; |
175 |
} |
|
176 | 0 |
if( component2 < component1 )
|
177 |
{ |
|
178 | 0 |
return true; |
179 |
} |
|
180 |
} |
|
181 | 0 |
return true; // Exact match |
182 |
} |
|
183 |
/**
|
|
184 |
* Return string representation of <code>DeweyDecimal</code>.
|
|
185 |
*
|
|
186 |
* @return the string representation of DeweyDecimal.
|
|
187 |
*/
|
|
188 | 0 |
public String toString()
|
189 |
{ |
|
190 | 0 |
final StringBuffer sb = new StringBuffer();
|
191 | 0 |
for( int i = 0; i < m_components.length; i++ ) |
192 |
{ |
|
193 | 0 |
if( i != 0 )
|
194 |
{ |
|
195 | 0 |
sb.append( '.' ); |
196 |
} |
|
197 | 0 |
sb.append( m_components[ i ] ); |
198 |
} |
|
199 | 0 |
return sb.toString();
|
200 |
} |
|
201 |
} |
|
202 |
|
|