View Javadoc

1   package org.openphacts.nextprot.util;
2   
3   /**
4    * <code>EchoTool</code> is a simple tool that can be placed in a velocity context to
5    * enable output to the console.
6    *
7    * Created: Wed Jun 13 15:01:33 2012
8    *
9    * @author <a href="mailto:">Oliver Karch</a>
10   * @version 1.0
11   */
12  import java.math.BigDecimal;
13  
14  import org.apache.velocity.tools.generic.SafeConfig;
15  
16  public class EchoTool extends SafeConfig {
17      private Object hashMark;
18      private int maxColumns;
19      private long progressCount;
20  
21      /**
22       * Creates the echo tool
23       */
24      public EchoTool() {
25  	super();
26  	this.hashMark = new String( "#" );
27  	this.maxColumns = 80;
28  	this.progressCount = 0L;
29      } // EchoTool constructor
30      
31      /**
32       * Prints a message to stdout.
33       *
34       * @param x the message object
35       */
36      public void println( Object x ) {
37  	if( x != null )
38  	    System.out.println( x );
39      }
40  
41      private BigDecimal convertNumber( Object x ) {
42  	BigDecimal num = null;
43  	if( x != null ) {
44  	    try {
45  		num = new BigDecimal( x.toString() );
46  	    }
47  	    catch( NumberFormatException nfe ) {
48  		num = null;
49  	    }
50  	}
51  	return num;
52      }
53  
54      private void printHashMark() {
55  	if( (progressCount % (long)maxColumns) == 0 )
56  	    System.out.println();
57  	if( hashMark != null ) 
58  	    System.out.print( hashMark );
59      }
60  
61      /**
62       * Resets the progress meter
63       */
64      public void startProgress() {
65  	this.progressCount = 0L;
66      }
67  
68      /**
69       * Advances the progress meter by a certain amount.
70       *
71       * @param amount the amount of progress.
72       */
73      public void advance( Object amount ) {
74  	BigDecimal amt = convertNumber( amount );
75  	if( amt != null ) {
76  	    progressCount+=amt.longValue();
77  	    printHashMark();
78  	}
79      }
80  
81      /**
82       * Advances the progress meter by a single unit.
83       */
84      public void advance() {
85  	progressCount++;
86  	printHashMark();
87      }
88  
89      /**
90       * Get the HashMark value.
91       * @return the HashMark value.
92       */
93      public Object getMark() {
94  	return hashMark;
95      }
96  
97      /**
98       * Set the HashMark value.
99       * @param newHashMark The new HashMark value.
100      */
101     public void setMark(Object newHashMark) {
102 	this.hashMark = newHashMark;
103     }
104     
105     /**
106      * Get the MaxCols value.
107      * @return the MaxCols value.
108      */
109     public Object getColumns() {
110 	return new Integer(maxColumns);
111     }
112 
113     /**
114      * Set the MaxCols value.
115      * @param newMaxCols The new MaxCols value.
116      */
117     public void setColumns(Object newMaxCols) {
118 	BigDecimal cols = convertNumber( newMaxCols );
119 	if( cols != null ) 
120 	    maxColumns = cols.intValue();
121     }
122     
123 } // EchoTool