/** * Constructor. * * @param inTable saved table as read */ public CodecTable(StarTable inTable) { /* Sort out table parameters. */ codecParamMap_ = new HashMap<String, DescribedValue>(); List<DescribedValue> dataParamList = new ArrayList<DescribedValue>(); for (Iterator it = inTable.getParameters().iterator(); it.hasNext(); ) { DescribedValue param = (DescribedValue) it.next(); String utype = param.getInfo().getUtype(); if (isCodecUtype(utype)) { codecParamMap_.put(utype, param); } else { dataParamList.add(param); } } /* Sort out table columns. */ codecIcolMap_ = new HashMap<String, Integer>(); IntList dataIcolList = new IntList(); for (int icol = 0; icol < inTable.getColumnCount(); icol++) { ColumnInfo info = inTable.getColumnInfo(icol); String utype = info.getUtype(); if (isCodecUtype(utype)) { codecIcolMap_.put(utype, icol); } else { dataIcolList.add(icol); } } int[] dataColMap = dataIcolList.toIntArray(); /* Construct a table containing only the data items. */ dataTable_ = new MetaCopyStarTable(new ColumnPermutedStarTable(inTable, dataColMap, true)); dataTable_.getParameters().clear(); dataTable_.getParameters().addAll(dataParamList); }
/** * 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; }