You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/10/11 13:28:16 UTC
[commons-rng] 04/16: Option to list only int/long providers
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git
commit 17428278ba06a72207e01fc2bd0a46a33de2dd2c
Author: aherbert <ah...@apache.org>
AuthorDate: Fri Oct 4 11:45:50 2019 +0100
Option to list only int/long providers
---
.../commons/rng/examples/stress/ListCommand.java | 26 ++++++++++-
.../rng/examples/stress/StressTestDataList.java | 50 ++++++++++++++++++++--
2 files changed, 72 insertions(+), 4 deletions(-)
diff --git a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ListCommand.java b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ListCommand.java
index 16fa969..3a640e6 100644
--- a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ListCommand.java
+++ b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ListCommand.java
@@ -51,6 +51,13 @@ class ListCommand implements Callable<Void> {
paramLabel = "<format>")
private ListFormat listFormat = ListFormat.STRESS_TEST;
+ /** The provider type. */
+ @Option(names = {"--provider"},
+ description = {"The provider type (default: ${DEFAULT-VALUE}).",
+ "Valid values: ${COMPLETION-CANDIDATES}."},
+ paramLabel = "<provider>")
+ private ProviderType providerType = ProviderType.ALL;
+
/** The prefix for each ID in the template list of random generators. */
@Option(names = {"-p", "--prefix"},
description = {"The ID prefix.",
@@ -74,12 +81,29 @@ class ListCommand implements Callable<Void> {
}
/**
+ * The type of provider.
+ */
+ enum ProviderType {
+ /** List all providers. */
+ ALL,
+ /** List int providers. */
+ INT,
+ /** List long providers. */
+ LONG,
+ }
+
+ /**
* Prints a template generators list to stdout.
*/
@Override
public Void call() throws Exception {
LogUtils.setLogLevel(reusableOptions.logLevel);
- final StressTestDataList list = new StressTestDataList(idPrefix, trials);
+ StressTestDataList list = new StressTestDataList(idPrefix, trials);
+ if (providerType == ProviderType.INT) {
+ list = list.subsetIntSource();
+ } else if (providerType == ProviderType.LONG) {
+ list = list.subsetLongSource();
+ }
// Write in one call to the output
final StringBuilder sb = new StringBuilder();
switch (listFormat) {
diff --git a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestDataList.java b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestDataList.java
index 6d6c72b..98ad8d1 100644
--- a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestDataList.java
+++ b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestDataList.java
@@ -16,7 +16,10 @@
*/
package org.apache.commons.rng.examples.stress;
+import org.apache.commons.rng.core.source32.RandomIntSource;
+import org.apache.commons.rng.core.source64.RandomLongSource;
import org.apache.commons.rng.simple.RandomSource;
+import org.apache.commons.rng.simple.internal.ProviderBuilder.RandomSourceInternal;
import java.util.ArrayList;
import java.util.EnumMap;
@@ -45,10 +48,10 @@ class StressTestDataList implements Iterable<StressTestData> {
private final List<StressTestData> list = new ArrayList<>();
/**
- * Creates the list with the number of trials set to 1.
+ * Creates an empty list.
*/
- StressTestDataList() {
- this("", 1);
+ private StressTestDataList() {
+ // Do nothing
}
/**
@@ -75,4 +78,45 @@ class StressTestDataList implements Iterable<StressTestData> {
public Iterator<StressTestData> iterator() {
return list.iterator();
}
+
+ /**
+ * Create a subset of the list containing only instances of {@link RandomIntSource}.
+ *
+ * @return the stress test data list
+ */
+ public StressTestDataList subsetIntSource() {
+ return subsetOf(RandomIntSource.class);
+ }
+
+ /**
+ * Create a subset of the list containing only instances of {@link RandomLongSource}.
+ *
+ * @return the stress test data list
+ */
+ public StressTestDataList subsetLongSource() {
+ return subsetOf(RandomLongSource.class);
+ }
+
+ /**
+ * Create a subset of the list containing only instances of the specified type.
+ *
+ * @param type The instance type.
+ * @return the stress test data list
+ */
+ private StressTestDataList subsetOf(Class<?> type) {
+ final StressTestDataList subset = new StressTestDataList();
+ for (StressTestData data : list) {
+ // This makes a big assumption that the two enums have the same name
+ RandomSourceInternal source;
+ try {
+ source = RandomSourceInternal.valueOf(data.getRandomSource().name());
+ } catch (IllegalArgumentException ex) {
+ throw new ApplicationException("Unknown internal source: " + data.getRandomSource(), ex);
+ }
+ if (type.isAssignableFrom(source.getRng())) {
+ subset.list.add(data);
+ }
+ }
+ return subset;
+ }
}