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