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    
018    package org.apache.commons.math.stat.clustering;
019    
020    import java.io.Serializable;
021    import java.util.Collection;
022    
023    import org.apache.commons.math.util.MathUtils;
024    
025    /**
026     * A simple implementation of {@link Clusterable} for points with integer coordinates.
027     * @version $Revision: 1042376 $ $Date: 2010-12-05 16:54:55 +0100 (dim. 05 d??c. 2010) $
028     * @since 2.0
029     */
030    public class EuclideanIntegerPoint implements Clusterable<EuclideanIntegerPoint>, Serializable {
031    
032        /** Serializable version identifier. */
033        private static final long serialVersionUID = 3946024775784901369L;
034    
035        /** Point coordinates. */
036        private final int[] point;
037    
038        /**
039         * Build an instance wrapping an integer array.
040         * <p>The wrapped array is referenced, it is <em>not</em> copied.</p>
041         * @param point the n-dimensional point in integer space
042         */
043        public EuclideanIntegerPoint(final int[] point) {
044            this.point = point;
045        }
046    
047        /**
048         * Get the n-dimensional point in integer space.
049         * @return a reference (not a copy!) to the wrapped array
050         */
051        public int[] getPoint() {
052            return point;
053        }
054    
055        /** {@inheritDoc} */
056        public double distanceFrom(final EuclideanIntegerPoint p) {
057            return MathUtils.distance(point, p.getPoint());
058        }
059    
060        /** {@inheritDoc} */
061        public EuclideanIntegerPoint centroidOf(final Collection<EuclideanIntegerPoint> points) {
062            int[] centroid = new int[getPoint().length];
063            for (EuclideanIntegerPoint p : points) {
064                for (int i = 0; i < centroid.length; i++) {
065                    centroid[i] += p.getPoint()[i];
066                }
067            }
068            for (int i = 0; i < centroid.length; i++) {
069                centroid[i] /= points.size();
070            }
071            return new EuclideanIntegerPoint(centroid);
072        }
073    
074        /** {@inheritDoc} */
075        @Override
076        public boolean equals(final Object other) {
077            if (!(other instanceof EuclideanIntegerPoint)) {
078                return false;
079            }
080            final int[] otherPoint = ((EuclideanIntegerPoint) other).getPoint();
081            if (point.length != otherPoint.length) {
082                return false;
083            }
084            for (int i = 0; i < point.length; i++) {
085                if (point[i] != otherPoint[i]) {
086                    return false;
087                }
088            }
089            return true;
090        }
091    
092        /** {@inheritDoc} */
093        @Override
094        public int hashCode() {
095            int hashCode = 0;
096            for (Integer i : point) {
097                hashCode += i.hashCode() * 13 + 7;
098            }
099            return hashCode;
100        }
101    
102        /**
103         * {@inheritDoc}
104         * @since 2.1
105         */
106        @Override
107        public String toString() {
108            final StringBuilder buff = new StringBuilder("(");
109            final int[] coordinates = getPoint();
110            for (int i = 0; i < coordinates.length; i++) {
111                buff.append(coordinates[i]);
112                if (i < coordinates.length - 1) {
113                    buff.append(",");
114                }
115            }
116            buff.append(")");
117            return buff.toString();
118        }
119    
120    }