patternjavaMinor
Fluent API for creating random integer arrays in Java
Viewed 0 times
randomarrayscreatingjavaapiforfluentinteger
Problem
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)
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.
*
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.
*
Solution
Ouch. That's a lot of code to do things that are already available in the native Java libraries. Also, while the usage code "looks" neat, writing it is a PITA because it returns a different object at each step, and is not really a fluent API at all because the order of method calls is pre-defined.
Regardless, your code is complicated to write, it's inconvenient to use because you need to write a lot of code to link a number of highly structured method calls that are in an inflexible call order.
The above example code, for me, would be written better, as:
If you want a non-default random, you can just use that instance:
The Stream API is more than enough when it comes to "fluent".
Also, if you don't want to include the size in the top call, you can limit the stream too:
Of course, you can do the same with ints.... (and doubles).
StringBuilder is an example where append(...) returns the same instance so you can keep appending and so on.... that's "fluent".Regardless, your code is complicated to write, it's inconvenient to use because you need to write a lot of code to link a number of highly structured method calls that are in an inflexible call order.
long[] array1 = createArray().ofLongs()
.ofLength(12)
.withMinimum(-100L)
.withMaximum(200L)
.withDefaultRandom();The above example code, for me, would be written better, as:
long[] array = ThreadLocalRandom.longs(12, -100, 200).toArray();If you want a non-default random, you can just use that instance:
Random rand = new Random();
long[] more = rand.longs(12, -100, 200).toArray();The Stream API is more than enough when it comes to "fluent".
Also, if you don't want to include the size in the top call, you can limit the stream too:
long[] more = rand.longs(-100, 200).limit(12).toArray();Of course, you can do the same with ints.... (and doubles).
int[] ints = rand.ints(-100, 200).limit(12).toArray();Code Snippets
long[] array1 = createArray().ofLongs()
.ofLength(12)
.withMinimum(-100L)
.withMaximum(200L)
.withDefaultRandom();long[] array = ThreadLocalRandom.longs(12, -100, 200).toArray();Random rand = new Random();
long[] more = rand.longs(12, -100, 200).toArray();long[] more = rand.longs(-100, 200).limit(12).toArray();int[] ints = rand.ints(-100, 200).limit(12).toArray();Context
StackExchange Code Review Q#104396, answer score: 8
Revisions (0)
No revisions yet.