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.math3.ml.clustering;

  18. import java.io.Serializable;
  19. import java.util.Arrays;

  20. /**
  21.  * A simple implementation of {@link Clusterable} for points with double coordinates.
  22.  * @since 3.2
  23.  */
  24. public class DoublePoint implements Clusterable, Serializable {

  25.     /** Serializable version identifier. */
  26.     private static final long serialVersionUID = 3946024775784901369L;

  27.     /** Point coordinates. */
  28.     private final double[] point;

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

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

  52.     /** {@inheritDoc} */
  53.     public double[] getPoint() {
  54.         return point;
  55.     }

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

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

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

  74. }