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.calculation;
025    
026    import org.ujmp.core.Coordinates;
027    import org.ujmp.core.Matrix;
028    import org.ujmp.core.Matrix.StorageType;
029    import org.ujmp.core.annotation.Annotation;
030    import org.ujmp.core.exceptions.MatrixException;
031    import org.ujmp.core.util.CoordinateIterator;
032    
033    public abstract class AbstractCalculation implements Calculation {
034    
035            private static final long serialVersionUID = -36063772015381070L;
036    
037            private Matrix[] sources = null;
038    
039            private int dimension = NONE;
040    
041            private Annotation annotation = null;
042    
043            public AbstractCalculation(Matrix... sources) {
044                    this.sources = sources;
045                    this.annotation = sources.length == 0 ? null : sources[0].getAnnotation();
046            }
047    
048            public AbstractCalculation(int dimension, Matrix... sources) {
049                    this.sources = sources;
050                    this.annotation = sources.length == 0 ? null : sources[0].getAnnotation();
051                    this.dimension = dimension;
052            }
053    
054            public boolean isSparse() {
055                    return false;
056            }
057    
058            public void setAnnotation(Annotation annotation) {
059                    this.annotation = annotation;
060            }
061    
062            public long getValueCount() {
063                    return Coordinates.product(getSize());
064            }
065    
066            public Iterable<long[]> availableCoordinates() {
067                    return allCoordinates();
068            }
069    
070            public Iterable<long[]> allCoordinates() {
071                    return new CoordinateIterator(getSize());
072            }
073    
074            public boolean contains(long... coordinates) {
075                    return Coordinates.isSmallerThan(coordinates, getSize());
076            }
077    
078            public Annotation getAnnotation() {
079                    return annotation;
080            }
081    
082            public final Matrix getSource() {
083                    return sources[0];
084            }
085    
086            public final Matrix[] getSources() {
087                    return sources;
088            }
089    
090            public void setSources(Matrix... sources) {
091                    this.sources = sources;
092            }
093    
094            public int getDimension() {
095                    return dimension;
096            }
097    
098            public void setDimension(int dimension) {
099                    this.dimension = dimension;
100            }
101    
102            public long[] getSize() {
103                    return getSource().getSize();
104            }
105    
106            public final Matrix calc(Ret returnType) throws MatrixException {
107                    switch (returnType) {
108                    case ORIG:
109                            return calcOrig();
110                    case LINK:
111                            return calcLink();
112                    default: // must be NEW
113                            return calcNew();
114                    }
115            }
116    
117            public Matrix[] calcMulti() throws MatrixException {
118                    return new Matrix[] { calcNew() };
119            }
120    
121            public final StorageType getStorageType() {
122                    return getSource().getStorageType();
123            }
124    
125    }