View Javadoc
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.domain.math;
28  
29  import com.rapiddweller.benerator.primitive.number.RecurrenceRelationNumberGenerator;
30  
31  /**
32   * Generates numbers according to the <i>Padovan Sequence</i>.
33   * It is defined recursively by
34   * <ul>
35   *   <li>P(0) = P(1) = P(2) = 1</li>
36   *   <li>P(n) = P(n-2) + P(n-3)</li>
37   * </ul>
38   * <br/>
39   * Created at 03.07.2009 13:22:41
40   *
41   * @author Volker Bergmann
42   * @see PadovanSequence
43   * @since 0.6.0
44   */
45  public class PadovanLongGenerator
46      extends RecurrenceRelationNumberGenerator<Long> {
47  
48    private final boolean unique;
49  
50    /**
51     * Instantiates a new Padovan long generator.
52     *
53     * @param min    the min
54     * @param max    the max
55     * @param unique the unique
56     */
57    public PadovanLongGenerator(Long min, Long max, boolean unique) {
58      super(Long.class, 3, min, max);
59      this.unique = unique;
60    }
61  
62    // RecurrenceRelationNumberGenerator interface implementation ------------------------------------------------------
63  
64    @Override
65    protected Long a0(int n) {
66      return 1L;
67    }
68  
69    @Override
70    protected Long aN() {
71      return aN(-2) + aN(-3);
72    }
73  
74    @Override
75    public void reset() {
76      super.reset();
77    }
78  
79    @Override
80    protected void resetMembers() {
81      super.resetMembers();
82      if (unique) {
83        // take out the first two '1' elements
84        generate();
85        generate();
86      }
87    }
88  
89  }