1 8 package org.apache.avalon.excalibur.util; 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 36 for( int i = 0; i < m_components.length; i++ ) 37 { 38 m_components[ i ] = components[ i ]; 39 } 40 } 41 42 48 public DeweyDecimal( final String string ) 49 throws NumberFormatException 50 { 51 final StringTokenizer tokenizer = new StringTokenizer ( string, ".", true ); 52 final int size = tokenizer.countTokens(); 53 54 m_components = new int[ (size + 1) / 2 ]; 55 56 for( int i = 0; i < m_components.length; i++ ) 57 { 58 final String component = tokenizer.nextToken(); 59 if( component.equals( "" ) ) 60 { 61 throw new NumberFormatException ( "Empty component in string" ); 62 } 63 64 m_components[ i ] = Integer.parseInt( component ); 65 66 if( tokenizer.hasMoreTokens() ) 68 { 69 tokenizer.nextToken(); 70 71 if( !tokenizer.hasMoreTokens() ) 73 { 74 throw new NumberFormatException ( "DeweyDecimal ended in a '.'" ); 75 } 76 } 77 } 78 } 79 80 85 public int getSize() 86 { 87 return m_components.length; 88 } 89 90 96 public int get( final int index ) 97 { 98 return m_components[ index ]; 99 } 100 101 108 public boolean isEqual( final DeweyDecimal other ) 109 { 110 final int max = Math.max( other.m_components.length, m_components.length ); 111 112 for( int i = 0; i < max; i++ ) 113 { 114 final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0; 115 final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0; 116 117 if( component2 != component1 ) return false; 118 } 119 120 return true; } 122 123 130 public boolean isLessThan( final DeweyDecimal other ) 131 { 132 return !isGreaterThanOrEqual( other ); 133 } 134 135 142 public boolean isLessThanOrEqual( final DeweyDecimal other ) 143 { 144 return !isGreaterThan( other ); 145 } 146 147 154 public boolean isGreaterThan( final DeweyDecimal other ) 155 { 156 final int max = Math.max( other.m_components.length, m_components.length ); 157 158 for( int i = 0; i < max; i++ ) 159 { 160 final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0; 161 final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0; 162 163 if( component2 > component1 ) return false; 164 if( component2 < component1 ) return true; 165 } 166 167 return false; } 169 170 177 public boolean isGreaterThanOrEqual( final DeweyDecimal other ) 178 { 179 final int max = Math.max( other.m_components.length, m_components.length ); 180 181 for( int i = 0; i < max; i++ ) 182 { 183 final int component1 = ( i < m_components.length ) ? m_components[ i ] : 0; 184 final int component2 = ( i < other.m_components.length ) ? other.m_components[ i ] : 0; 185 186 if( component2 > component1 ) return false; 187 if( component2 < component1 ) return true; 188 } 189 190 return true; } 192 193 198 public String toString() 199 { 200 final StringBuffer sb = new StringBuffer (); 201 202 for( int i = 0; i < m_components.length; i++ ) 203 { 204 if( i != 0 ) sb.append( '.' ); 205 sb.append( m_components[ i ] ); 206 } 207 208 return sb.toString(); 209 } 210 } 211 | Popular Tags |