View Javadoc
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  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.Field;
24  
25  /**
26   * Representation of the complex numbers field.
27   *
28   * @param <T> the type of the field elements
29   * @see FieldComplex
30   * @since 2.0
31   */
32  public class FieldComplexField<T extends CalculusFieldElement<T>> implements Field<FieldComplex<T>> {
33  
34      /** Cached fields. */
35      private static final Map<Field<?>, FieldComplexField<?>> CACHE = new HashMap<>();
36  
37      /** Constant 0. */
38      private final FieldComplex<T> zero;
39  
40      /** Constant 1. */
41      private final FieldComplex<T> one;
42  
43      /** Simple constructor.
44       * @param field type of the field element
45       */
46      private FieldComplexField(final Field<T> field) {
47          zero = FieldComplex.getZero(field);
48          one  = FieldComplex.getOne(field);
49      }
50  
51      /** Get the field for complex numbers.
52       * @param partsField field for the real and imaginary parts
53       * @param <T> the type of the field elements
54       * @return cached field
55       */
56      public static <T extends CalculusFieldElement<T>> FieldComplexField<T> getField(final Field<T> partsField) {
57          FieldComplexField<?> cachedField;
58          synchronized (CACHE) {
59              cachedField = CACHE.get(partsField);
60              if (cachedField == null) {
61                  cachedField = new FieldComplexField<>(partsField);
62                  CACHE.put(partsField, cachedField);
63              }
64          }
65  
66          @SuppressWarnings("unchecked")
67          final FieldComplexField<T> tCached = (FieldComplexField<T>) cachedField;
68          return tCached;
69  
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public FieldComplex<T> getOne() {
75          return one;
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public FieldComplex<T> getZero() {
81          return zero;
82      }
83  
84      /** {@inheritDoc} */
85      @SuppressWarnings("unchecked")
86      @Override
87      public Class<FieldComplex<T>> getRuntimeClass() {
88          return (Class<FieldComplex<T>>) getZero().getClass();
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public boolean equals(final Object other) {
94          return this == other;
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public int hashCode() {
100         return 0xd368f208;
101     }
102 
103 }