001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jdbc.support.incrementer;
018
019import javax.sql.DataSource;
020
021import org.springframework.util.Assert;
022
023/**
024 * Abstract base class for {@link DataFieldMaxValueIncrementer} implementations that use
025 * a column in a custom sequence table. Subclasses need to provide the specific handling
026 * of that table in their {@link #getNextKey()} implementation.
027 *
028 * @author Juergen Hoeller
029 * @since 2.5.3
030 */
031public abstract class AbstractColumnMaxValueIncrementer extends AbstractDataFieldMaxValueIncrementer {
032
033        /** The name of the column for this sequence. */
034        private String columnName;
035
036        /** The number of keys buffered in a cache. */
037        private int cacheSize = 1;
038
039
040        /**
041         * Default constructor for bean property style usage.
042         * @see #setDataSource
043         * @see #setIncrementerName
044         * @see #setColumnName
045         */
046        public AbstractColumnMaxValueIncrementer() {
047        }
048
049        /**
050         * Convenience constructor.
051         * @param dataSource the DataSource to use
052         * @param incrementerName the name of the sequence/table to use
053         * @param columnName the name of the column in the sequence table to use
054         */
055        public AbstractColumnMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) {
056                super(dataSource, incrementerName);
057                Assert.notNull(columnName, "Column name must not be null");
058                this.columnName = columnName;
059        }
060
061
062        /**
063         * Set the name of the column in the sequence table.
064         */
065        public void setColumnName(String columnName) {
066                this.columnName = columnName;
067        }
068
069        /**
070         * Return the name of the column in the sequence table.
071         */
072        public String getColumnName() {
073                return this.columnName;
074        }
075
076        /**
077         * Set the number of buffered keys.
078         */
079        public void setCacheSize(int cacheSize) {
080                this.cacheSize = cacheSize;
081        }
082
083        /**
084         * Return the number of buffered keys.
085         */
086        public int getCacheSize() {
087                return this.cacheSize;
088        }
089
090        @Override
091        public void afterPropertiesSet() {
092                super.afterPropertiesSet();
093                if (this.columnName == null) {
094                        throw new IllegalArgumentException("Property 'columnName' is required");
095                }
096        }
097
098}