Now I have this fluent API for creating random integer arrays. I can say that I want an array of particular length, having a particular minimum/maximum values and using a particular (or default) java.util.Random
. See what I have:
FluentArrays.java:
package net.coderodde.util;
import java.util.Arrays;
import java.util.Random;
/**
* This class facilitates creation of random integer arrays using a fluent API.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Sep 11, 2015)
*/
public class FluentArrays {
/**
* This inner static class is responsible for choosing between integers and
* long integers.
*/
public static class TypeSelector {
/**
* Chooses long integers and returns the array length selector.
*
* @return the array length selector.
*/
public LongLengthSelector ofLongs() {
return new LongLengthSelector();
}
/**
* Chooses integers and returns the array length selector.
*
* @return the array length selector.
*/
public IntLengthSelector ofIntegers() {
return new IntLengthSelector();
}
}
/**
* This inner static class is responsible for choosing the length of a long
* integer array.
*/
public static class LongLengthSelector {
/**
* Chooses the array length and returns the minimum value selector.
*
* @param length the length of the array.
* @return the minimum value selector.
*/
public LongMinimumSelector ofLength(int length) {
checkLength(length);
return new LongMinimumSelector(length);
}
}
/**
* This inner static class is responsible for choosing the length of an
* integer array.
*/
public static class IntLengthSelector {
/**
* Chooses the array length and returns the minimum value selector.
*
* @param length the length of the array.
* @return the minimum value selector.
*/
public IntMinimumSelector ofLength(int length) {
checkLength(length);
return new IntMinimumSelector(length);
}
}
/**
* This inner static class is responsible for choosing the minimum value of
* an array of long integers.
*/
public static class LongMinimumSelector {
/**
* The length of the requested array.
*/
private final int length;
private LongMinimumSelector(int length) {
this.length = length;
}
/**
* Selects the value of the minimum array component.
*
* @param minimum the minimum value allowed.
* @return the maximum value selector.
*/
public LongMaximumSelector withMinimum(long minimum) {
return new LongMaximumSelector(length, minimum);
}
}
/**
* This inner static class is responsible for choosing the minimum value of
* an array of integers.
*/
public static class IntMinimumSelector {
/**
* The length of the requested array.
*/
private final int length;
private IntMinimumSelector(int length) {
this.length = length;
}
/**
* Selects the value of the minimum array component.
*
* @param minimum the minimum value allowed.
* @return the maximum value selector.
*/
public IntMaximumSelector withMinimum(int minimum) {
return new IntMaximumSelector(length, minimum);
}
}
/**
* This inner static class is responsible for choosing the maximum value of
* an array of long integers.
*/
public static class LongMaximumSelector {
/**
* The length of the requested array.
*/
private final int length;
/**
* The minimum requested value.
*/
private final long minimum;
private LongMaximumSelector(int length, long minimum) {
this.length = length;
this.minimum = minimum;
}
/**
* Selects the value of the maximum array component.
*
* @param maximum the maximum value allowed.
* @return the random number generator selector.
*/
public LongRandomSelector withMaximum(long maximum) {
checkMinMax(minimum, maximum);
return new LongRandomSelector(length, minimum, maximum);
}
}
/**
* This inner static class is responsible for choosing the maximum value of
* an array of integers.
*/
public static class IntMaximumSelector {
/**
* The length of the requested array.
*/
private final int length;
/**
* The minimum requested value.
*/
private final int minimum;
private IntMaximumSelector(int length, int minimum) {
this.length = length;
this.minimum = minimum;
}
/**
* Selects the value of the maximum array component.
*
* @param maximum the maximum value allowed.
* @return the random number generator selector.
*/
public IntRandomSelector withMaximum(int maximum) {
checkMinMax(minimum, maximum);
return new IntRandomSelector(length, minimum, maximum);
}
}
/**
* This inner static class is responsible for creating the actual long
* integer arrays.
*/
public static class LongRandomSelector {
/**
* The length of the requested array.
*/
private final int length;
/**
* The minimum requested value.
*/
private final long minimum;
/**
* The maximum requested value.
*/
private final long maximum;
private LongRandomSelector(int length,
long minimum,
long maximum) {
this.length = length;
this.minimum = minimum;
this.maximum = maximum;
}
/**
* Creates the actual array of long integers using a specified random
* number generator.
*
* @param random the random number generator.
* @return the array of long integers.
*/
public long[] withRandom(Random random) {
final long[] array = new long[length];
for (int i = 0; i < length; ++i) {
array[i] = Math.abs(random.nextLong()) %
(maximum - minimum + 1) + minimum;
}
return array;
}
/**
* Create the actual array of long integers using a default random
* number generator.
*
* @return the array of long integers.
*/
public long[] withDefaultRandom() {
return withRandom(new Random());
}
}
/**
* This inner static class is responsible for creating the actual integer
* arrays.
*/
public static class IntRandomSelector {
/**
* The length of the requested array.
*/
private final int length;
/**
* The minimum requested value.
*/
private final int minimum;
/**
* The maximum requested value.
*/
private final int maximum;
private IntRandomSelector(int length,
int minimum,
int maximum) {
this.length = length;
this.minimum = minimum;
this.maximum = maximum;
}
/**
* Creates the actual array of integers using a specified random number
* generator.
*
* @param random the random number generator.
* @return the array of integers.
*/
public int[] withRandom(Random random) {
final int[] array = new int[length];
for (int i = 0; i < length; ++i) {
array[i] = random.nextInt(maximum - minimum + 1) + minimum;
}
return array;
}
/**
* Creates the actual array of integers using a default random number
* generator.
*
* @return the array of integers.
*/
public int[] withDefaultRandom() {
return withRandom(new Random());
}
}
/**
* This class is responsible for initiating a fluent API call.
*
* @return the type selector.
*/
public static TypeSelector createArray() {
return new TypeSelector();
}
private static void checkLength(int length) {
if (length < 0) {
throw new IllegalArgumentException(
"The requested length is negative (" + length + ").");
}
}
private static void checkMinMax(int a, int b) {
if (a > b) {
throw new IllegalArgumentException(
"The requested minimum value " +
"(" + a + ") is greater than the maximum (" + b + ").");
}
}
private static void checkMinMax(long a, long b) {
if (a > b) {
throw new IllegalArgumentException(
"The requested minimum value " +
"(" + a + ") is greater than the maximum (" + b + ").");
}
}
public static void main(String[] args) {
long[] array1 = createArray().ofLongs()
.ofLength(12)
.withMinimum(-100L)
.withMaximum(200L)
.withDefaultRandom();
System.out.println(Arrays.toString(array1));
int[] array2 = createArray().ofIntegers()
.ofLength(15)
.withMinimum(-10)
.withMaximum(20)
.withDefaultRandom();
System.out.println(Arrays.toString(array2));
}
}
So, what do you think?
long
/int
), so it could reduce code duplication. – Spotted Sep 11 '15 at 11:06