001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.geometry.euclidean.oned;
019
020import java.io.Serializable;
021
022import org.apache.commons.math3.exception.MathUnsupportedOperationException;
023import org.apache.commons.math3.exception.util.LocalizedFormats;
024import org.apache.commons.math3.geometry.Space;
025
026/**
027 * This class implements a one-dimensional space.
028 * @since 3.0
029 */
030public class Euclidean1D implements Serializable, Space {
031
032    /** Serializable version identifier. */
033    private static final long serialVersionUID = -1178039568877797126L;
034
035    /** Private constructor for the singleton.
036     */
037    private Euclidean1D() {
038    }
039
040    /** Get the unique instance.
041     * @return the unique instance
042     */
043    public static Euclidean1D getInstance() {
044        return LazyHolder.INSTANCE;
045    }
046
047    /** {@inheritDoc} */
048    public int getDimension() {
049        return 1;
050    }
051
052    /** {@inheritDoc}
053     * <p>
054     * As the 1-dimension Euclidean space does not have proper sub-spaces,
055     * this method always throws a {@link NoSubSpaceException}
056     * </p>
057     * @return nothing
058     * @throws NoSubSpaceException in all cases
059     */
060    public Space getSubSpace() throws NoSubSpaceException {
061        throw new NoSubSpaceException();
062    }
063
064    // CHECKSTYLE: stop HideUtilityClassConstructor
065    /** Holder for the instance.
066     * <p>We use here the Initialization On Demand Holder Idiom.</p>
067     */
068    private static class LazyHolder {
069        /** Cached field instance. */
070        private static final Euclidean1D INSTANCE = new Euclidean1D();
071    }
072    // CHECKSTYLE: resume HideUtilityClassConstructor
073
074    /** Handle deserialization of the singleton.
075     * @return the singleton instance
076     */
077    private Object readResolve() {
078        // return the singleton instance
079        return LazyHolder.INSTANCE;
080    }
081
082    /** Specialized exception for inexistent sub-space.
083     * <p>
084     * This exception is thrown when attempting to get the sub-space of a one-dimensional space
085     * </p>
086     */
087    public static class NoSubSpaceException extends MathUnsupportedOperationException {
088
089        /** Serializable UID. */
090        private static final long serialVersionUID = 20140225L;
091
092        /** Simple constructor.
093         */
094        public NoSubSpaceException() {
095            super(LocalizedFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1);
096        }
097
098    }
099
100}