Sphere1D.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.  *      https://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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.geometry.spherical.oned;

  22. import java.io.Serializable;

  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.exception.MathRuntimeException;
  25. import org.hipparchus.geometry.LocalizedGeometryFormats;
  26. import org.hipparchus.geometry.Space;
  27. import org.hipparchus.util.FastMath;

  28. /**
  29.  * This class implements a one-dimensional sphere (i.e. a circle).
  30.  * <p>
  31.  * We use here the topologists definition of the 1-sphere (see
  32.  * <a href="http://mathworld.wolfram.com/Sphere.html">Sphere</a> on
  33.  * MathWorld), i.e. the 1-sphere is the one-dimensional closed curve
  34.  * defined in 2D as x<sup>2</sup>+y<sup>2</sup>=1.
  35.  * </p>
  36.  */
  37. public class Sphere1D implements Serializable, Space {

  38.     /** Smallest tolerance that can be managed.
  39.      * <p>
  40.      * Tolerances smaller than this value will generate exceptions.
  41.      * </p>
  42.      * @since 1.4
  43.      */
  44.     public static final double SMALLEST_TOLERANCE = FastMath.ulp(2 * FastMath.PI);

  45.     /** Serializable version identifier. */
  46.     private static final long serialVersionUID = 20131218L;

  47.     /** Private constructor for the singleton.
  48.      */
  49.     private Sphere1D() {
  50.     }

  51.     /** Get the unique instance.
  52.      * @return the unique instance
  53.      */
  54.     public static Sphere1D getInstance() {
  55.         return LazyHolder.INSTANCE;
  56.     }

  57.     /** Check tolerance against {@link #SMALLEST_TOLERANCE}.
  58.      * @param tolerance tolerance to check
  59.      * @exception MathIllegalArgumentException if tolerance is smaller
  60.      * than {@link #SMALLEST_TOLERANCE}
  61.      */
  62.     public static void checkTolerance(final double tolerance)
  63.         throws MathIllegalArgumentException {
  64.         if (tolerance < SMALLEST_TOLERANCE) {
  65.             throw new MathIllegalArgumentException(LocalizedGeometryFormats.TOO_SMALL_TOLERANCE,
  66.                                                    tolerance, "Sphere1D.SMALLEST_TOLERANCE", SMALLEST_TOLERANCE);
  67.         }
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public int getDimension() {
  72.         return 1;
  73.     }

  74.     /** {@inheritDoc}
  75.      * <p>
  76.      * As the 1-dimension sphere does not have proper sub-spaces,
  77.      * this method always throws a {@link NoSubSpaceException}
  78.      * </p>
  79.      * @return nothing
  80.      * @throws NoSubSpaceException in all cases
  81.      */
  82.     @Override
  83.     public Space getSubSpace() throws NoSubSpaceException {
  84.         throw new NoSubSpaceException();
  85.     }

  86.     // CHECKSTYLE: stop HideUtilityClassConstructor
  87.     /** Holder for the instance.
  88.      * <p>We use here the Initialization On Demand Holder Idiom.</p>
  89.      */
  90.     private static class LazyHolder {
  91.         /** Cached field instance. */
  92.         private static final Sphere1D INSTANCE = new Sphere1D();
  93.     }
  94.     // CHECKSTYLE: resume HideUtilityClassConstructor

  95.     /** Handle deserialization of the singleton.
  96.      * @return the singleton instance
  97.      */
  98.     private Object readResolve() {
  99.         // return the singleton instance
  100.         return LazyHolder.INSTANCE;
  101.     }

  102.     /** Specialized exception for inexistent sub-space.
  103.      * <p>
  104.      * This exception is thrown when attempting to get the sub-space of a one-dimensional space
  105.      * </p>
  106.      */
  107.     public static class NoSubSpaceException extends MathRuntimeException {

  108.         /** Serializable UID. */
  109.         private static final long serialVersionUID = 20140225L;

  110.         /** Simple constructor.
  111.          */
  112.         public NoSubSpaceException() {
  113.             super(LocalizedGeometryFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1);
  114.         }

  115.     }

  116. }