KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > web > format > ByteSizeFormat


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: ByteSizeFormat.java,v $
31  * Revision 1.3 2005/04/11 16:54:15 colinmacleod
32  * Changed HTMLFormat to an interface (rather
33  * than an abstract class.)
34  *
35  * Revision 1.2 2005/04/09 17:19:59 colinmacleod
36  * Changed copyright text to GPL v2 explicitly.
37  *
38  * Revision 1.1.1.1 2005/03/10 17:51:35 colinmacleod
39  * Restructured ivata op around Hibernate/PicoContainer.
40  * Renamed ivata groupware.
41  *
42  * Revision 1.1 2004/09/30 15:16:01 colinmacleod
43  * Split off addressbook elements into security subproject.
44  *
45  * Revision 1.3 2004/03/21 21:16:08 colinmacleod
46  * Shortened name to ivata op.
47  *
48  * Revision 1.2 2004/02/01 22:00:33 colinmacleod
49  * Added full names to author tags
50  *
51  * Revision 1.1.1.1 2004/01/27 20:57:55 colinmacleod
52  * Moved ivata openportal to SourceForge..
53  *
54  * Revision 1.1.1.1 2003/10/13 20:50:09 colin
55  * Restructured portal into subprojects
56  *
57  * Revision 1.2 2003/06/19 07:46:41 jano
58  * fixing bug with int size
59  *
60  * Revision 1.1 2003/02/24 19:33:32 colin
61  * moved to jsp
62  *
63  * Revision 1.2 2003/02/04 17:43:46 colin
64  * copyright notice
65  *
66  * Revision 1.1 2002/09/18 16:34:46 colin
67  * first version used in email list
68  * -----------------------------------------------------------------------------
69  */

70 package com.ivata.groupware.web.format;
71
72 import com.ivata.mask.util.StringHandling;
73 import com.ivata.mask.web.format.HTMLFormat;
74
75
76 /**
77  * <p>Format the size of a file or other object as bytes, kb, or Mb
78  * depending if the number is less than a kilobyte, or less than a
79  * megabyte in size..</p>
80  *
81  * @since 2002-09-18
82  * @author Colin MacLeod
83  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
84  * @version $Revision: 1.3 $
85  */

