1 /* 2 * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, is permitted under the terms of the 6 * GNU General Public License. 7 * 8 * For redistributing this software or a derivative work under a license other 9 * than the GPL-compatible Free Software License as defined by the Free 10 * Software Foundation or approved by OSI, you must first obtain a commercial 11 * license to this software product from rapiddweller GmbH & Volker Bergmann. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS, 15 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE 17 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 package com.rapiddweller.benerator.distribution.sequence; 28 29 import com.rapiddweller.benerator.GeneratorContext; 30 import com.rapiddweller.benerator.primitive.number.AbstractNonNullNumberGenerator; 31 32 /** 33 * Long Generator that implements a 'step' Long Sequence.<br/> 34 * <br/> 35 * Created: 26.07.2007 18:36:45 36 * 37 * @author Volker Bergmann 38 */ 39 public class StepLongGenerator extends AbstractNonNullNumberGenerator<Long> { 40 41 private long increment; 42 private final long initial; 43 44 private long next; 45 46 // constructors ---------------------------------------------------------------------------------------------------- 47 48 /** 49 * Instantiates a new Step long generator. 50 */ 51 public StepLongGenerator() { 52 this(Long.MIN_VALUE, Long.MAX_VALUE); 53 } 54 55 /** 56 * Instantiates a new Step long generator. 57 * 58 * @param min the min 59 * @param max the max 60 */ 61 public StepLongGenerator(long min, long max) { 62 this(min, max, 1); 63 } 64 65 /** 66 * Instantiates a new Step long generator. 67 * 68 * @param min the min 69 * @param max the max 70 * @param increment the increment 71 */ 72 public StepLongGenerator(long min, long max, long increment) { 73 this(min, max, increment, null); 74 } 75 76 /** 77 * Instantiates a new Step long generator. 78 * 79 * @param min the min 80 * @param max the max 81 * @param increment the increment 82 * @param initial the initial 83 */ 84 public StepLongGenerator(long min, Long max, long increment, Long initial) { 85 super(Long.class, min, max, Math.abs(increment)); 86 this.increment = increment; 87 this.initial = (initial != null ? initial : (increment >= 0 ? min : max)); 88 reset(); 89 } 90 91 @Override 92 public void setGranularity(Long granularity) { 93 super.setGranularity(granularity); 94 this.increment = granularity; 95 } 96 97 // Generator implementation ---------------------------------------------------------------------------------------- 98 99 @Override 100 public void init(GeneratorContext context) { 101 reset(); 102 super.init(context); 103 } 104 105 @Override 106 public synchronized Long generate() { 107 if ((increment == 0 || (increment > 0 && (max == null || next <= max)) || (increment < 0 && next >= min))) { 108 long value = next; 109 next += increment; 110 return value; 111 } else { 112 return null; 113 } 114 } 115 116 @Override 117 public synchronized void reset() { 118 next = initial; 119 } 120 121 }