ComplexField.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.  *      http://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. package org.apache.commons.math3.complex;

  18. import java.io.Serializable;

  19. import org.apache.commons.math3.Field;
  20. import org.apache.commons.math3.FieldElement;

  21. /**
  22.  * Representation of the complex numbers field.
  23.  * <p>
  24.  * This class is a singleton.
  25.  * </p>
  26.  * @see Complex
  27.  * @since 2.0
  28.  */
  29. public class ComplexField implements Field<Complex>, Serializable  {

  30.     /** Serializable version identifier. */
  31.     private static final long serialVersionUID = -6130362688700788798L;

  32.     /** Private constructor for the singleton.
  33.      */
  34.     private ComplexField() {
  35.     }

  36.     /** Get the unique instance.
  37.      * @return the unique instance
  38.      */
  39.     public static ComplexField getInstance() {
  40.         return LazyHolder.INSTANCE;
  41.     }

  42.     /** {@inheritDoc} */
  43.     public Complex getOne() {
  44.         return Complex.ONE;
  45.     }

  46.     /** {@inheritDoc} */
  47.     public Complex getZero() {
  48.         return Complex.ZERO;
  49.     }

  50.     /** {@inheritDoc} */
  51.     public Class<? extends FieldElement<Complex>> getRuntimeClass() {
  52.         return Complex.class;
  53.     }

  54.     // CHECKSTYLE: stop HideUtilityClassConstructor
  55.     /** Holder for the instance.
  56.      * <p>We use here the Initialization On Demand Holder Idiom.</p>
  57.      */
  58.     private static class LazyHolder {
  59.         /** Cached field instance. */
  60.         private static final ComplexField INSTANCE = new ComplexField();
  61.     }
  62.     // CHECKSTYLE: resume HideUtilityClassConstructor

  63.     /** Handle deserialization of the singleton.
  64.      * @return the singleton instance
  65.      */
  66.     private Object readResolve() {
  67.         // return the singleton instance
  68.         return LazyHolder.INSTANCE;
  69.     }

  70. }