View Javadoc

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