View Javadoc
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  
18  package org.apache.commons.math4.legacy.ml.clustering;
19  
20  import java.util.Arrays;
21  
22  /**
23   * A simple implementation of {@link Clusterable} for points with double coordinates.
24   * @since 3.2
25   */
26  public class DoublePoint implements Clusterable {
27      /** Point coordinates. */
28      private final double[] point;
29  
30      /**
31       * Build an instance wrapping an double array.
32       * <p>
33       * The wrapped array is referenced, it is <em>not</em> copied.
34       *
35       * @param point the n-dimensional point in double space
36       */
37      public DoublePoint(final double[] point) {
38          this.point = point;
39      }
40  
41      /**
42       * Build an instance wrapping an integer array.
43       * <p>
44       * The wrapped array is copied to an internal double array.
45       *
46       * @param point the n-dimensional point in integer space
47       */
48      public DoublePoint(final int[] point) {
49          this.point = new double[point.length];
50          for ( int i = 0; i < point.length; i++) {
51              this.point[i] = point[i];
52          }
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public double[] getPoint() {
58          return point;
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public boolean equals(final Object other) {
64          if (!(other instanceof DoublePoint)) {
65              return false;
66          }
67          return Arrays.equals(point, ((DoublePoint) other).point);
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public int hashCode() {
73          return Arrays.hashCode(point);
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public String toString() {
79          return Arrays.toString(point);
80      }
81  }