1 16 17 package org.springframework.jdbc.support.incrementer; 18 19 import javax.sql.DataSource ; 20 21 import org.springframework.beans.factory.InitializingBean; 22 import org.springframework.dao.DataAccessException; 23 24 33 public abstract class AbstractDataFieldMaxValueIncrementer implements DataFieldMaxValueIncrementer, InitializingBean { 34 35 private DataSource dataSource; 36 37 38 private String incrementerName; 39 40 41 protected int paddingLength = 0; 42 43 44 47 public void setDataSource(DataSource dataSource) { 48 this.dataSource = dataSource; 49 } 50 51 54 public DataSource getDataSource() { 55 return this.dataSource; 56 } 57 58 61 public void setIncrementerName(String incrementerName) { 62 this.incrementerName = incrementerName; 63 } 64 65 68 public String getIncrementerName() { 69 return this.incrementerName; 70 } 71 72 76 public void setPaddingLength(int paddingLength) { 77 this.paddingLength = paddingLength; 78 } 79 80 83 public int getPaddingLength() { 84 return paddingLength; 85 } 86 87 public void afterPropertiesSet() { 88 if (this.dataSource == null) { 89 throw new IllegalArgumentException ("dataSource is required"); 90 } 91 if (this.incrementerName == null) { 92 throw new IllegalArgumentException ("incrementerName is required"); 93 } 94 } 95 96 97 public int nextIntValue() throws DataAccessException { 98 return (int) getNextKey(); 99 } 100 101 public long nextLongValue() throws DataAccessException { 102 return getNextKey(); 103 } 104 105 public String nextStringValue() throws DataAccessException { 106 String s = Long.toString(getNextKey()); 107 int len = s.length(); 108 if (len < this.paddingLength) { 109 StringBuffer buf = new StringBuffer (this.paddingLength); 110 for (int i = 0; i < this.paddingLength - len; i++) { 111 buf.append('0'); 112 } 113 buf.append(s); 114 s = buf.toString(); 115 } 116 return s; 117 } 118 119 124 protected abstract long getNextKey(); 125 126 } 127 | Popular Tags |