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  
18  package org.apache.commons.rng.sampling.shape;
19  
20  /**
21   * Utility class for common coordinate operations for shape samplers.
22   *
23   * @since 1.4
24   */
25  final class Coordinates {
26  
27      /** No public construction. */
28      private Coordinates() {}
29  
30      /**
31       * Check that the values are finite. This method is primarily for parameter
32       * validation in methods and constructors, for example:
33       *
34       * <pre>
35       * public Line(double[] start, double[] end) {
36       *     this.start = Coordinates.requireFinite(start, "start");
37       *     this.end = Coordinates.requireFinite(end, "end");
38       * }
39       * </pre>
40       *
41       * @param values the values
42       * @param message the message detail to prepend to the message in the event an exception is thrown
43       * @return the values
44       * @throws IllegalArgumentException if a non-finite value is found
45       */
46      static double[] requireFinite(double[] values, String message) {
47          for (final double value : values) {
48              if (!Double.isFinite(value)) {
49                  throw new IllegalArgumentException(message + " contains non-finite value: " + value);
50              }
51          }
52          return values;
53      }
54  
55      /**
56       * Check that the values is the specified length. This method is primarily for
57       * parameter validation in methods and constructors, for example:
58       *
59       * <pre>
60       * public Square(double[] topLeft, double[] bottomRight) {
61       *     this.topLeft = Coordinates.requireLength(topLeft, 2, "topLeft");
62       *     this.bottomRight = Coordinates.requireLength(bottomRight, 2, "bottomRight");
63       * }
64       * </pre>
65       *
66       * @param values the values
67       * @param length the length
68       * @param message the message detail to prepend to the message in the event an
69       * exception is thrown
70       * @return the values
71       * @throws IllegalArgumentException if the array length is not the specified length
72       */
73      static double[] requireLength(double[] values, int length, String message) {
74          if (values.length != length) {
75              throw new IllegalArgumentException(String.format("%s length mismatch: %d != %d",
76                      message, values.length, length));
77          }
78          return values;
79      }
80  }