|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2014 Ilkka Seppälä |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | +package com.iluwatar.hexagonal.domain; |
| 24 | + |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.PrimitiveIterator; |
| 28 | +import java.util.Random; |
| 29 | +import java.util.Set; |
| 30 | + |
| 31 | +/** |
| 32 | + * |
| 33 | + * Value object representing lottery numbers. This lottery uses sets of 4 numbers. The numbers must be unique and |
| 34 | + * between 1 and 20. |
| 35 | + * |
| 36 | + */ |
| 37 | +public class LotteryNumbers { |
| 38 | + |
| 39 | + private final Set<Integer> numbers; |
| 40 | + |
| 41 | + public static final int MIN_NUMBER = 1; |
| 42 | + public static final int MAX_NUMBER = 20; |
| 43 | + public static final int NUM_NUMBERS = 4; |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructor. Creates random lottery numbers. |
| 47 | + */ |
| 48 | + private LotteryNumbers() { |
| 49 | + numbers = new HashSet<>(); |
| 50 | + generateRandomNumbers(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Constructor. Uses given numbers. |
| 55 | + */ |
| 56 | + private LotteryNumbers(Set<Integer> givenNumbers) { |
| 57 | + numbers = new HashSet<>(); |
| 58 | + numbers.addAll(givenNumbers); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return random LotteryNumbers |
| 63 | + */ |
| 64 | + public static LotteryNumbers createRandom() { |
| 65 | + return new LotteryNumbers(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return given LotteryNumbers |
| 70 | + */ |
| 71 | + public static LotteryNumbers create(Set<Integer> givenNumbers) { |
| 72 | + return new LotteryNumbers(givenNumbers); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return lottery numbers |
| 77 | + */ |
| 78 | + public Set<Integer> getNumbers() { |
| 79 | + return Collections.unmodifiableSet(numbers); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public int hashCode() { |
| 84 | + final int prime = 31; |
| 85 | + int result = 1; |
| 86 | + result = prime * result + ((numbers == null) ? 0 : numbers.hashCode()); |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean equals(Object obj) { |
| 92 | + if (this == obj) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + if (obj == null) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + if (getClass() != obj.getClass()) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + LotteryNumbers other = (LotteryNumbers) obj; |
| 102 | + if (numbers == null) { |
| 103 | + if (other.numbers != null) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + } else if (!numbers.equals(other.numbers)) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Generates 4 unique random numbers between 1-20 into numbers set. |
| 114 | + */ |
| 115 | + private void generateRandomNumbers() { |
| 116 | + numbers.clear(); |
| 117 | + RandomNumberGenerator generator = new RandomNumberGenerator(MIN_NUMBER, MAX_NUMBER); |
| 118 | + while (numbers.size() < NUM_NUMBERS) { |
| 119 | + int num = generator.nextInt(); |
| 120 | + if (!numbers.contains(num)) { |
| 121 | + numbers.add(num); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * |
| 128 | + * Helper class for generating random numbers. |
| 129 | + * |
| 130 | + */ |
| 131 | + private static class RandomNumberGenerator { |
| 132 | + |
| 133 | + private PrimitiveIterator.OfInt randomIterator; |
| 134 | + |
| 135 | + /** |
| 136 | + * Initialize a new random number generator that generates random numbers in the range [min, max] |
| 137 | + * |
| 138 | + * @param min the min value (inclusive) |
| 139 | + * @param max the max value (inclusive) |
| 140 | + */ |
| 141 | + public RandomNumberGenerator(int min, int max) { |
| 142 | + randomIterator = new Random().ints(min, max + 1).iterator(); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @return a random number in the range (min, max) |
| 147 | + */ |
| 148 | + public int nextInt() { |
| 149 | + return randomIterator.nextInt(); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments