KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tanesha > replacer > formatters > Percent


1 /*
2  * libreplacer, Java library to support C-sprintf alike text formatting
3  * Copyright (C) 2002 Tanesha FTPD Project, www.tanesha.net
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 /*
20  * $Id: Percent.java,v 1.4 2002/12/12 13:06:48 flower Exp $
21  */

22 package org.tanesha.replacer.formatters;
23
24 // project imports
25
import org.tanesha.replacer.ReplacerFormat;
26
27 /**
28  * Stringformatter helper to format percents.
29  * <p>
30  * It can be used to create output like;
31  * <code><pre>######----</pre></code>
32  *
33  * @author Soren, www.tanesha.net
34  */

35 public class Percent implements StringFormatter {
36
37     private float _per;
38     private char _a, _b;
39
40     /**
41      * Creates a new percent formatter.
42      *
43      * @param per the percent value, eg, 0.20 = 20%
44      * @param a the active char, used for the active part of the percent bar, eg. '#'
45      * @param b the inactive char, used for the inactive part of the percent bar, eg '-'
46      */

47     public Percent(float per, char a, char b) {
48         _per = per;
49         _a = a;
50         _b = b;
51     }
52
53     // Implements StringFormatter
54
public String JavaDoc format(boolean align, int p, int q) {
55
56         if (p == ReplacerFormat.UNDEF)
57             return "<no width specified>";
58
59         int gw = Math.round(_per * p);
60
61         StringBuffer JavaDoc o = new StringBuffer JavaDoc();
62
63         for (int i = 0; i < gw; i++)
64             o.append(_a);
65         for (int i = gw; i < p; i++)
66             o.append(_b);
67
68         return o.toString();
69     }
70
71 }
72
Popular Tags