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.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    @Override
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}