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.math4.util;
019
020import java.io.Serializable;
021
022import org.apache.commons.math4.Field;
023import org.apache.commons.math4.FieldElement;
024
025/**
026 * Representation of real numbers with arbitrary precision field.
027 * <p>
028 * This class is a singleton.
029 * </p>
030 * @see BigReal
031 * @since 2.0
032 */
033public class BigRealField implements Field<BigReal>, Serializable  {
034
035    /** Serializable version identifier */
036    private static final long serialVersionUID = 4756431066541037559L;
037
038    /** Private constructor for the singleton.
039     */
040    private BigRealField() {
041    }
042
043    /** Get the unique instance.
044     * @return the unique instance
045     */
046    public static BigRealField getInstance() {
047        return LazyHolder.INSTANCE;
048    }
049
050    /** {@inheritDoc} */
051    @Override
052    public BigReal getOne() {
053        return BigReal.ONE;
054    }
055
056    /** {@inheritDoc} */
057    @Override
058    public BigReal getZero() {
059        return BigReal.ZERO;
060    }
061
062    /** {@inheritDoc} */
063    @Override
064    public Class<? extends FieldElement<BigReal>> getRuntimeClass() {
065        return BigReal.class;
066    }
067
068    // CHECKSTYLE: stop HideUtilityClassConstructor
069    /** Holder for the instance.
070     * <p>We use here the Initialization On Demand Holder Idiom.</p>
071     */
072    private static class LazyHolder {
073        /** Cached field instance. */
074        private static final BigRealField INSTANCE = new BigRealField();
075    }
076    // CHECKSTYLE: resume HideUtilityClassConstructor
077
078    /** Handle deserialization of the singleton.
079     * @return the singleton instance
080     */
081    private Object readResolve() {
082        // return the singleton instance
083        return LazyHolder.INSTANCE;
084    }
085
086}