|
|
@@ -0,0 +1,275 @@
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/???
|
|
|
+ */
|
|
|
+package net.ranides.assira.collection.mockup;
|
|
|
+
|
|
|
+import java.util.AbstractMap;
|
|
|
+import java.util.EnumMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
+import java.util.Random;
|
|
|
+import java.util.TreeMap;
|
|
|
+import net.ranides.assira.collection.HashComparator;
|
|
|
+import net.ranides.assira.collection.lookups.Lookup;
|
|
|
+import static net.ranides.assira.collection.mockup.ValueEnum.*;
|
|
|
+import net.ranides.assira.generic.CompareUtils;
|
|
|
+import static org.junit.Assert.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ */
|
|
|
+public final class IntValueUtils {
|
|
|
+
|
|
|
+ private IntValueUtils() {
|
|
|
+ // utility class
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final ValueGenerator<Integer, ValueEnum> KEYS = new ValueGenerator<Integer, ValueEnum>(TPoint.class) {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HashComparator<Integer> comparator() {
|
|
|
+ return IntValue.MOD;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer object(ValueEnum key) {
|
|
|
+ return $key(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer object(int key) {
|
|
|
+ return key;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ public static final ValueGenerator<Entry<Integer, String>, ValueEnum> ENTRIES = new ValueGenerator<Entry<Integer, String>, ValueEnum>(Entry.class) {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HashComparator<Entry<Integer, String>> comparator() {
|
|
|
+ return ENTRY_SUM;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Entry<Integer, String> object(ValueEnum key) {
|
|
|
+ return $entry(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Entry<Integer, String> object(int key) {
|
|
|
+ return $entry(KEYS.object(key), "s"+key);
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
+ private static final HashComparator<Entry<Integer,String>> ENTRY_SUM = new HashComparator<Entry<Integer, String>>() {
|
|
|
+ @Override
|
|
|
+ public int hashCode(Entry<Integer, String> entry) {
|
|
|
+ return IntValue.MOD.hashCode(entry.getKey());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int compare(Entry<Integer, String> entry1, Entry<Integer, String> entry2) {
|
|
|
+ int d = IntValue.MOD.compare(entry1.getKey(), entry2.getKey());
|
|
|
+ return d != 0 ? d : CompareUtils.cmp(entry1.getValue(), entry2.getValue());
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ private static final EnumMap<ValueEnum, Integer> M_KEYS = new EnumMap<>(ValueEnum.class);
|
|
|
+
|
|
|
+ static {
|
|
|
+ M_KEYS.put(ValueEnum.A1, 20500); // 17
|
|
|
+ M_KEYS.put(ValueEnum.A1_EQ, 30500); // 17
|
|
|
+ M_KEYS.put(ValueEnum.A1_NE, 21500); // 9
|
|
|
+ M_KEYS.put(ValueEnum.B1, 20501); // 24
|
|
|
+ M_KEYS.put(ValueEnum.C1, 20502); // 26
|
|
|
+ M_KEYS.put(ValueEnum.D1, 20510); // 63
|
|
|
+ M_KEYS.put(ValueEnum.D2, 20511); // 64
|
|
|
+ M_KEYS.put(ValueEnum.D3, 20512); // 65
|
|
|
+ M_KEYS.put(ValueEnum.D4, 20513); // 66
|
|
|
+ M_KEYS.put(ValueEnum.E1, 20520); // 81
|
|
|
+ M_KEYS.put(ValueEnum.E1_EQ, 30520); // 81
|
|
|
+ M_KEYS.put(ValueEnum.E1_NE, 31520); // 92
|
|
|
+
|
|
|
+ M_KEYS.put(ValueEnum.SUB_MIN, 0); // 6
|
|
|
+ M_KEYS.put(ValueEnum.SUB_MAX, 32000); // 99
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final EnumMap<ValueEnum, String> M_VALS = new EnumMap<>(ValueEnum.class);
|
|
|
+
|
|
|
+ static {
|
|
|
+ M_VALS.put(ValueEnum.A1, "v17");
|
|
|
+ M_VALS.put(ValueEnum.A1_EQ, "v19");
|
|
|
+ M_VALS.put(ValueEnum.A1_NE, "v20");
|
|
|
+ M_VALS.put(ValueEnum.B1, "v21");
|
|
|
+ M_VALS.put(ValueEnum.C1, "v22");
|
|
|
+ M_VALS.put(ValueEnum.D1, "v31");
|
|
|
+ M_VALS.put(ValueEnum.D2, "v32");
|
|
|
+ M_VALS.put(ValueEnum.D3, "v33");
|
|
|
+ M_VALS.put(ValueEnum.D4, "v34");
|
|
|
+ M_VALS.put(ValueEnum.E1, "v81");
|
|
|
+ M_VALS.put(ValueEnum.E1_EQ, "v81");
|
|
|
+ M_VALS.put(ValueEnum.E1_NE, "v81");
|
|
|
+ M_VALS.put(ValueEnum.SUB_MIN, "v91");
|
|
|
+ M_VALS.put(ValueEnum.SUB_MAX, "v92");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void $put(Map<Integer, String> target, ValueEnum... items) {
|
|
|
+ for(ValueEnum item : items) {
|
|
|
+ target.put($key(item), $value(item));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void $put(Map<Integer, String> target, Integer... items) {
|
|
|
+ for (Integer point : items) {
|
|
|
+ target.put(point, $value(B1));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Entry<Integer, String> $find(Map<Integer, String> target, ValueEnum key) {
|
|
|
+ for(Entry<Integer, String> entry : target.entrySet()) {
|
|
|
+ if($equals(entry.getKey(), key)) {
|
|
|
+ return entry;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fail("Entry not found: " + key);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int[] $shuffle(int[] array) {
|
|
|
+ $shuffle(array, null);
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void $shuffle(int[] array, String[] values) {
|
|
|
+ int n = array.length;
|
|
|
+ Random random = new Random(777);
|
|
|
+ for(int i=0; i<n; i++) {
|
|
|
+ int a = random.nextInt(n);
|
|
|
+ int b = random.nextInt(n);
|
|
|
+ Integer point = array[a];
|
|
|
+ array[a] = array[b];
|
|
|
+ array[b] = point;
|
|
|
+ if(null != values) {
|
|
|
+ String value = values[a];
|
|
|
+ values[a] = values[b];
|
|
|
+ values[b] = value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer $key(ValueEnum type) {
|
|
|
+ return M_KEYS.get(type);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int[] $keys(ValueEnum... values) {
|
|
|
+ int[] array = new int[values.length];
|
|
|
+ for(int i=0; i<values.length; i++) {
|
|
|
+ array[i] = $key(values[i]);
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int[] $keys(int count) {
|
|
|
+ int[] array = new int[count];
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ array[i] = i;
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Zwraca listę 3*count elementów, w której punkty są częściowo równe:
|
|
|
+ * A B1 C, A B1 C, A B1 C, ...
|
|
|
+ * gdzie
|
|
|
+ * A - punkt oryginalny
|
|
|
+ * B1 - punkty równy
|
|
|
+ * C - punkt różny ale ten sam hashcode
|
|
|
+ * @param count
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int[] $ukeys(int count) {
|
|
|
+ int n = 3 * count;
|
|
|
+ int[] array = new int[n];
|
|
|
+ for (int i = 0; i < n; i += 3) {
|
|
|
+ array[i + 0] = i;
|
|
|
+ array[i + 1] = i+10000;
|
|
|
+ array[i + 2] = i+11000;
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String $value(ValueEnum type) {
|
|
|
+ return M_VALS.get(type);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String[] $values(int count) {
|
|
|
+ String[] array = new String[count];
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ array[i] = "s"+(i++);
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String[] $values(ValueEnum... values) {
|
|
|
+ String[] array = new String[values.length];
|
|
|
+ for(int i=0; i<values.length; i++) {
|
|
|
+ array[i] = $value(values[i]);
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Map<Integer, String> $map(int[] keys) {
|
|
|
+ Map<Integer, String> target = new TreeMap<>(IntValue.STD);
|
|
|
+ String[] values = $values(keys.length);
|
|
|
+ for (int i = 0; i < keys.length; i++) {
|
|
|
+ target.put(keys[i], values[i]);
|
|
|
+ }
|
|
|
+ return target;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Map<Integer, String> $map(int[] points, String[] values) {
|
|
|
+ Map<Integer, String> target = new TreeMap<>(IntValue.STD);
|
|
|
+ for (int i = 0; i < points.length; i++) {
|
|
|
+ target.put(points[i], values[i]);
|
|
|
+ }
|
|
|
+ return target;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean $equals(Integer point1, Integer point2) {
|
|
|
+ return IntValue.MOD.equals(point1, point2);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean $equals(Integer point1, ValueEnum point2) {
|
|
|
+ return IntValue.MOD.equals(point1, $key(point2));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int $hashcode(Integer point) {
|
|
|
+ return IntValue.MOD.hashCode(point);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int $hashcode(ValueEnum point) {
|
|
|
+ return IntValue.MOD.hashCode($key(point));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Entry<Integer, String> $entry(Integer key, String value){
|
|
|
+ return new AbstractMap.SimpleEntry<>(key, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Entry<Integer, String> $entry(ValueEnum point){
|
|
|
+ return $entry($key(point), $value(point));
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static Entry<Integer, String>[] $entries(ValueEnum... values) {
|
|
|
+ Entry<Integer, String>[] array = new Entry[values.length];
|
|
|
+ for(int i=0; i<values.length; i++) {
|
|
|
+ array[i] = $entry(values[i]);
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|