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 java.util.Arrays;
027    import java.util.HashMap;
028    import java.util.Map;
029    
030    import org.ujmp.core.Coordinates;
031    import org.ujmp.core.Matrix;
032    import org.ujmp.core.MatrixFactory;
033    import org.ujmp.core.enums.ValueType;
034    import org.ujmp.core.exceptions.MatrixException;
035    
036    public class DefaultAnnotation extends AbstractAnnotation {
037            private static final long serialVersionUID = -7988756144808776868L;
038    
039            private Object matrixAnnotation = null;
040    
041            private Map<Integer, Matrix> dimensionMatrices = null;
042    
043            public DefaultAnnotation(long[] size) {
044                    super(size);
045            }
046    
047            public Matrix getDimensionMatrix(int dimension) {
048                    if (dimensionMatrices == null) {
049                            dimensionMatrices = new HashMap<Integer, Matrix>(getDimensionCount());
050                    }
051                    Matrix m = dimensionMatrices.get(dimension);
052                    if (m == null) {
053                            long[] t = Coordinates.copyOf(getSize());
054                            t[dimension] = 1;
055                            m = MatrixFactory.sparse(ValueType.OBJECT, t);
056                            dimensionMatrices.put(dimension, m);
057                    }
058                    return m;
059            }
060    
061            public Object getMatrixAnnotation() {
062                    return matrixAnnotation;
063            }
064    
065            public void setMatrixAnnotation(Object matrixAnnotation) {
066                    this.matrixAnnotation = matrixAnnotation;
067            }
068    
069            public Annotation clone() {
070                    Annotation a = new DefaultAnnotation(getSize());
071                    a.setMatrixAnnotation(getMatrixAnnotation());
072                    for (int i = 0; i < getDimensionCount(); i++) {
073                            a.setDimensionMatrix(i, getDimensionMatrix(i).clone());
074                    }
075                    return a;
076            }
077    
078            public void clear() {
079                    matrixAnnotation = null;
080                    dimensionMatrices = null;
081            }
082    
083            public Object getAxisAnnotation(int dimension, long... position) {
084                    Matrix m = getDimensionMatrix(dimension);
085                    long old = position[dimension];
086                    position[dimension] = 0;
087                    Object o = null;
088                    if (Coordinates.isSmallerThan(position, m.getSize())) {
089                            o = m.getAsObject(position);
090                    }
091                    position[dimension] = old;
092                    return o;
093            }
094    
095            public long[] getPositionForLabel(int dimension, Object label) {
096                    if (label == null) {
097                            throw new MatrixException("label is null");
098                    }
099                    Matrix m = getDimensionMatrix(dimension);
100                    for (long[] c : m.availableCoordinates()) {
101                            Object o = m.getAsObject(c);
102                            if (label.equals(o)) {
103                                    return c;
104                            }
105                    }
106                    long[] t = new long[getDimensionCount()];
107                    Arrays.fill(t, -1);
108                    return t;
109            }
110    
111            public void setAxisAnnotation(int dimension, Object label, long... position) {
112                    Matrix m = getDimensionMatrix(dimension);
113                    long old = position[dimension];
114                    position[dimension] = 0;
115                    m.setAsObject(label, position);
116                    position[dimension] = old;
117            }
118    
119            public void setDimensionMatrix(int dimension, Matrix matrix) {
120                    if (dimensionMatrices == null) {
121                            dimensionMatrices = new HashMap<Integer, Matrix>(getDimensionCount());
122                    }
123                    if (matrix == null) {
124                            dimensionMatrices.put(dimension, null);
125                    } else {
126                            dimensionMatrices.put(dimension, matrix);
127                    }
128            }
129    
130    }