1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.util;
21
22 /***
23 * Represents the progress of a transfer.
24 */
25 public class Progress implements Comparable
26 {
27
28 private long rate;
29 private long size;
30 private long transferred;
31
32 /***
33 *
34 * @param size
35 * @param transferred
36 *
37 * @throws IllegalArgumentException if transferred is greater then size
38 */
39 public Progress(long size, long transferred)
40 {
41 if (transferred > size) {
42 throw new IllegalArgumentException("transferred must not be greater than size");
43 }
44 this.size = size;
45 this.transferred = transferred;
46 }
47
48 public Progress()
49 {
50 this(-1, -1);
51 }
52
53 /***
54 * Use a higher precission to make a difference between 0 progress and 0.5.
55 */
56 public int compareTo(Object o)
57 {
58 return (int)(getPercent() - ((Progress)o).getPercent());
59 }
60
61 public double getPercent()
62 {
63 return (size <= 0 || transferred <= 0)
64 ? 0 : ((double)(transferred * 100) / size);
65 }
66 /***
67 * Returns the current rate.
68 */
69 public long getRate()
70 {
71 return rate;
72 }
73
74 public void setRate(long rate)
75 {
76 this.rate = rate;
77 }
78
79 public long getSize()
80 {
81 return size;
82 }
83
84 public long getTransferred()
85 {
86 return transferred;
87 }
88
89 }