Skip to main content
summaryrefslogtreecommitdiffstats
blob: f423136d9b6e9633a5af2ec45b2e3f226ed277e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*******************************************************************************
 * Copyright (c) 2004, 2011 Willian Mitsuda and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Willian Mitsuda - initial API and implementation
 *     Tasktop Technologies - improvements
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.editors;

import java.util.Locale;

import com.ibm.icu.text.DecimalFormat;
import com.ibm.icu.text.NumberFormat;

/**
 * Format attachment size values originally in bytes to nice messages.
 * <p>
 * This formatter tries to use the most applicable measure unit based on size magnitude, i.e.:
 * <p>
 * <ul>
 * <li>< 1 KB - byte based: 1 byte, 100 bytes, etc.
 * <li>>=1 KB and < 1 MB - KB based: 2.00 KB, 100.76 KB
 * <li>>=1 MB and < 1 GB - MB based: 1.00 MB, 33.33 MB
 * <li>>=1 GB - GB based: 2.00 GB
 * </ul>
 * <p>
 * This formatter assumes 1 KB == 1024 bytes, <strong>NOT</strong> 1000 bytes.
 * <p>
 * This formatter always uses 2 decimal places.
 * <p>
 * The size is provided as a String, because it will probably come from a attachment attribute. If the value cannot be
 * decoded, for any reason, it returns {@link #UNKNOWN_SIZE}
 * 
 * @author Willian Mitsuda
 * @author Frank Becker
 * @author Steffen Pingel
 */
public class AttachmentSizeFormatter {

	/**
	 * Default value returned by this formatter when the size is unparseable, contain errors, etc.
	 */
	public static final String UNKNOWN_SIZE = "-"; //$NON-NLS-1$

	public final static AttachmentSizeFormatter getInstance() {
		return new AttachmentSizeFormatter();
	}

	private final DecimalFormat decimalFormat;

	public AttachmentSizeFormatter() {
		this(Locale.getDefault());
	}

	public AttachmentSizeFormatter(Locale locale) {
		this.decimalFormat = (DecimalFormat) NumberFormat.getInstance(locale);
	}

	public String format(String sizeInBytes) {
		if (sizeInBytes == null) {
			return UNKNOWN_SIZE;
		}
		try {
			return format(Long.parseLong(sizeInBytes));
		} catch (NumberFormatException e) {
			return UNKNOWN_SIZE;
		}
	}

	public String format(long size) {
		if (size < 0) {
			return UNKNOWN_SIZE;
		}
		if (size < 1024) {
			// format as byte
			if (size == 1) {
				return Messages.AttachmentSizeFormatter_1_byte;
			}
			DecimalFormat fmt = new DecimalFormat(Messages.AttachmentSizeFormatter_0_bytes);
			return fmt.format(size);
		} else if (size >= 1024 && size <= 1048575) {
			// format as KB
			double formattedValue = size / 1024.0;
			decimalFormat.applyPattern(Messages.AttachmentSizeFormatter_0_KB);
			return decimalFormat.format(formattedValue);
		} else if (size >= 1048576 && size <= 1073741823) {
			// format as MB
			double formattedValue = size / 1048576.0;
			decimalFormat.applyPattern(Messages.AttachmentSizeFormatter_0_MB);
			return decimalFormat.format(formattedValue);
		}

		// format as GB
		double formattedValue = size / 1073741824.0;
		decimalFormat.applyPattern(Messages.AttachmentSizeFormatter_0_GB);
		return decimalFormat.format(formattedValue);
	}

}

Back to the top