Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. package net.coderodde.util;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Random;
  5.  
  6. /**
  7. * This class facilitates creation of random integer arrays using a fluent API.
  8. *
  9. * @author Rodion "rodde" Efremov
  10. * @version 1.6 (Sep 11, 2015)
  11. */
  12. public class FluentArrays {
  13.  
  14. /**
  15. * This inner static class is responsible for choosing between integers and
  16. * long integers.
  17. */
  18. public static class TypeSelector {
  19.  
  20. /**
  21. * Chooses long integers and returns the array length selector.
  22. *
  23. * @return the array length selector.
  24. */
  25. public LongLengthSelector ofLongs() {
  26. return new LongLengthSelector();
  27. }
  28.  
  29. /**
  30. * Chooses integers and returns the array length selector.
  31. *
  32. * @return the array length selector.
  33. */
  34. public IntLengthSelector ofIntegers() {
  35. return new IntLengthSelector();
  36. }
  37. }
  38.  
  39. /**
  40. * This inner static class is responsible for choosing the length of a long
  41. * integer array.
  42. */
  43. public static class LongLengthSelector {
  44.  
  45. /**
  46. * Chooses the array length and returns the minimum value selector.
  47. *
  48. * @param length the length of the array.
  49. * @return the minimum value selector.
  50. */
  51. public LongMinimumSelector ofLength(int length) {
  52. checkLength(length);
  53. return new LongMinimumSelector(length);
  54. }
  55. }
  56.  
  57. /**
  58. * This inner static class is responsible for choosing the length of an
  59. * integer array.
  60. */
  61. public static class IntLengthSelector {
  62.  
  63. /**
  64. * Chooses the array length and returns the minimum value selector.
  65. *
  66. * @param length the length of the array.
  67. * @return the minimum value selector.
  68. */
  69. public IntMinimumSelector ofLength(int length) {
  70. checkLength(length);
  71. return new IntMinimumSelector(length);
  72. }
  73. }
  74.  
  75. /**
  76. * This inner static class is responsible for choosing the minimum value of
  77. * an array of long integers.
  78. */
  79. public static class LongMinimumSelector {
  80.  
  81. /**
  82. * The length of the requested array.
  83. */
  84. private final int length;
  85.  
  86. private LongMinimumSelector(int length) {
  87. this.length = length;
  88. }
  89.  
  90. /**
  91. * Selects the value of the minimum array component.
  92. *
  93. * @param minimum the minimum value allowed.
  94. * @return the maximum value selector.
  95. */
  96. public LongMaximumSelector withMinimum(long minimum) {
  97. return new LongMaximumSelector(length, minimum);
  98. }
  99. }
  100.  
  101. /**
  102. * This inner static class is responsible for choosing the minimum value of
  103. * an array of integers.
  104. */
  105. public static class IntMinimumSelector {
  106.  
  107. /**
  108. * The length of the requested array.
  109. */
  110. private final int length;
  111.  
  112. private IntMinimumSelector(int length) {
  113. this.length = length;
  114. }
  115.  
  116. /**
  117. * Selects the value of the minimum array component.
  118. *
  119. * @param minimum the minimum value allowed.
  120. * @return the maximum value selector.
  121. */
  122. public IntMaximumSelector withMinimum(int minimum) {
  123. return new IntMaximumSelector(length, minimum);
  124. }
  125. }
  126.  
  127. /**
  128. * This inner static class is responsible for choosing the maximum value of
  129. * an array of long integers.
  130. */
  131. public static class LongMaximumSelector {
  132.  
  133. /**
  134. * The length of the requested array.
  135. */
  136. private final int length;
  137.  
  138. /**
  139. * The minimum requested value.
  140. */
  141. private final long minimum;
  142.  
  143. private LongMaximumSelector(int length, long minimum) {
  144. this.length = length;
  145. this.minimum = minimum;
  146. }
  147.  
  148. /**
  149. * Selects the value of the maximum array component.
  150. *
  151. * @param maximum the maximum value allowed.
  152. * @return the random number generator selector.
  153. */
  154. public LongRandomSelector withMaximum(long maximum) {
  155. checkMinMax(minimum, maximum);
  156. return new LongRandomSelector(length, minimum, maximum);
  157. }
  158. }
  159.  
  160. /**
  161. * This inner static class is responsible for choosing the maximum value of
  162. * an array of integers.
  163. */
  164. public static class IntMaximumSelector {
  165.  
  166. /**
  167. * The length of the requested array.
  168. */
  169. private final int length;
  170.  
  171. /**
  172. * The minimum requested value.
  173. */
  174. private final int minimum;
  175.  
  176. private IntMaximumSelector(int length, int minimum) {
  177. this.length = length;
  178. this.minimum = minimum;
  179. }
  180.  
  181. /**
  182. * Selects the value of the maximum array component.
  183. *
  184. * @param maximum the maximum value allowed.
  185. * @return the random number generator selector.
  186. */
  187. public IntRandomSelector withMaximum(int maximum) {
  188. checkMinMax(minimum, maximum);
  189. return new IntRandomSelector(length, minimum, maximum);
  190. }
  191. }
  192.  
  193. /**
  194. * This inner static class is responsible for creating the actual long
  195. * integer arrays.
  196. */
  197. public static class LongRandomSelector {
  198.  
  199. /**
  200. * The length of the requested array.
  201. */
  202. private final int length;
  203.  
  204. /**
  205. * The minimum requested value.
  206. */
  207. private final long minimum;
  208.  
  209. /**
  210. * The maximum requested value.
  211. */
  212. private final long maximum;
  213.  
  214. private LongRandomSelector(int length,
  215. long minimum,
  216. long maximum) {
  217. this.length = length;
  218. this.minimum = minimum;
  219. this.maximum = maximum;
  220. }
  221.  
  222. /**
  223. * Creates the actual array of long integers using a specified random
  224. * number generator.
  225. *
  226. * @param random the random number generator.
  227. * @return the array of long integers.
  228. */
  229. public long[] withRandom(Random random) {
  230. final long[] array = new long[length];
  231.  
  232. for (int i = 0; i < length; ++i) {
  233. array[i] = Math.abs(random.nextLong()) %
  234. (maximum - minimum + 1) + minimum;
  235. }
  236.  
  237. return array;
  238. }
  239.  
  240. /**
  241. * Create the actual array of long integers using a default random
  242. * number generator.
  243. *
  244. * @return the array of long integers.
  245. */
  246. public long[] withDefaultRandom() {
  247. return withRandom(new Random());
  248. }
  249. }
  250.  
  251. /**
  252. * This inner static class is responsible for creating the actual integer
  253. * arrays.
  254. */
  255. public static class IntRandomSelector {
  256.  
  257. /**
  258. * The length of the requested array.
  259. */
  260. private final int length;
  261.  
  262. /**
  263. * The minimum requested value.
  264. */
  265. private final int minimum;
  266.  
  267. /**
  268. * The maximum requested value.
  269. */
  270. private final int maximum;
  271.  
  272. private IntRandomSelector(int length,
  273. int minimum,
  274. int maximum) {
  275. this.length = length;
  276. this.minimum = minimum;
  277. this.maximum = maximum;
  278. }
  279.  
  280. /**
  281. * Creates the actual array of integers using a specified random number
  282. * generator.
  283. *
  284. * @param random the random number generator.
  285. * @return the array of integers.
  286. */
  287. public int[] withRandom(Random random) {
  288. final int[] array = new int[length];
  289.  
  290. for (int i = 0; i < length; ++i) {
  291. array[i] = random.nextInt(maximum - minimum + 1) + minimum;
  292. }
  293.  
  294. return array;
  295. }
  296.  
  297. /**
  298. * Creates the actual array of integers using a default random number
  299. * generator.
  300. *
  301. * @return the array of integers.
  302. */
  303. public int[] withDefaultRandom() {
  304. return withRandom(new Random());
  305. }
  306. }
  307.  
  308. /**
  309. * This class is responsible for initiating a fluent API call.
  310. *
  311. * @return the type selector.
  312. */
  313. public static TypeSelector createArray() {
  314. return new TypeSelector();
  315. }
  316.  
  317. private static void checkLength(int length) {
  318. if (length < 0) {
  319. throw new IllegalArgumentException(
  320. "The requested length is negative (" + length + ").");
  321. }
  322. }
  323.  
  324. private static void checkMinMax(int a, int b) {
  325. if (a > b) {
  326. throw new IllegalArgumentException(
  327. "The requested minimum value " +
  328. "(" + a + ") is greater than the maximum (" + b + ").");
  329. }
  330. }
  331.  
  332. private static void checkMinMax(long a, long b) {
  333. if (a > b) {
  334. throw new IllegalArgumentException(
  335. "The requested minimum value " +
  336. "(" + a + ") is greater than the maximum (" + b + ").");
  337. }
  338. }
  339.  
  340. public static void main(String[] args) {
  341. long[] array1 = createArray().ofLongs()
  342. .ofLength(12)
  343. .withMinimum(-100L)
  344. .withMaximum(200L)
  345. .withDefaultRandom();
  346.  
  347. System.out.println(Arrays.toString(array1));
  348.  
  349. int[] array2 = createArray().ofIntegers()
  350. .ofLength(15)
  351. .withMinimum(-10)
  352. .withMaximum(20)
  353. .withDefaultRandom();
  354.  
  355. System.out.println(Arrays.toString(array2));
  356. }
  357. }
  358.  
  359. long[] array1 = createArray().ofLongs()
  360. .ofLength(12)
  361. .withMinimum(-100L)
  362. .withMaximum(200L)
  363. .withDefaultRandom();
  364.  
  365. long[] array = ThreadLocalRandom.longs(12, -100, 200).toArray();
  366.  
  367. Random rand = new Random();
  368. long[] more = rand.longs(12, -100, 200).toArray();
  369.  
  370. long[] more = rand.longs(-100, 200).limit(12).toArray();
  371.  
  372. int[] ints = rand.ints(-100, 200).limit(12).toArray();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement