    001    package org.broadinstitute.genee.matrix;
    002
    003    import java.util.ArrayList;
    004    import java.util.Collection;
    005    import java.util.HashMap;
    006    import java.util.List;
    007    import java.util.Map;
    008
    009    /**
    010     *
    011     * Abstract dataset.
    012     *
    013     */
    014    public abstract class AbstractDataset implements Dataset {
    015
    016        protected List<Matrix> matrices = new ArrayList<Matrix>();
    017        protected String name;
    018        protected Map<Object, Object> properties = new HashMap<Object, Object>();
    019        protected List<Class<?>> seriesClasses = new ArrayList<Class<?>>();
    020        protected List<String> seriesNames = new ArrayList<String>();
    021
    022        public AbstractDataset() {
    023        }
    024
    025        public int addSeries(String name, Class<?> c) {
    026            seriesNames.add(name);
    027            seriesClasses.add(c);
    028            matrices.add(createMatrix());
    029            return seriesNames.size();
    030        }
    031
    032        @Override
    033        public final String getName() {
    034            return name;
    035        }
    036
    037        @Override
    038        public Object getObjectValue(int rowIndex, int columnIndex, int seriesIndex) {
    039            if (seriesIndex == 0) {
    040                return getValue(rowIndex, columnIndex);
    041            }
    042            Matrix matrix = matrices.get(seriesIndex - 1);
    043            return matrix.get(rowIndex, columnIndex);
    044        }
    045
    046        @Override
    047        public Object getProperty(Object key) {
    048            return properties.get(key);
    049        }
    050
    051        @Override
    052        public Collection<Object> getPropertyKeys() {
    053            return properties.keySet();
    054        }
    055
    056        @Override
    057        public Class<?> getSeriesClass(int series) {
    058            if (series == 0) {
    059                return Float.class;
    060            }
    061            return seriesClasses.get(series - 1);
    062        }
    063
    064        @Override
    065        public int getSeriesCount() {
    066            return matrices.size() + 1;
    067        }
    068
    069        @Override
    070        public String getSeriesName(int series) {
    071            if (series == 0) {
    072                return getName();
    073            }
    074            return seriesNames.get(series - 1);
    075        }
    076
    077        @Override
    078        public float getValue(int rowIndex, int columnIndex, int seriesIndex) {
    079            if (seriesIndex == 0) {
    080                return getValue(rowIndex, columnIndex);
    081            }
    082            Matrix matrix = matrices.get(seriesIndex - 1);
    083            Number number = (Number) matrix.get(rowIndex, columnIndex);
    084            return number != null ? number.floatValue() : Float.NaN;
    085        }
    086
    087        public void removeSeries(int index) {
    088            if (index > 0) {
    089                seriesNames.remove(index - 1);
    090                seriesClasses.remove(index - 1);
    091                matrices.remove(index - 1);
    092            }
    093        }
    094
    095        @Override
    096        public void setName(String name) {
    097            this.name = name;
    098        }
    099
    100        /**
    101         * Sets the value at the given row and column.
    102         *
    103         * @param rowIndex
    104         *            the row index.
    105         * @param columnIndex
    106         *            the column index.
    107         * @param seriesIndex
    108         *            the series index.
    109         * @throws IndexOutOfBoundsException
    110         *             if <tt>rowIndex</tt> or <tt>columnIndex</tt> are out of
    111         *             range.
    112         */
    113        public void setObjectValue(int rowIndex, int columnIndex, Object value, int seriesIndex) {
    114            if (seriesIndex == 0) {
    115                setValue(rowIndex, columnIndex, ((Number) value).floatValue());
    116            } else {
    117                Matrix m = matrices.get(seriesIndex - 1);
    118                m.set(rowIndex, columnIndex, value);
    119            }
    120        }
    121
    122        @Override
    123        public void setProperty(Object key, Object value) {
    124            if (value == null) {
    125                properties.remove(key);
    126            } else {
    127                properties.put(key, value);
    128            }
    129        }
    130
    131        protected abstract Matrix createMatrix();
    132
    133        protected static interface Matrix {
    134
    135            public Object get(int row, int column);
    136
    137            public void set(int row, int column, Object value);
    138        }
    139
    140    }