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.math3.ml.clustering;
019
020import java.io.Serializable;
021import java.util.Arrays;
022
023/**
024 * A simple implementation of {@link Clusterable} for points with double coordinates.
025 * @since 3.2
026 */
027public class DoublePoint implements Clusterable, Serializable {
028
029    /** Serializable version identifier. */
030    private static final long serialVersionUID = 3946024775784901369L;
031
032    /** Point coordinates. */
033    private final double[] point;
034
035    /**
036     * Build an instance wrapping an double array.
037     * <p>
038     * The wrapped array is referenced, it is <em>not</em> copied.
039     *
040     * @param point the n-dimensional point in double space
041     */
042    public DoublePoint(final double[] point) {
043        this.point = point;
044    }
045
046    /**
047     * Build an instance wrapping an integer array.
048     * <p>
049     * The wrapped array is copied to an internal double array.
050     *
051     * @param point the n-dimensional point in integer space
052     */
053    public DoublePoint(final int[] point) {
054        this.point = new double[point.length];
055        for ( int i = 0; i < point.length; i++) {
056            this.point[i] = point[i];
057        }
058    }
059
060    /** {@inheritDoc} */
061    public double[] getPoint() {
062        return point;
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public boolean equals(final Object other) {
068        if (!(other instanceof DoublePoint)) {
069            return false;
070        }
071        return Arrays.equals(point, ((DoublePoint) other).point);
072    }
073
074    /** {@inheritDoc} */
075    @Override
076    public int hashCode() {
077        return Arrays.hashCode(point);
078    }
079
080    /** {@inheritDoc} */
081    @Override
082    public String toString() {
083        return Arrays.toString(point);
084    }
085
086}