1 8 package org.codehaus.loom.extension; 9 10 import java.util.StringTokenizer ; 11 12 22 public final class DeweyDecimal 23 { 24 private int[] m_components; 26 27 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 47 public DeweyDecimal( final String string ) 48 throws NumberFormatException 49 { 50 final StringTokenizer tokenizer = new StringTokenizer ( 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 component = tokenizer.nextToken(); 58 if( component.equals( "" ) ) 59 { 60 throw new NumberFormatException ( "Empty component in string" ); 61 } 62 m_components[ i ] = Integer.parseInt( component ); 63 if( tokenizer.hasMoreTokens() ) 65 { 66 tokenizer.nextToken(); 67 if( !tokenizer.hasMoreTokens() ) 69 { 70 final String message = "DeweyDecimal ended in a '.'"; 71 throw new NumberFormatException ( message ); 72 } 73 } 74 } 75 } 76 77 82 public int getSize() 83 { 84 return m_components.length; 85 } 86 87 93 public int get( final int index ) 94 { 95 return m_components[ index ]; 96 } 97 98 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; } 124 125 132 public boolean isLessThan( final DeweyDecimal other ) 133 { 134 return !isGreaterThanOrEqual( other ); 135 } 136 137 145 public boolean isLessThanOrEqual( final DeweyDecimal other ) 146 { 147 return !isGreaterThan( other ); 148 } 149 150 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; } 180 181 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; } 212 213 218 public String toString() 219 { 220 final StringBuffer sb = new StringBuffer (); 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 |