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.  *      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.math4.legacy.ml.clustering;

  18. import java.util.Arrays;

  19. /**
  20.  * A simple implementation of {@link Clusterable} for points with double coordinates.
  21.  * @since 3.2
  22.  */
  23. public class DoublePoint implements Clusterable {
  24.     /** Point coordinates. */
  25.     private final double[] point;

  26.     /**
  27.      * Build an instance wrapping an double array.
  28.      * <p>
  29.      * The wrapped array is referenced, it is <em>not</em> copied.
  30.      *
  31.      * @param point the n-dimensional point in double space
  32.      */
  33.     public DoublePoint(final double[] point) {
  34.         this.point = point;
  35.     }

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

  49.     /** {@inheritDoc} */
  50.     @Override
  51.     public double[] getPoint() {
  52.         return point;
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public boolean equals(final Object other) {
  57.         if (!(other instanceof DoublePoint)) {
  58.             return false;
  59.         }
  60.         return Arrays.equals(point, ((DoublePoint) other).point);
  61.     }

  62.     /** {@inheritDoc} */
  63.     @Override
  64.     public int hashCode() {
  65.         return Arrays.hashCode(point);
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     public String toString() {
  70.         return Arrays.toString(point);
  71.     }
  72. }