NativeSeedType.java

  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.internal;

  18. import org.apache.commons.rng.core.util.NumberFactory;

  19. /**
  20.  * The native seed type. Contains values for all native seed types and methods
  21.  * to convert supported seed types to the native seed type.
  22.  *
  23.  * <p>Valid native seed types are:</p>
  24.  * <ul>
  25.  *  <li>{@code Integer}</li>
  26.  *  <li>{@code Long}</li>
  27.  *  <li>{@code int[]}</li>
  28.  *  <li>{@code long[]}</li>
  29.  * </ul>
  30.  *
  31.  * <p>Valid types for seed conversion are:</p>
  32.  * <ul>
  33.  *  <li>{@code Integer} (or {@code int})</li>
  34.  *  <li>{@code Long} (or {@code long})</li>
  35.  *  <li>{@code int[]}</li>
  36.  *  <li>{@code long[]}</li>
  37.  *  <li>{@code byte[]}</li>
  38.  * </ul>
  39.  *
  40.  * @since 1.3
  41.  */
  42. public enum NativeSeedType {
  43.     /** The seed type is {@code Integer}. */
  44.     INT(Integer.class, 4) {
  45.         @Override
  46.         public Integer createSeed(int size, int from, int to) {
  47.             return SeedFactory.createInt();
  48.         }
  49.         @Override
  50.         protected Integer convert(Integer seed, int size) {
  51.             return seed;
  52.         }
  53.         @Override
  54.         protected Integer convert(Long seed, int size) {
  55.             return Conversions.long2Int(seed);
  56.         }
  57.         @Override
  58.         protected Integer convert(int[] seed, int size) {
  59.             return Conversions.intArray2Int(seed);
  60.         }
  61.         @Override
  62.         protected Integer convert(long[] seed, int size) {
  63.             return Conversions.longArray2Int(seed);
  64.         }
  65.         @Override
  66.         protected Integer convert(byte[] seed, int size) {
  67.             return Conversions.byteArray2Int(seed);
  68.         }
  69.     },
  70.     /** The seed type is {@code Long}. */
  71.     LONG(Long.class, 8) {
  72.         @Override
  73.         public Long createSeed(int size, int from, int to) {
  74.             return SeedFactory.createLong();
  75.         }
  76.         @Override
  77.         protected Long convert(Integer seed, int size) {
  78.             return Conversions.int2Long(seed);
  79.         }
  80.         @Override
  81.         protected Long convert(Long seed, int size) {
  82.             return seed;
  83.         }
  84.         @Override
  85.         protected Long convert(int[] seed, int size) {
  86.             return Conversions.intArray2Long(seed);
  87.         }
  88.         @Override
  89.         protected Long convert(long[] seed, int size) {
  90.             return Conversions.longArray2Long(seed);
  91.         }
  92.         @Override
  93.         protected Long convert(byte[] seed, int size) {
  94.             return Conversions.byteArray2Long(seed);
  95.         }
  96.     },
  97.     /** The seed type is {@code int[]}. */
  98.     INT_ARRAY(int[].class, 4) {
  99.         @Override
  100.         public int[] createSeed(int size, int from, int to) {
  101.             // Limit the number of calls to the synchronized method. The generator
  102.             // will support self-seeding.
  103.             return SeedFactory.createIntArray(Math.min(size, RANDOM_SEED_ARRAY_SIZE),
  104.                                               from, to);
  105.         }
  106.         @Override
  107.         protected int[] convert(Integer seed, int size) {
  108.             return Conversions.int2IntArray(seed, size);
  109.         }
  110.         @Override
  111.         protected int[] convert(Long seed, int size) {
  112.             return Conversions.long2IntArray(seed, size);
  113.         }
  114.         @Override
  115.         protected int[] convert(int[] seed, int size) {
  116.             return seed;
  117.         }
  118.         @Override
  119.         protected int[] convert(long[] seed, int size) {
  120.             // Avoid zero filling seeds that are too short
  121.             return Conversions.longArray2IntArray(seed,
  122.                 Math.min(size, Conversions.intSizeFromLongSize(seed.length)));
  123.         }
  124.         @Override
  125.         protected int[] convert(byte[] seed, int size) {
  126.             // Avoid zero filling seeds that are too short
  127.             return Conversions.byteArray2IntArray(seed,
  128.                 Math.min(size, Conversions.intSizeFromByteSize(seed.length)));
  129.         }
  130.     },
  131.     /** The seed type is {@code long[]}. */
  132.     LONG_ARRAY(long[].class, 8) {
  133.         @Override
  134.         public long[] createSeed(int size, int from, int to) {
  135.             // Limit the number of calls to the synchronized method. The generator
  136.             // will support self-seeding.
  137.             return SeedFactory.createLongArray(Math.min(size, RANDOM_SEED_ARRAY_SIZE),
  138.                                                from, to);
  139.         }
  140.         @Override
  141.         protected long[] convert(Integer seed, int size) {
  142.             return Conversions.int2LongArray(seed, size);
  143.         }
  144.         @Override
  145.         protected long[] convert(Long seed, int size) {
  146.             return Conversions.long2LongArray(seed, size);
  147.         }
  148.         @Override
  149.         protected long[] convert(int[] seed, int size) {
  150.             // Avoid zero filling seeds that are too short
  151.             return Conversions.intArray2LongArray(seed,
  152.                 Math.min(size, Conversions.longSizeFromIntSize(seed.length)));
  153.         }
  154.         @Override
  155.         protected long[] convert(long[] seed, int size) {
  156.             return seed;
  157.         }
  158.         @Override
  159.         protected long[] convert(byte[] seed, int size) {
  160.             // Avoid zero filling seeds that are too short
  161.             return Conversions.byteArray2LongArray(seed,
  162.                 Math.min(size, Conversions.longSizeFromByteSize(seed.length)));
  163.         }
  164.     };

  165.     /** Error message for unrecognized seed types. */
  166.     private static final String UNRECOGNISED_SEED = "Unrecognized seed type: ";
  167.     /** Maximum length of the seed array (for creating array seeds). */
  168.     private static final int RANDOM_SEED_ARRAY_SIZE = 128;

  169.     /** Define the class type of the native seed. */
  170.     private final Class<?> type;

  171.     /**
  172.      * Define the number of bytes required to represent the native seed. If the type is
  173.      * an array then this represents the size of a single value of the type.
  174.      */
  175.     private final int bytes;

  176.     /**
  177.      * Instantiates a new native seed type.
  178.      *
  179.      * @param type Define the class type of the native seed.
  180.      * @param bytes Define the number of bytes required to represent the native seed.
  181.      */
  182.     NativeSeedType(Class<?> type, int bytes) {
  183.         this.type = type;
  184.         this.bytes = bytes;
  185.     }

  186.     /**
  187.      * Gets the class type of the native seed.
  188.      *
  189.      * @return the type
  190.      */
  191.     public Class<?> getType() {
  192.         return type;
  193.     }

  194.     /**
  195.      * Gets the number of bytes required to represent the native seed type. If the type is
  196.      * an array then this represents the size of a single value of the type.
  197.      *
  198.      * @return the number of bytes
  199.      */
  200.     public int getBytes() {
  201.         return bytes;
  202.     }

  203.     /**
  204.      * Creates the seed. The output seed type is determined by the native seed type. If the
  205.      * output is an array the required size of the array can be specified.
  206.      *
  207.      * @param size The size of the seed (array types only).
  208.      * @return the seed
  209.      */
  210.     public Object createSeed(int size) {
  211.         // Maintain behaviour since 1.3 to ensure position [0] of array seeds is non-zero.
  212.         return createSeed(size, 0, Math.min(size, 1));
  213.     }

  214.     /**
  215.      * Creates the seed. The output seed type is determined by the native seed type. If
  216.      * the output is an array the required size of the array can be specified and a
  217.      * sub-range that must not be all-zero.
  218.      *
  219.      * @param size The size of the seed (array types only).
  220.      * @param from The start of the not all-zero sub-range (inclusive; array types only).
  221.      * @param to The end of the not all-zero sub-range (exclusive; array types only).
  222.      * @return the seed
  223.      * @throws IndexOutOfBoundsException if the sub-range is out of bounds
  224.      * @since 1.5
  225.      */
  226.     public abstract Object createSeed(int size, int from, int to);

  227.     /**
  228.      * Converts the input seed from any of the supported seed types to the native seed type.
  229.      * If the output is an array the required size of the array can be specified.
  230.      *
  231.      * @param seed Input seed.
  232.      * @param size The size of the output seed (array types only).
  233.      * @return the native seed.
  234.      * @throws UnsupportedOperationException if the {@code seed} type is invalid.
  235.      */
  236.     public Object convertSeed(Object seed,
  237.                               int size) {
  238.         // Convert to native type.
  239.         // Each method must be overridden by specific implementations.

  240.         if (seed instanceof Integer) {
  241.             return convert((Integer) seed, size);
  242.         } else if (seed instanceof Long) {
  243.             return convert((Long) seed, size);
  244.         } else if (seed instanceof int[]) {
  245.             return convert((int[]) seed, size);
  246.         } else if (seed instanceof long[]) {
  247.             return convert((long[]) seed, size);
  248.         } else if (seed instanceof byte[]) {
  249.             return convert((byte[]) seed, size);
  250.         }

  251.         throw new UnsupportedOperationException(unrecognizedSeedMessage(seed));
  252.     }

  253.     /**
  254.      * Convert the input {@code Integer} seed to the native seed type.
  255.      *
  256.      * @param seed Input seed.
  257.      * @param size The size of the output seed (array types only).
  258.      * @return the native seed.
  259.      */
  260.     protected abstract Object convert(Integer seed, int size);

  261.     /**
  262.      * Convert the input {@code Long} seed to the native seed type.
  263.      *
  264.      * @param seed Input seed.
  265.      * @param size The size of the output seed (array types only).
  266.      * @return the native seed.
  267.      */
  268.     protected abstract Object convert(Long seed, int size);

  269.     /**
  270.      * Convert the input {@code int[]} seed to the native seed type.
  271.      *
  272.      * @param seed Input seed.
  273.      * @param size The size of the output seed (array types only).
  274.      * @return the native seed.
  275.      */
  276.     protected abstract Object convert(int[] seed, int size);

  277.     /**
  278.      * Convert the input {@code long[]} seed to the native seed type.
  279.      *
  280.      * @param seed Input seed.
  281.      * @param size The size of the output seed (array types only).
  282.      * @return the native seed.
  283.      */
  284.     protected abstract Object convert(long[] seed, int size);

  285.     /**
  286.      * Convert the input {@code byte[]} seed to the native seed type.
  287.      *
  288.      * @param seed Input seed.
  289.      * @param size The size of the output seed (array types only).
  290.      * @return the native seed.
  291.      */
  292.     protected abstract Object convert(byte[] seed, int size);

  293.     /**
  294.      * Converts the input seed from any of the supported seed types to bytes.
  295.      *
  296.      * @param seed Input seed.
  297.      * @return the seed bytes.
  298.      * @throws UnsupportedOperationException if the {@code seed} type is invalid.
  299.      */
  300.     public static byte[] convertSeedToBytes(Object seed) {
  301.         if (seed instanceof Integer) {
  302.             return NumberFactory.makeByteArray((Integer) seed);
  303.         } else if (seed instanceof Long) {
  304.             return NumberFactory.makeByteArray((Long) seed);
  305.         } else if (seed instanceof int[]) {
  306.             return NumberFactory.makeByteArray((int[]) seed);
  307.         } else if (seed instanceof long[]) {
  308.             return NumberFactory.makeByteArray((long[]) seed);
  309.         } else if (seed instanceof byte[]) {
  310.             return (byte[]) seed;
  311.         }

  312.         throw new UnsupportedOperationException(unrecognizedSeedMessage(seed));
  313.     }

  314.     /**
  315.      * Create an unrecognized seed message. This will add the class type of the seed.
  316.      *
  317.      * @param seed the seed
  318.      * @return the message
  319.      */
  320.     private static String unrecognizedSeedMessage(Object seed) {
  321.         return UNRECOGNISED_SEED + ((seed == null) ? "null" : seed.getClass().getName());
  322.     }
  323. }