KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > support > incrementer > AbstractDataFieldMaxValueIncrementer


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.support.incrementer;
18
19 import javax.sql.DataSource JavaDoc;
20
21 import org.springframework.beans.factory.InitializingBean;
22 import org.springframework.dao.DataAccessException;
23
24 /**
25  * Implementation of DataFieldMaxValueIncrementer that delegates
26  * to a single getNextKey template method that returns a long.
27  * Uses longs for String values, padding with zeroes if required.
28  *
29  * @author Dmitriy Kopylenko
30  * @author Juergen Hoeller
31  * @author Jean-Pierre Pawlak
32  */

33 public abstract class AbstractDataFieldMaxValueIncrementer implements DataFieldMaxValueIncrementer, InitializingBean {
34
35     private DataSource JavaDoc dataSource;
36
37     /** The name of the sequence/table containing the sequence */
38     private String JavaDoc incrementerName;
39
40     /** The length to which a string result should be pre-pended with zeroes */
41     protected int paddingLength = 0;
42
43
44     /**
45      * Set the data source to retrieve the value from.
46      */

47     public void setDataSource(DataSource JavaDoc dataSource) {
48         this.dataSource = dataSource;
49     }
50
51     /**
52      * Return the data source to retrieve the value from.
53      */

54     public DataSource JavaDoc getDataSource() {
55         return this.dataSource;
56     }
57
58     /**
59      * Set the name of the sequence/table.
60      */

61     public void setIncrementerName(String JavaDoc incrementerName) {
62         this.incrementerName = incrementerName;
63     }
64
65     /**
66      * Return the name of the sequence/table.
67      */

68     public String JavaDoc getIncrementerName() {
69         return this.incrementerName;
70     }
71
72     /**
73      * Set the padding length, i.e. the length to which a string result
74      * should be pre-pended with zeroes.
75      */

76     public void setPaddingLength(int paddingLength) {
77         this.paddingLength = paddingLength;
78     }
79
80     /**
81      * Return the padding length for String values.
82      */

83     public int getPaddingLength() {
84         return paddingLength;
85     }
86
87     public void afterPropertiesSet() {
88         if (this.dataSource == null) {
89             throw new IllegalArgumentException JavaDoc("dataSource is required");
90         }
91         if (this.incrementerName == null) {
92             throw new IllegalArgumentException JavaDoc("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 JavaDoc nextStringValue() throws DataAccessException {
106         String JavaDoc s = Long.toString(getNextKey());
107         int len = s.length();
108         if (len < this.paddingLength) {
109             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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     /**
120      * Determine the next key to use, as a long.
121      * @return the key to use as a long. It will eventually be converted later
122      * in another format by the public concrete methods of this class.
123      */

124     protected abstract long getNextKey();
125
126 }
127
Popular Tags