001    /*
002     * The MIT License
003     *
004     * Copyright (c) 2013, Ninja Squad
005     *
006     * Permission is hereby granted, free of charge, to any person obtaining a copy
007     * of this software and associated documentation files (the "Software"), to deal
008     * in the Software without restriction, including without limitation the rights
009     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010     * copies of the Software, and to permit persons to whom the Software is
011     * furnished to do so, subject to the following conditions:
012     *
013     * The above copyright notice and this permission notice shall be included in
014     * all copies or substantial portions of the Software.
015     *
016     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
022     * THE SOFTWARE.
023     */
024    
025    package com.ninja_squad.dbsetup.generator;
026    
027    /**
028     * A {@link ValueGenerator} which generates a sequence of long values. By default, the sequence starts at 1 and
029     * increments by 1, but this can be customized. Instances of this class are created using
030     * {@link ValueGenerators#sequence()}.
031     * @author JB Nizet
032     */
033    public final class SequenceValueGenerator implements ValueGenerator<Long> {
034    
035        private long next = 1L;
036        private int increment = 1;
037    
038        SequenceValueGenerator() {
039            this(1, 1);
040        }
041    
042        private SequenceValueGenerator(long start, int increment) {
043            this.next = start;
044            this.increment = increment;
045        }
046    
047        /**
048         * Restarts the sequence at the given value
049         * @param start the starting value of the created generator
050         * @return this instance, for chaining
051         */
052        public SequenceValueGenerator startingAt(long start) {
053            this.next = start;
054            return this;
055        }
056    
057        /**
058         * Increments the value by the given increment.
059         * @return this instance, for chaining
060         */
061        public SequenceValueGenerator incrementingBy(int increment) {
062            this.increment = increment;
063            return this;
064        }
065    
066        @Override
067        public Long nextValue() {
068            long result = next;
069            next += increment;
070            return result;
071        }
072    
073        @Override
074        public String toString() {
075            return "SequenceValueGenerator["
076                   + "next="
077                   + next
078                   + ", increment="
079                   + increment
080                   + "]";
081        }
082    }