001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math4.legacy.ml.clustering;
019
020import java.util.Arrays;
021
022/**
023 * A simple implementation of {@link Clusterable} for points with double coordinates.
024 * @since 3.2
025 */
026public class DoublePoint implements Clusterable {
027    /** Point coordinates. */
028    private final double[] point;
029
030    /**
031     * Build an instance wrapping an double array.
032     * <p>
033     * The wrapped array is referenced, it is <em>not</em> copied.
034     *
035     * @param point the n-dimensional point in double space
036     */
037    public DoublePoint(final double[] point) {
038        this.point = point;
039    }
040
041    /**
042     * Build an instance wrapping an integer array.
043     * <p>
044     * The wrapped array is copied to an internal double array.
045     *
046     * @param point the n-dimensional point in integer space
047     */
048    public DoublePoint(final int[] point) {
049        this.point = new double[point.length];
050        for ( int i = 0; i < point.length; i++) {
051            this.point[i] = point[i];
052        }
053    }
054
055    /** {@inheritDoc} */
056    @Override
057    public double[] getPoint() {
058        return point;
059    }
060
061    /** {@inheritDoc} */
062    @Override
063    public boolean equals(final Object other) {
064        if (!(other instanceof DoublePoint)) {
065            return false;
066        }
067        return Arrays.equals(point, ((DoublePoint) other).point);
068    }
069
070    /** {@inheritDoc} */
071    @Override
072    public int hashCode() {
073        return Arrays.hashCode(point);
074    }
075
076    /** {@inheritDoc} */
077    @Override
078    public String toString() {
079        return Arrays.toString(point);
080    }
081}