    001    /**
    002     * Copyright 2009-2010 by The Broad Institute.
    003     *
    004
    005     */
    006    package org.broadinstitute.genee.matrix;
    007
    008    /**
    009     *
    010     * Fixed size abstract dataset.
    011     *
    012     */
    013    public abstract class FixedSizeAbstractDataset extends AbstractDataset {
    014
    015        protected MetadataModel columnMetadata;
    016
    017        protected int columns;
    018
    019        protected MetadataModel rowMetadata;
    020
    021        protected int rows;
    022
    023        /**
    024         * Creates a new dataset
    025         *
    026         * @param name
    027         *            the dataset name
    028         * @param rows
    029         *            the number of rows
    030         * @param columns
    031         *            the number of columns
    032         */
    033        protected FixedSizeAbstractDataset(String name, int rows, int columns) {
    034            this.name = name;
    035            this.rows = rows;
    036            this.columns = columns;
    037            rowMetadata = new DefaultMetadataModel(rows);
    038            columnMetadata = new DefaultMetadataModel(columns);
    039        }
    040
    041        /**
    042         * Inserts the specified series.
    043         *
    044         * @param name
    045         *            series name to be inserted
    046         * @param values
    047         *            array of values
    048         * @param c
    049         *            class of values
    050         * @return the series index
    051         */
    052        public int addSeries(String name, Object[][] values, Class<?> c) {
    053            seriesNames.add(name);
    054            seriesClasses.add(c);
    055            matrices.add(new ObjectMatrix(values));
    056            return seriesNames.size();
    057        }
    058
    059        @Override
    060        public int getColumnCount() {
    061            return columns;
    062        }
    063
    064        @Override
    065        public MetadataModel getColumnMetadata() {
    066            return columnMetadata;
    067        }
    068
    069        @Override
    070        public int getRowCount() {
    071            return rows;
    072        }
    073
    074        @Override
    075        public MetadataModel getRowMetadata() {
    076            return rowMetadata;
    077        }
    078
    079        public void setColumnMetadata(MetadataModel metadata) {
    080            if (metadata.getItemCount() != getColumnCount()) {
    081                throw new IllegalArgumentException("Wrong number of items.");
    082            }
    083            this.columnMetadata = metadata;
    084        }
    085
    086        public void setRowMetadata(MetadataModel metadata) {
    087            if (metadata.getItemCount() != getRowCount()) {
    088                throw new IllegalArgumentException("Wrong number of items.");
    089            }
    090            this.rowMetadata = metadata;
    091        }
    092
    093        protected Matrix createMatrix() {
    094            return new ObjectMatrix(getRowCount(), getColumnCount());
    095        }
    096
    097        static class ObjectMatrix implements Matrix {
    098            private Object[][] matrix;
    099
    100            public ObjectMatrix(Object[][] values) {
    101                this.matrix = values;
    102            }
    103
    104            private ObjectMatrix(int rows, int columns) {
    105                matrix = new Object[rows][columns];
    106            }
    107
    108            @Override
    109            public Object get(int row, int column) {
    110                return matrix[row][column];
    111            }
    112
    113            @Override
    114            public void set(int row, int column, Object value) {
    115                matrix[row][column] = value;
    116            }
    117        }
    118
    119    }