View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.rng.simple;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  
24  /**
25   * The purpose of this class is to provide the list of all generators
26   * implemented in the library.
27   * The list must be updated with each new RNG implementation.
28   *
29   * @see #list()
30   * @see #list32()
31   * @see #list64()
32   */
33  public class ProvidersList {
34      /** List of all RNGs implemented in the library. */
35      private static final List<Data[]> LIST = new ArrayList<Data[]>();
36      /** List of 32-bits based RNGs. */
37      private static final List<Data[]> LIST32 = new ArrayList<Data[]>();
38      /** List of 64-bits based RNGs. */
39      private static final List<Data[]> LIST64 = new ArrayList<Data[]>();
40  
41      static {
42          try {
43              // "int"-based RNGs.
44              add(LIST32, RandomSource.JDK, -122333444455555L);
45              add(LIST32, RandomSource.MT, new int[] { -123, -234, -345 });
46              add(LIST32, RandomSource.WELL_512_A, new int[] { -23, -34, -45 });
47              add(LIST32, RandomSource.WELL_1024_A, new int[] { -1234, -2345, -3456 });
48              add(LIST32, RandomSource.WELL_19937_A, new int[] { -2123, -3234, -4345 });
49              add(LIST32, RandomSource.WELL_19937_C, new int[] { -123, -234, -345, -456 });
50              add(LIST32, RandomSource.WELL_44497_A, new int[] { -12345, -23456, -34567 });
51              add(LIST32, RandomSource.WELL_44497_B, new int[] { 123, 234, 345 });
52              add(LIST32, RandomSource.ISAAC, new int[] { 123, -234, 345, -456 });
53              add(LIST32, RandomSource.MWC_256, new int[] { 12, -1234, -3456, 45678 });
54              add(LIST32, RandomSource.KISS, new int[] { 12, 1234, 23456, 345678 });
55              // ... add more here.
56  
57              // "long"-based RNGs.
58              add(LIST64, RandomSource.SPLIT_MIX_64, -988777666655555L);
59              add(LIST64, RandomSource.XOR_SHIFT_1024_S, new long[] { 123456L, 234567L, -345678L });
60              add(LIST64, RandomSource.TWO_CMRES, 55443322);
61              add(LIST64, RandomSource.TWO_CMRES_SELECT, -987654321, 5, 8);
62              add(LIST64, RandomSource.MT_64, new long[] { 1234567L, 2345678L, -3456789L });
63              // ... add more here.
64  
65              // Do not modify the remaining statements.
66              // Complete list.
67              LIST.addAll(LIST32);
68              LIST.addAll(LIST64);
69          } catch (Exception e) {
70              System.err.println("Unexpected exception while creating the list of generators: " + e);
71              e.printStackTrace(System.err);
72              throw new RuntimeException(e);
73          }
74      }
75  
76      /**
77       * Class contains only static methods.
78       */
79      private ProvidersList() {}
80  
81      /**
82       * Helper to statisfy Junit requirement that each parameter set contains
83       * the same number of objects.
84       */
85      private static void add(List<Data[]> list,
86                              RandomSource source,
87                              Object ... data) {
88          final RandomSource rng = source;
89          final Object seed = data.length > 0 ? data[0] : null;
90          final Object[] args = data.length > 1 ? Arrays.copyOfRange(data, 1, data.length) : null;
91  
92          list.add(new Data[] { new Data(rng, seed, args) });
93      }
94  
95      /**
96       * Subclasses that are "parametric" tests can forward the call to
97       * the "@Parameters"-annotated method to this method.
98       *
99       * @return the list of all generators.
100      */
101     public static Iterable<Data[]> list() {
102         return Collections.unmodifiableList(LIST);
103     }
104 
105     /**
106      * Subclasses that are "parametric" tests can forward the call to
107      * the "@Parameters"-annotated method to this method.
108      *
109      * @return the list of 32-bits based generators.
110      */
111     public static Iterable<Data[]> list32() {
112         return Collections.unmodifiableList(LIST32);
113     }
114 
115     /**
116      * Subclasses that are "parametric" tests can forward the call to
117      * the "@Parameters"-annotated method to this method.
118      *
119      * @return the list of 64-bits based generators.
120      */
121     public static Iterable<Data[]> list64() {
122         return Collections.unmodifiableList(LIST64);
123     }
124 
125     /**
126      * Helper.
127      * Better not to mix Junit assumptions of the usage of "Object[]".
128      */
129     public static class Data {
130         private final RandomSource source;
131         private final Object seed;
132         private final Object[] args;
133 
134         public Data(RandomSource source,
135                     Object seed,
136                     Object[] args) {
137             this.source = source;
138             this.seed = seed;
139             this.args = args;
140         }
141 
142         public RandomSource getSource() {
143             return source;
144         }
145 
146         public Object getSeed() {
147             return seed;
148         }
149 
150         public Object[] getArgs() {
151             return args == null ? null : Arrays.copyOf(args, args.length);
152         }
153 
154         @Override
155         public String toString() {
156             return source.toString() + " seed=" + seed + " args=" + Arrays.toString(args);
157         }
158     }
159 }