UnivariateDerivative2Field.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.analysis.differentiation;

  18. import java.io.Serializable;

  19. import org.hipparchus.Field;

  20. /** Field for {@link UnivariateDerivative2} instances.
  21.  * <p>
  22.  * This class is a singleton.
  23.  * </p>
  24.  * @since 1.7
  25.  */
  26. public class UnivariateDerivative2Field implements Field<UnivariateDerivative2>, Serializable {

  27.     /** Serializable version identifier. */
  28.     private static final long serialVersionUID = 20200520L;

  29.     /** Zero constant. */
  30.     private final UnivariateDerivative2 zero;

  31.     /** One constant. */
  32.     private final UnivariateDerivative2 one;

  33.     /** Associated factory for conversions to {@link DerivativeStructure}. */
  34.     private final DSFactory factory;

  35.     /** Private constructor for the singleton.
  36.      */
  37.     private UnivariateDerivative2Field() {
  38.         zero    = new UnivariateDerivative2(0.0, 0.0, 0.0);
  39.         one     = new UnivariateDerivative2(1.0, 0.0, 0.0);
  40.         factory = new DSFactory(1, 2);
  41.     }

  42.     /** Get the unique instance.
  43.      * @return the unique instance
  44.      */
  45.     public static UnivariateDerivative2Field getInstance() {
  46.         return LazyHolder.INSTANCE;
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public UnivariateDerivative2 getOne() {
  51.         return one;
  52.     }

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     public UnivariateDerivative2 getZero() {
  56.         return zero;
  57.     }

  58.     /** Get the factory for converting to {@link DerivativeStructure}.
  59.      * <p>
  60.      * This factory is used only for conversions. {@code UnivariateDerivative2} by
  61.      * itself does not rely at all on {@link DSFactory}, {@link DSCompiler}
  62.      * or {@link DerivativeStructure} for its computation. For this reason,
  63.      * the factory here is hidden and this method is package private, so
  64.      * only {@link UnivariateDerivative2#toDerivativeStructure()} can call it on an
  65.      * existing {@link UnivariateDerivative2} instance
  66.      * </p>
  67.      * @return factory for conversions
  68.      */
  69.     DSFactory getConversionFactory() {
  70.         return factory;
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     public Class<UnivariateDerivative2> getRuntimeClass() {
  75.         return UnivariateDerivative2.class;
  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 0x71f43303;
  86.     }

  87.     // CHECKSTYLE: stop HideUtilityClassConstructor
  88.     /** Holder for the instance.
  89.      * <p>We use here the Initialization On Demand Holder Idiom.</p>
  90.      */
  91.     private static class LazyHolder {
  92.         /** Cached field instance. */
  93.         private static final UnivariateDerivative2Field INSTANCE = new UnivariateDerivative2Field();
  94.     }
  95.     // CHECKSTYLE: resume HideUtilityClassConstructor

  96.     /** Handle deserialization of the singleton.
  97.      * @return the singleton instance
  98.      */
  99.     private Object readResolve() {
  100.         // return the singleton instance
  101.         return LazyHolder.INSTANCE;
  102.     }

  103. }