Coordinates.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.sampling.shape;

  18. /**
  19.  * Utility class for common coordinate operations for shape samplers.
  20.  *
  21.  * @since 1.4
  22.  */
  23. final class Coordinates {

  24.     /** No public construction. */
  25.     private Coordinates() {}

  26.     /**
  27.      * Check that the values are finite. This method is primarily for parameter
  28.      * validation in methods and constructors, for example:
  29.      *
  30.      * <pre>
  31.      * public Line(double[] start, double[] end) {
  32.      *     this.start = Coordinates.requireFinite(start, "start");
  33.      *     this.end = Coordinates.requireFinite(end, "end");
  34.      * }
  35.      * </pre>
  36.      *
  37.      * @param values the values
  38.      * @param message the message detail to prepend to the message in the event an exception is thrown
  39.      * @return the values
  40.      * @throws IllegalArgumentException if a non-finite value is found
  41.      */
  42.     static double[] requireFinite(double[] values, String message) {
  43.         for (final double value : values) {
  44.             if (!Double.isFinite(value)) {
  45.                 throw new IllegalArgumentException(message + " contains non-finite value: " + value);
  46.             }
  47.         }
  48.         return values;
  49.     }

  50.     /**
  51.      * Check that the values is the specified length. This method is primarily for
  52.      * parameter validation in methods and constructors, for example:
  53.      *
  54.      * <pre>
  55.      * public Square(double[] topLeft, double[] bottomRight) {
  56.      *     this.topLeft = Coordinates.requireLength(topLeft, 2, "topLeft");
  57.      *     this.bottomRight = Coordinates.requireLength(bottomRight, 2, "bottomRight");
  58.      * }
  59.      * </pre>
  60.      *
  61.      * @param values the values
  62.      * @param length the length
  63.      * @param message the message detail to prepend to the message in the event an
  64.      * exception is thrown
  65.      * @return the values
  66.      * @throws IllegalArgumentException if the array length is not the specified length
  67.      */
  68.     static double[] requireLength(double[] values, int length, String message) {
  69.         if (values.length != length) {
  70.             throw new IllegalArgumentException(String.format("%s length mismatch: %d != %d",
  71.                     message, values.length, length));
  72.         }
  73.         return values;
  74.     }
  75. }