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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22
23 package org.hipparchus.geometry.spherical.oned;
24
25 import java.io.Serializable;
26
27 import org.hipparchus.exception.MathIllegalArgumentException;
28 import org.hipparchus.exception.MathRuntimeException;
29 import org.hipparchus.geometry.LocalizedGeometryFormats;
30 import org.hipparchus.geometry.Space;
31 import org.hipparchus.util.FastMath;
32
33 /**
34 * This class implements a one-dimensional sphere (i.e. a circle).
35 * <p>
36 * We use here the topologists definition of the 1-sphere (see
37 * <a href="http://mathworld.wolfram.com/Sphere.html">Sphere</a> on
38 * MathWorld), i.e. the 1-sphere is the one-dimensional closed curve
39 * defined in 2D as x<sup>2</sup>+y<sup>2</sup>=1.
40 * </p>
41 */
42 public class Sphere1D implements Serializable, Space {
43
44 /** Smallest tolerance that can be managed.
45 * <p>
46 * Tolerances smaller than this value will generate exceptions.
47 * </p>
48 * @since 1.4
49 */
50 public static final double SMALLEST_TOLERANCE = FastMath.ulp(2 * FastMath.PI);
51
52 /** Serializable version identifier. */
53 private static final long serialVersionUID = 20131218L;
54
55 /** Private constructor for the singleton.
56 */
57 private Sphere1D() {
58 }
59
60 /** Get the unique instance.
61 * @return the unique instance
62 */
63 public static Sphere1D getInstance() {
64 return LazyHolder.INSTANCE;
65 }
66
67 /** Check tolerance against {@link #SMALLEST_TOLERANCE}.
68 * @param tolerance tolerance to check
69 * @exception MathIllegalArgumentException if tolerance is smaller
70 * than {@link #SMALLEST_TOLERANCE}
71 */
72 public static void checkTolerance(final double tolerance)
73 throws MathIllegalArgumentException {
74 if (tolerance < Sphere1D.SMALLEST_TOLERANCE) {
75 throw new MathIllegalArgumentException(LocalizedGeometryFormats.TOO_SMALL_TOLERANCE,
76 tolerance, "Sphere1D.SMALLEST_TOLERANCE", SMALLEST_TOLERANCE);
77 }
78 }
79
80 /** {@inheritDoc} */
81 @Override
82 public int getDimension() {
83 return 1;
84 }
85
86 /** {@inheritDoc}
87 * <p>
88 * As the 1-dimension sphere does not have proper sub-spaces,
89 * this method always throws a {@link NoSubSpaceException}
90 * </p>
91 * @return nothing
92 * @throws NoSubSpaceException in all cases
93 */
94 @Override
95 public Space getSubSpace() throws NoSubSpaceException {
96 throw new NoSubSpaceException();
97 }
98
99 // CHECKSTYLE: stop HideUtilityClassConstructor
100 /** Holder for the instance.
101 * <p>We use here the Initialization On Demand Holder Idiom.</p>
102 */
103 private static class LazyHolder {
104 /** Cached field instance. */
105 private static final Sphere1D INSTANCE = new Sphere1D();
106 }
107 // CHECKSTYLE: resume HideUtilityClassConstructor
108
109 /** Handle deserialization of the singleton.
110 * @return the singleton instance
111 */
112 private Object readResolve() {
113 // return the singleton instance
114 return LazyHolder.INSTANCE;
115 }
116
117 /** Specialized exception for inexistent sub-space.
118 * <p>
119 * This exception is thrown when attempting to get the sub-space of a one-dimensional space
120 * </p>
121 */
122 public static class NoSubSpaceException extends MathRuntimeException {
123
124 /** Serializable UID. */
125 private static final long serialVersionUID = 20140225L;
126
127 /** Simple constructor.
128 */
129 public NoSubSpaceException() {
130 super(LocalizedGeometryFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1);
131 }
132
133 }
134
135 }