FieldComplexField.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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. package org.hipparchus.complex;

  18. import java.util.HashMap;
  19. import java.util.Map;

  20. import org.hipparchus.CalculusFieldElement;
  21. import org.hipparchus.Field;

  22. /**
  23.  * Representation of the complex numbers field.
  24.  *
  25.  * @param <T> the type of the field elements
  26.  * @see FieldComplex
  27.  * @since 2.0
  28.  */
  29. public class FieldComplexField<T extends CalculusFieldElement<T>> implements Field<FieldComplex<T>> {

  30.     /** Cached fields. */
  31.     private static final Map<Field<?>, FieldComplexField<?>> CACHE = new HashMap<>();

  32.     /** Constant 0. */
  33.     private final FieldComplex<T> zero;

  34.     /** Constant 1. */
  35.     private final FieldComplex<T> one;

  36.     /** Simple constructor.
  37.      * @param field type of the field element
  38.      */
  39.     private FieldComplexField(final Field<T> field) {
  40.         zero = FieldComplex.getZero(field);
  41.         one  = FieldComplex.getOne(field);
  42.     }

  43.     /** Get the field for complex numbers.
  44.      * @param partsField field for the real and imaginary parts
  45.      * @param <T> the type of the field elements
  46.      * @return cached field
  47.      */
  48.     public static <T extends CalculusFieldElement<T>> FieldComplexField<T> getField(final Field<T> partsField) {
  49.         FieldComplexField<?> cachedField;
  50.         synchronized (CACHE) {
  51.             cachedField = CACHE.get(partsField);
  52.             if (cachedField == null) {
  53.                 cachedField = new FieldComplexField<>(partsField);
  54.                 CACHE.put(partsField, cachedField);
  55.             }
  56.         }

  57.         @SuppressWarnings("unchecked")
  58.         final FieldComplexField<T> tCached = (FieldComplexField<T>) cachedField;
  59.         return tCached;

  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public FieldComplex<T> getOne() {
  64.         return one;
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     public FieldComplex<T> getZero() {
  69.         return zero;
  70.     }

  71.     /** {@inheritDoc} */
  72.     @SuppressWarnings("unchecked")
  73.     @Override
  74.     public Class<FieldComplex<T>> getRuntimeClass() {
  75.         return (Class<FieldComplex<T>>) getZero().getClass();
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     public boolean equals(final Object other) {
  80.         return this == other;
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public int hashCode() {
  85.         return 0xd368f208;
  86.     }

  87. }