86 public class ByteSizeFormat implements HTMLFormat {
87     /**
88      * <p>The size under which the number appears just as it is, a raw
89      * number. If the numer is greater than this size (but less than
90      * <code>megaSize</code>) it will be displayed as a multiple of this
91      * number.</p>
92      */

93     static final int kiloSize = 1024;
94     /**
95      * <p>The size above which the number appears as a multiple of this
96      * number, a multiple of <i>megabytes</i>.</p>
97      */

98     static final int megaSize = 1048576;
99     /**
100      * <p>The number of decimal places to show for <i>kilobyte</i> or
101      * <i>megabyte</i> sizes.</p>
102      */

103     private int decimals = 1;
104     /**
105      * <p>String to append to the number if it is expressed in
106      * <i>bytes</i>.</p>
107      */

108     private String JavaDoc byteUnits = "";
109     /**
110      * <p>String to append to the number if it is expressed in
111      * <i>kilobytes</i>.</p>
112      */

113     private String JavaDoc kiloUnits = "k";
114     /**
115      * <p>String to append to the number if it is expressed in
116      * <i>Megabytes</i>.</p>
117      */

118     private String JavaDoc megaUnits = "M";
119     /**
120      * <p>The character to use as a decimal point.</p>
121      */

122     private char decimalCharacter = '.';
123
124     /**
125      * <p>Convert the number the given string represents according to
126      * whether or not the number represents just <i>bytes</i>,
127      * <i>kilobytes</i> or <i>megabytes<i>.</p>
128      *
129      * @param byteTextParam string number to convert.
130      * @return a string representing the size, expressed in the
131      * appropriate units.
132      */

133     public String JavaDoc format(final String JavaDoc byteTextParam) {
134         // first convert the string to an integer
135
Integer JavaDoc bytesInteger = StringHandling.integerValue(byteTextParam);
136         int bytes;
137         String JavaDoc byteText = byteTextParam;
138         if(bytesInteger == null) {
139             bytes = 0;
140         } else {
141             bytes = bytesInteger.intValue();
142         }
143         // is this less than the kilo size?
144
if(bytes < kiloSize) {
145             byteText = bytes + byteUnits;
146         } else {
147             // both the kilo and mega ranges need to do rounding
148
int rounding = 10;
149             for(int exp = 0; exp < decimals; ++exp) {
150                 rounding *= 10;
151             }
152             long newValue = (long) bytes * rounding;
153             String JavaDoc units;
154             // if this is in the kilo range, divide by that amount
155
if(bytes < megaSize) {
156                 newValue /= kiloSize;
157                 units = kiloUnits;
158             } else {
159                 newValue /= megaSize;
160                 units = megaUnits;
161             }
162             // the clever bit - this does the rounding up or down whichever is
163
// nearest
164
newValue += 5;
165             newValue /= 10;
166
167             // now to add the decimal point
168
String JavaDoc noDecimal = Long.toString(newValue);
169             int length = noDecimal.length();
170             byteText = noDecimal.substring(0, length - decimals)
171                 + decimalCharacter
172                 + noDecimal.substring(length - decimals)
173                 + units;
174         }
175         return byteText;
176     }
177
178     /**
179      * <p>The number of decimal places to show for <i>kilobyte</i> or
180      * <i>megabyte</i> sizes.</p>
181      *
182      * @return the current value of decimal places.
183      */

184     public final int getDecimals() {
185         return decimals;
186     }
187
188     /**
189      * <p>The number of decimal places to show for <i>kilobyte</i> or
190      * <i>megabyte</i> sizes.</p>
191      *
192      * @param decimals the new value of decimal places to display.
193      */

194     public final void setDecimals(final int decimals) {
195         this.decimals = decimals;
196     }
197
198     /**
199      * <p>String to append to the number if it is expressed in
200      * <i>bytes</i>.</p>
201      *
202      * @return the current value of byte units.
203      */

204     public final String JavaDoc getByteUnits() {
205         return byteUnits;
206     }
207
208     /**
209      * <p>String to append to the number if it is expressed in
210      * <i>bytes</i>.</p>
211      *
212      * @param byteUnits the new value of units to append to numbers
213      * expressed in bytes.
214      */

215     public final void setByteUnits(final String JavaDoc byteUnits) {
216         this.byteUnits = byteUnits;
217     }
218
219     /**
220      * <p>String to append to the number if it is expressed in
221      * <i>kilobytes</i>.</p>
222      *
223      * @return the current value of kilo units.
224      */

225     public final String JavaDoc getKiloUnits() {
226         return kiloUnits;
227     }
228
229     /**
230      * <p>String to append to the number if it is expressed in
231      * <i>kilobytes</i>.</p>
232      *
233      * @param kiloUnits the new value of units to append to numbers
234      * expressed in kilobytes.
235      */

236     public final void setKiloUnits(final String JavaDoc kiloUnits) {
237         this.kiloUnits = kiloUnits;
238     }
239
240     /**
241      * <p>String to append to the number if it is expressed in
242      * <i>Megabytes</i>.</p>
243      *
244      * @return the current value of mega units.
245      */

246     public final String JavaDoc getMegaUnits() {
247         return megaUnits;
248     }
249
250     /**
251      * <p>String to append to the number if it is expressed in
252      * <i>Megabytes</i>.</p>
253      *
254      * @param megaUnits the new value of units to append to numbers
255      * expressed in megabytes.
256      */

257     public final void setMegaUnits(final String JavaDoc megaUnits) {
258         this.megaUnits = megaUnits;
259     }
260
261     /**
262      * <p>The character to use as a decimal point.</p>
263      *
264      * @return the current value of decimal character.
265      */

266     public final char getDecimalCharacter() {
267         return decimalCharacter;
268     }
269
270     /**
271      * <p>The character to use as a decimal point.</p>
272      *
273      * @param decimalCharacter the new value of decimal character.
274      */

275     public final void setDecimalCharacter(final char decimalCharacter) {
276         this.decimalCharacter = decimalCharacter;
277     }
278 }
279
Popular Tags