KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > datatypes > processors > FormatFileSize


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.datatypes.processors;
11
12 import org.mmbase.bridge.*;
13
14 /**
15  * A processor that gets a number as a file-size, that is, rounded with kbytes and Mb's and so on.
16  *
17  * @author Michiel Meeuwissen
18  * @version $Id: FormatFileSize.java,v 1.1 2005/12/07 19:44:49 michiel Exp $
19  * @since MMBase-1.8
20  */

21
22 public class FormatFileSize implements Processor {
23
24     private static final long serialVersionUID = 1L;
25     private static int KILO = 1000;
26     private static int MEGA = 1000000;
27     private static int GIGA = 1000000000;
28     private static int KBYTE = 1024;
29     private static int MBYTE = KBYTE * KBYTE;
30     private static int GBYTE = KBYTE * KBYTE * KBYTE;
31
32
33     public final Object JavaDoc process(Node node, Field field, Object JavaDoc value) {
34         int size = node.getIntValue(field.getName());
35         if (size < 9 * KILO) {
36             return "" + size + " byte";
37         } else if (size < 9 * MEGA) {
38             return "" + size / KBYTE + " kbyte";
39         } else if (size < 9 * GIGA) {
40             return "" + size / MBYTE + " Mbyte";
41         } else {
42             return "" + size / GBYTE + " Gbyte";
43         }
44     }
45
46     public String JavaDoc toString() {
47         return "FILESIZE";
48     }
49 }
50
51
52
Popular Tags