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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22
23 package org.hipparchus.clustering;
24
25 import java.io.Serializable;
26 import java.util.Arrays;
27
28 /**
29 * A simple implementation of {@link Clusterable} for points with double coordinates.
30 */
31 public class DoublePoint implements Clusterable, Serializable {
32
33 /** Serializable version identifier. */
34 private static final long serialVersionUID = 3946024775784901369L;
35
36 /** Point coordinates. */
37 private final double[] point;
38
39 /**
40 * Build an instance wrapping an double array.
41 * <p>
42 * The wrapped array is referenced, it is <em>not</em> copied.
43 *
44 * @param point the n-dimensional point in double space
45 */
46 public DoublePoint(final double[] point) {
47 this.point = point; // NOPMD - storage of array reference is intentional and documented here
48 }
49
50 /**
51 * Build an instance wrapping an integer array.
52 * <p>
53 * The wrapped array is copied to an internal double array.
54 *
55 * @param point the n-dimensional point in integer space
56 */
57 public DoublePoint(final int[] point) {
58 this.point = new double[point.length];
59 for ( int i = 0; i < point.length; i++) {
60 this.point[i] = point[i];
61 }
62 }
63
64 /** {@inheritDoc}
65 * <p>
66 * In this implementation of the {@link Clusterable} interface,
67 * the method <em>always</em> returns a reference to an internal array.
68 * </p>
69 */
70 @Override
71 public double[] getPoint() {
72 return point; // NOPMD - returning a reference to an internal array is documented here
73 }
74
75 /** {@inheritDoc} */
76 @Override
77 public boolean equals(final Object other) {
78 if (!(other instanceof DoublePoint)) {
79 return false;
80 }
81 return Arrays.equals(point, ((DoublePoint) other).point);
82 }
83
84 /** {@inheritDoc} */
85 @Override
86 public int hashCode() {
87 return Arrays.hashCode(point);
88 }
89
90 /** {@inheritDoc} */
91 @Override
92 public String toString() {
93 return Arrays.toString(point);
94 }
95
96 }