DoublePoint.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.  *      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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.clustering;

  22. import java.io.Serializable;
  23. import java.util.Arrays;

  24. /**
  25.  * A simple implementation of {@link Clusterable} for points with double coordinates.
  26.  */
  27. public class DoublePoint implements Clusterable, Serializable {

  28.     /** Serializable version identifier. */
  29.     private static final long serialVersionUID = 3946024775784901369L;

  30.     /** Point coordinates. */
  31.     private final double[] point;

  32.     /**
  33.      * Build an instance wrapping an double array.
  34.      * <p>
  35.      * The wrapped array is referenced, it is <em>not</em> copied.
  36.      *
  37.      * @param point the n-dimensional point in double space
  38.      */
  39.     public DoublePoint(final double[] point) {
  40.         this.point = point; // NOPMD - storage of array reference is intentional and documented here
  41.     }

  42.     /**
  43.      * Build an instance wrapping an integer array.
  44.      * <p>
  45.      * The wrapped array is copied to an internal double array.
  46.      *
  47.      * @param point the n-dimensional point in integer space
  48.      */
  49.     public DoublePoint(final int[] point) {
  50.         this.point = new double[point.length];
  51.         for ( int i = 0; i < point.length; i++) {
  52.             this.point[i] = point[i];
  53.         }
  54.     }

  55.     /** {@inheritDoc}
  56.      * <p>
  57.      * In this implementation of the {@link Clusterable} interface,
  58.      * the method <em>always</em> returns a reference to an internal array.
  59.      * </p>
  60.      */
  61.     @Override
  62.     public double[] getPoint() {
  63.         return point; // NOPMD - returning a reference to an internal array is documented here
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public boolean equals(final Object other) {
  68.         if (!(other instanceof DoublePoint)) {
  69.             return false;
  70.         }
  71.         return Arrays.equals(point, ((DoublePoint) other).point);
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public int hashCode() {
  76.         return Arrays.hashCode(point);
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public String toString() {
  81.         return Arrays.toString(point);
  82.     }

  83. }