/** * Returns a table which is the same as the input table, but with a zero-based index column as the * first column. * * @param inTable input table * @return output table */ private static StarTable prependIndex(StarTable inTable) { final long nrow = inTable.getRowCount(); ColumnStarTable indexTable = new ColumnStarTable(inTable) { public long getRowCount() { return nrow; } }; indexTable.addColumn( new ColumnData(INDEX_INFO) { public Object readValue(long irow) { return new Integer((int) irow); } }); return new JoinStarTable(new StarTable[] {indexTable, inTable}); }
/** * Turns a TopcatModel into a StarTable, ready for serialization. * * @param tcModel model * @return table */ public StarTable encode(TopcatModel tcModel) { /* Prepare table data and metadata for use and adjustment. */ final StarTable dataModel = tcModel.getDataModel(); List<DescribedValue> paramList = new ArrayList<DescribedValue>(); long nrow = dataModel.getRowCount(); ColumnStarTable extraTable = ColumnStarTable.makeTableWithRows(nrow); /* Mark as serialized TopcatModel. */ paramList.add(new DescribedValue(IS_TCMODEL_INFO, Boolean.TRUE)); paramList.add(new DescribedValue(VERSION_INFO, TopcatUtils.getVersion())); /* Record label. */ paramList.add(new DescribedValue(LABEL_INFO, tcModel.getLabel())); /* Record column sequences. */ ColumnList colList = tcModel.getColumnList(); int nCol = colList.size(); int[] icols = new int[nCol]; boolean[] activs = new boolean[nCol]; for (int jc = 0; jc < nCol; jc++) { icols[jc] = colList.getColumn(jc).getModelIndex(); activs[jc] = colList.isActive(jc); } paramList.add(new DescribedValue(COLS_INDEX_INFO, icols)); paramList.add(new DescribedValue(COLS_VISIBLE_INFO, activs)); /* Record whether to broadcast rows. */ paramList.add(new DescribedValue(SEND_ROWS_INFO, tcModel.getRowSendModel().isSelected())); /* Record sort order. */ SortOrder sortOrder = tcModel.getSelectedSort(); TableColumn sortCol = sortOrder == null ? null : sortOrder.getColumn(); if (sortCol != null) { int icolSort = tcModel.getColumnList().indexOf(sortCol); if (icolSort >= 0) { boolean sense = tcModel.getSortSenseModel().isSelected(); paramList.add(new DescribedValue(SORT_COLUMN_INFO, new Integer(icolSort))); paramList.add(new DescribedValue(SORT_SENSE_INFO, Boolean.valueOf(sense))); } } /* Store row subset flags in a new column. */ List<RowSubset> subsetList = new ArrayList<RowSubset>(tcModel.getSubsets()); boolean hadAll = subsetList.remove(RowSubset.ALL); assert hadAll; RowSubset[] subsets = subsetList.toArray(new RowSubset[0]); if (subsets.length > 0) { String[] subsetNames = new String[subsets.length]; for (int is = 0; is < subsets.length; is++) { subsetNames[is] = subsets[is].getName(); } paramList.add(new DescribedValue(SUBSET_NAMES_INFO, subsetNames)); Object flagsArray = createFlagsArray(dataModel, subsets); if (flagsArray != null) { ColumnData flagsCol = ArrayColumn.makeColumn(SUBSET_FLAGS_INFO.getName(), flagsArray); ColumnInfo info = new ColumnInfo(SUBSET_FLAGS_INFO); info.setContentClass(flagsCol.getColumnInfo().getContentClass()); flagsCol.setColumnInfo(info); extraTable.addColumn(flagsCol); } } /* Record current subset. */ int iset = subsetList.indexOf(tcModel.getSelectedSubset()); if (iset >= 0) { paramList.add(new DescribedValue(CURRENT_SUBSET_INFO, new Integer(iset))); } /* Copy parameters from the input table. * Be paranoid about possible name clashes. */ for (Iterator it = dataModel.getParameters().iterator(); it.hasNext(); ) { Object item = it.next(); if (item instanceof DescribedValue) { DescribedValue dval = (DescribedValue) item; String name = dval.getInfo().getName(); String utype = dval.getInfo().getUtype(); if (!isCodecUtype(utype)) { paramList.add(dval); } } } /* Prepare the output table object. */ List<StarTable> joinList = new ArrayList<StarTable>(); joinList.add(dataModel); if (extraTable.getColumnCount() > 0) { joinList.add(extraTable); } StarTable[] joins = joinList.toArray(new StarTable[0]); StarTable outTable = new MetaCopyStarTable(new JoinStarTable(joinList.toArray(new StarTable[0]))); outTable.setName(dataModel.getName()); /* Set the parameters. */ outTable.getParameters().clear(); outTable.getParameters().addAll(paramList); /* Return the result. */ return outTable; }