1 19 20 package org.apache.avalon.excalibur.datasource.ids; 21 22 import java.math.BigDecimal ; 23 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 27 36 public abstract class AbstractDataSourceBlockIdGenerator 37 extends AbstractDataSourceIdGenerator 38 { 39 42 private BigDecimal m_firstBigDecimal; 43 44 47 private long m_firstLong; 48 49 52 private int m_blockSize; 53 54 57 private int m_allocated; 58 59 62 public AbstractDataSourceBlockIdGenerator() 63 { 64 } 65 66 69 78 protected abstract BigDecimal allocateBigDecimalIdBlock( int blockSize ) 79 throws IdException; 80 81 90 protected abstract long allocateLongIdBlock( int blockSize ) 91 throws IdException; 92 93 96 104 protected BigDecimal getNextBigDecimalIdInner() 105 throws IdException 106 { 107 if( m_allocated >= m_blockSize ) 108 { 109 try 111 { 112 m_firstBigDecimal = allocateBigDecimalIdBlock( m_blockSize ); 113 114 m_allocated = 0; 116 } 117 catch( IdException e ) 118 { 119 m_allocated = Integer.MAX_VALUE; 121 throw e; 122 } 123 } 124 125 BigDecimal id = m_firstBigDecimal.add( new BigDecimal ( m_allocated ) ); 128 m_allocated++; 129 130 return id; 131 } 132 133 141 protected long getNextLongIdInner() 142 throws IdException 143 { 144 if( m_allocated >= m_blockSize ) 145 { 146 try 148 { 149 m_firstLong = allocateLongIdBlock( m_blockSize ); 150 151 m_allocated = 0; 153 } 154 catch( IdException e ) 155 { 156 m_allocated = Integer.MAX_VALUE; 158 throw e; 159 } 160 } 161 162 long id = m_firstLong + m_allocated; 165 if( id < 0 ) 166 { 167 String msg = "No more Ids are available, the maximum long value has been reached."; 169 getLogger().error( msg ); 170 throw new IdException( msg ); 171 } 172 m_allocated++; 173 174 return id; 175 } 176 177 180 187 public void configure( Configuration configuration ) 188 throws ConfigurationException 189 { 190 super.configure( configuration ); 191 192 m_blockSize = configuration.getAttributeAsInteger( "block-size", 10 ); 194 } 195 196 199 204 public void initialize() 205 throws Exception 206 { 207 super.initialize(); 208 209 m_allocated = Integer.MAX_VALUE; 211 } 212 } 213 214 | Popular Tags |