Column.java :  » Development » JOpt-Simple » joptsimple » util » Java Open Source

Java Open Source » Development » JOpt Simple 
JOpt Simple » joptsimple » util » Column.java
/*
 Copyright 2004-2007 Paul R. Holser, Jr.  All rights reserved.
 Licensed under the Academic Free License version 3.0
 */

package joptsimple.util;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
 * @since 2.1
 * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 * @version $Id: Column.java,v 1.5 2007/04/10 20:06:27 pholser Exp $
 */
public class Column {
    static final Comparator BY_HEIGHT = new Comparator() {
        public int compare( Object first, Object second ) {
            return ( (Column) first ).height() - ( (Column) second ).height();
        }
    };

    private final String header;
    private final List data;
    private int width;
    private int height;

    Column( String header ) {
        this.header = header;
        data = new LinkedList();
        width = header.length();
        height = 0;
    }

    void addRow( Object row ) {
        String asString = String.valueOf( row );

        data.add( asString );
        width = Math.max( width, asString.length() );
        ++height;
    }

    void writeHeaderOn( StringBuffer buffer ) {
        buffer.append( header )
            .append( StringUtilities.repeat( ' ', width - header.length() ) )
            .append( ' ' );
    }

    void writeSeparatorOn( StringBuffer buffer ) {
        buffer.append( StringUtilities.repeat( '-', width ) )
            .append( ' ' );
    }

    void writeCellOn( int index, StringBuffer buffer ) {
        if ( index < data.size() ) {
            String item = (String) data.get( index );

            buffer.append( item )
                .append( StringUtilities.repeat( ' ', width - item.length() ) )
                .append( ' ' );
        }
    }

    int height() {
        return height;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.