1 17 18 package org.apache.avalon.extension; 19 20 import java.util.StringTokenizer ; 21 22 33 public final class DeweyDecimal 34 { 35 private int[] m_components; 37 38 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 59 public DeweyDecimal( final String string ) 60 throws NumberFormatException 61 { 62 final StringTokenizer tokenizer = new StringTokenizer ( 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 component = tokenizer.nextToken(); 70 if( component.equals( "" ) ) 71 { 72 throw new NumberFormatException ( "Empty component in string" ); 73 } 74 75 m_components[ i ] = Integer.parseInt( component ); 76 77 if( tokenizer.hasMoreTokens() ) 79 { 80 tokenizer.nextToken(); 81 82 if( !tokenizer.hasMoreTokens() ) 84 { 85 throw new NumberFormatException ( "DeweyDecimal ended in a '.'" ); 86 } 87 } 88 } 89 } 90 91 96 public int getSize() 97 { 98 return m_components.length; 99 } 100 101 107 public int get( final int index ) 108 { 109 return m_components[ index ]; 110 } 111 112 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; } 136 137 144 public boolean isLessThan( final DeweyDecimal other ) 145 { 146 return !isGreaterThanOrEqual( other ); 147 } 148 149 156 public boolean isLessThanOrEqual( final DeweyDecimal other ) 157 { 158 return !isGreaterThan( other ); 159 } 160 161 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; } 189 190 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; } 218 219 224 public String toString() 225 { 226 final StringBuffer sb = new StringBuffer (); 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 |