Add tags

Add tags to the following comment
import java.io.*;
import java.util.*;

class Table {
	
	public static void main( String[] args ) {
		Table table = new Table()
			.input( "in.tsv" )
			.sort( 0 )
			.replace( 1, 2 )
			.increment( 3 )
			.output( "out.tsv" );
	}
	
	Cell[][] cells;
	
	class Cell {
		String value;
		Cell( String value ) {
			this.value = value;
		}
		public String toString() { return value; }
		public int compareTo( Cell that ) { return this.value.compareTo( that.value ); }
		public void increment() { value = ( Integer.parseInt( value ) + 1 ) + ""; }
	}
	
	class Sorter implements Comparable {
		Cell key;
		Cell[] row;
		Sorter( Cell key, Cell[] row ) {
			this.key = key;
			this.row = row;
		}
		public int compareTo( Object that ) {
			return this.key.compareTo( ( (Sorter) that ).key );
		}
	}
	
	Table input( String file ) {
		try {
			BufferedReader in = new BufferedReader( new FileReader( file ) );
			List<Cell[]> list = new ArrayList<Cell[]>();
			String line;
			while ( ( line = in.readLine() ) != null ) {
				list.add( makeCells( line.split( "\t" ) ) );
			}
			in.close();
			cells = list.toArray( new Cell[0][] );
			return this;
		} catch ( IOException e ) { throw new RuntimeException ( e ); }
	}
	
	Table sort( int ic ) {
		Cell[] column = column( ic );
		Sorter[] sorters = new Sorter[rows()-1];
		for ( int ir = 1; ir < rows(); ++ir ) {
			sorters[ir-1] = new Sorter( column[ir], row( ir ) );
		}
		Arrays.sort( sorters );
		for ( int ir = 1; ir < rows(); ++ir ) {
			cells[ir] = sorters[ir-1].row;
		}
		return this;
	}
	
	Table replace( int ic1, int ic2 ) {
		Cell[] column1 = column( ic1 );
		Cell[] column2 = column( ic2 );
		set( ic1, column2 );
		set( ic2, column1 );
		return this;
	}
	
	Table increment( int ic ) {
		Cell[] column = column( ic );
		for ( int ir = 1; ir < rows(); ++ir ) {
			column[ir].increment();
		}
		return this;
	}
	
	Table output( String file ) {
		try {
			PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter( file ) ) );
			out.print( toString() );
			out.flush();
			out.close();
			return this;
		} catch ( IOException e ) { throw new RuntimeException( e ); }
	}

	Cell[] makeCells( String[] array ) {
		Cell[] cells = new Cell[array.length];
		for ( int i = 0; i < array.length; ++i ) {
			cells[i] = new Cell( array[i] );
		}
		return cells;
	}
	
	void set( int ic, Cell[] column ) {
		for ( int ir = 0; ir < rows(); ++ir ) {
			cells[ir][ic] = column[ir];
		}
	}
	
	Cell[] row( int ir ) {
		Cell[] bak = new Cell[columns()];
		for ( int ic = 0; ic < columns(); ++ic ) {
			bak[ic] = cells[ir][ic];
		}
		return bak;
	}
	
	Cell[] column( int ic ) {
		Cell[] bak = new Cell[rows()];
		for ( int ir = 0; ir < rows(); ++ir ) {
			bak[ir] = cells[ir][ic];
		}
		return bak;
	}
	
	int rows() { return cells.length; }
	
	int columns() { return cells[0].length; }
	
	public String toString() {
		StringBuilder bak = new StringBuilder();
		for ( int ir = 0; ir < cells.length; ++ir ) {
			for ( int ic = 0; ic < cells[0].length; ++ic ) {
				bak.append( ic > 0 ? "\t"
						: "" ).append( cells[ir][ic].toString() );
			}
			bak.append( "\r\n" );
		}
		return bak.toString();
	}
}

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...