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 * @version $Id: Euclidean1D.java 1571737 2014-02-25 16:42:07Z luc $
029 * @since 3.0
030 */
031public class Euclidean1D implements Serializable, Space {
032
033    /** Serializable version identifier. */
034    private static final long serialVersionUID = -1178039568877797126L;
035
036    /** Private constructor for the singleton.
037     */
038    private Euclidean1D() {
039    }
040
041    /** Get the unique instance.
042     * @return the unique instance
043     */
044    public static Euclidean1D getInstance() {
045        return LazyHolder.INSTANCE;
046    }
047
048    /** {@inheritDoc} */
049    public int getDimension() {
050        return 1;
051    }
052
053    /** {@inheritDoc}
054     * <p>
055     * As the 1-dimension Euclidean space does not have proper sub-spaces,
056     * this method always throws a {@link NoSubSpaceException}
057     * </p>
058     * @return nothing
059     * @throws NoSubSpaceException in all cases
060     */
061    public Space getSubSpace() throws NoSubSpaceException {
062        throw new NoSubSpaceException();
063    }
064
065    // CHECKSTYLE: stop HideUtilityClassConstructor
066    /** Holder for the instance.
067     * <p>We use here the Initialization On Demand Holder Idiom.</p>
068     */
069    private static class LazyHolder {
070        /** Cached field instance. */
071        private static final Euclidean1D INSTANCE = new Euclidean1D();
072    }
073    // CHECKSTYLE: resume HideUtilityClassConstructor
074
075    /** Handle deserialization of the singleton.
076     * @return the singleton instance
077     */
078    private Object readResolve() {
079        // return the singleton instance
080        return LazyHolder.INSTANCE;
081    }
082
083    /** Specialized exception for inexistent sub-space.
084     * <p>
085     * This exception is thrown when attempting to get the sub-space of a one-dimensional space
086     * </p>
087     */
088    public static class NoSubSpaceException extends MathUnsupportedOperationException {
089
090        /** Serializable UID. */
091        private static final long serialVersionUID = 20140225L;
092
093        /** Simple constructor.
094         */
095        public NoSubSpaceException() {
096            super(LocalizedFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1);
097        }
098
099    }
100
101}