All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.ac.starlink.table.AbstractStarTable Maven / Gradle / Ivy

package uk.ac.starlink.table;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Abstract base class providing an implementation of the generic and
 * straightforward parts of the StarTable interface.
 * This implementation assumes that random access is not available;
 * subclasses which provide random access should override 
 * the isRandom, getCell and perhaps getRow methods.
 *
 * @author   Mark Taylor (Starlink)
 */
public abstract class AbstractStarTable implements StarTable {

    private List parameters_ = new ArrayList();
    private String name_;
    private URL url_;

    /**
     * Goes through the table columns (ColumnInfo objects) 
     * and picks out all the AuxData items which exist, generalising
     * where necessary and returning a union of them in 
     * alphabetical order by name.
     * Subclasses should override this if they can do better, for instance
     * providing an order for the keys.
     *
     * @return  a list of all the auxiliary metadata ValueInfo items
     *          which in fact crop up in column metadata
     */
    public List getColumnAuxDataInfos() {
        Map auxMap = new TreeMap();
        for ( int i = 0; i < getColumnCount(); i++ ) {
            for ( DescribedValue dval : getColumnInfo( i ).getAuxData() ) {

                /* Construct a ValueInfo based on this DescribedValue. */
                ValueInfo info = dval.getInfo();
                String name = info.getName();

                /* We already have one by this name, if necessary generalise
                 * the stored ValueInfo so that it is consistent with this 
                 * one too. */
                if ( auxMap.containsKey( name ) ) {
                    ValueInfo oldInfo = auxMap.get( name );
                    auxMap.put( name, 
                                DefaultValueInfo.generalise( oldInfo, info ) );
                }

                /* Not encountered one with this name before, put it 
                 * straight in the pool. */
                else {
                    auxMap.put( name, info );
                }
            }
        }
        return Collections
              .unmodifiableList( new ArrayList( auxMap.values() ) );
    }

    public List getParameters() {
        return parameters_;
    }

    /**
     * Sets the list of table parameters, that is items which pertain
     * to the entire table.  Each element of the provided list 
     * parameters should be a {@link DescribedValue} object.
     *
     * @param  parameters   a List of DescribedValues pertaining
     *         to this table
     */
    public void setParameters( List parameters ) {
        parameters_ = parameters;
    }

    public String getName() {
        return name_;
    }

    /**
     * Sets the name for this table. 
     *
     * @param  name  the table name - may be null
     */
    public void setName( String name ) {
        name_ = name;
    }

    public URL getURL() {
        return url_;
    }

    /**
     * Sets the URL for this table.
     *
     * @param  url  the URL where this table lives - may be null
     */
    public void setURL( URL url ) {
        url_ = url;
    }

    /**
     * Convenience method to get an int value from a long.
     * Invokes {@link Tables#checkedLongToInt}.
     */
    public static int checkedLongToInt( long lval ) {
        return Tables.checkedLongToInt( lval );
    }

    /**
     * The AbstractStarTable implementation of this method 
     * returns false.
     */
    public boolean isRandom() {
        return false;
    }

    public RowAccess getRowAccess() throws IOException {
        throw new UnsupportedOperationException( "No random access available" );
    }

    /**
     * Returns a default splittable which relies on table random access
     * if available, or otherwise provides only sequential access (no splits).
     *
     * 

It is often possible to provide a better implementation than this. * * @return {@link Tables#getDefaultRowSplittable * Tables.getDefaultRowSplittable(this)} */ public RowSplittable getRowSplittable() throws IOException { return Tables.getDefaultRowSplittable( this ); } /** * The AbstractStarTable implementation of this method throws an * UnsupportedOperationException, since unless otherwise * provided there is no random access. */ public Object getCell( long irow, int icol ) throws IOException { throw new UnsupportedOperationException( "No random access available" ); } /** * The AbstractStarTable implementation of this method * constructs a row by repeated invocation of {@link #getCell}. */ public Object[] getRow( long irow ) throws IOException { int ncol = getColumnCount(); Object[] row = new Object[ ncol ]; for ( int icol = 0; icol < ncol; icol++ ) { row[ icol ] = getCell( irow, icol ); } return row; } /** * The AbstractStarTable implementation of this method * does nothing. */ public void close() throws IOException { } abstract public ColumnInfo getColumnInfo( int icol ); abstract public int getColumnCount(); abstract public long getRowCount(); abstract public RowSequence getRowSequence() throws IOException; }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy