001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.annotation;
025    
026    import org.ujmp.core.Coordinates;
027    import org.ujmp.core.Matrix;
028    
029    public abstract class AbstractAnnotation implements Annotation {
030            private static final long serialVersionUID = 2939231340832922069L;
031    
032            private long[] size = null;
033    
034            public AbstractAnnotation(long... size) {
035                    this.size = Coordinates.copyOf(size);
036            }
037    
038            public final long[] getSize() {
039                    return size;
040            }
041    
042            public final int getDimensionCount() {
043                    return getSize().length;
044            }
045    
046            public final Object getAxisAnnotation(int axis) {
047                    Matrix m = getDimensionMatrix(axis);
048                    return m.getMatrixAnnotation();
049            }
050    
051            public final void setAxisAnnotation(int axis, Object value) {
052                    Matrix m = getDimensionMatrix(axis);
053                    m.setMatrixAnnotation(value);
054            }
055    
056            public final String toString() {
057                    StringBuilder s = new StringBuilder();
058                    String EOL = System.getProperty("line.separator");
059                    s.append("Label: " + getMatrixAnnotation() + EOL);
060                    for (int i = 0; i < getDimensionCount(); i++) {
061                            s.append("Dimension " + i + ":" + EOL);
062                            s.append(getDimensionMatrix(i));
063                            s.append(EOL);
064                    }
065                    return s.toString();
066            }
067    
068            @Override
069            public final int hashCode() {
070                    final int prime = 31;
071                    int result = 1;
072                    result = prime * result
073                                    + ((getMatrixAnnotation() == null) ? 0 : getMatrixAnnotation().hashCode());
074                    for (int i = 0; i < getDimensionCount(); i++) {
075                            result = prime * result
076                                            + ((getDimensionMatrix(i) == null) ? 0 : getDimensionMatrix(i).hashCode());
077                    }
078                    return result;
079            }
080    
081            @Override
082            public final boolean equals(Object obj) {
083                    if (this == obj) {
084                            return true;
085                    }
086                    if (obj == null) {
087                            return false;
088                    }
089                    if (getClass() != obj.getClass()) {
090                            return false;
091                    }
092                    DefaultAnnotation other = (DefaultAnnotation) obj;
093                    if (getDimensionCount() != other.getDimensionCount()) {
094                            return false;
095                    }
096                    if (getMatrixAnnotation() == null) {
097                            if (other.getMatrixAnnotation() != null) {
098                                    return false;
099                            }
100                    } else if (!getMatrixAnnotation().equals(other.getMatrixAnnotation())) {
101                            return false;
102                    }
103                    for (int i = 0; i < getDimensionCount(); i++) {
104                            if (getDimensionMatrix(i) == null) {
105                                    if (other.getDimensionMatrix(i) != null) {
106                                            return false;
107                                    }
108                            } else if (!getDimensionMatrix(i).equals(other.getDimensionMatrix(i))) {
109                                    return false;
110                            }
111                    }
112                    return true;
113            }
114    
115            public abstract Annotation clone();
116    
117    }