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 import com.ninja_squad.dbsetup.util.Preconditions; 028 029 import javax.annotation.Nonnull; 030 import javax.annotation.Nullable; 031 032 /** 033 * Utility class containing factory methods for {@link ValueGenerator} 034 * @author JB Nizet 035 */ 036 public final class ValueGenerators { 037 private ValueGenerators() { 038 } 039 040 /** 041 * Returns a value generator which generates a sequence of long values starting with 1, with an increment of 1. 042 * The starting value and increment can be customized using 043 * <pre> 044 * ValueGenerators.increment().startingAt(1000).incrementingBy(5) 045 * </pre> 046 */ 047 public static SequenceValueGenerator sequence() { 048 return new SequenceValueGenerator(); 049 } 050 051 /** 052 * Returns a value generator which always returns the same, given value. 053 */ 054 public static <T> ValueGenerator<T> constant(@Nullable final T constant) { 055 return new ValueGenerator<T>() { 056 @Override 057 public T nextValue() { 058 return constant; 059 } 060 061 @Override 062 public String toString() { 063 return "ValueGenerators.constant(" + constant + ")"; 064 } 065 }; 066 } 067 068 /** 069 * Returns a value generator that returns a string prefix followed by a sequence number, optionally left-padded 070 * with 0 to ensure a correct ordering (for example: CODE_001, CODE_002, etc.). The returned generator starts the 071 * sequence at 1 and increments by 1, and doesn't pad the numbers. 072 * @param prefix the prefix before the generated number (for example: <code>"CODE_"</code>). 073 */ 074 public static StringSequenceValueGenerator stringSequence(@Nonnull String prefix) { 075 Preconditions.checkNotNull(prefix, "prefix may not be null"); 076 return new StringSequenceValueGenerator(prefix); 077 } 078 079 /** 080 * Returns a value generator that returns a sequence of dates, starting at a given date and incremented by a given 081 * time, specified as an increment and a calendar field. The returned generator starts today at 00:00:00 082 * and increments by 1 day by default. 083 */ 084 public static DateSequenceValueGenerator dateSequence() { 085 return new DateSequenceValueGenerator(); 086 } 087 }