[477] | 1 | package schedframe.resources.units; |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | public class Storage extends AbstractResourceUnit { |
---|
| 5 | |
---|
| 6 | protected int total; |
---|
| 7 | protected int used; |
---|
| 8 | |
---|
| 9 | public Storage(Storage s, int total, int used) { |
---|
| 10 | super(s); |
---|
| 11 | this.total = total; |
---|
| 12 | this.used = used; |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | public Storage (Storage s) { |
---|
| 16 | super(s); |
---|
| 17 | this.total = s.total; |
---|
| 18 | this.used = s.used; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | public Storage(int total, int used) { |
---|
| 22 | super(StandardResourceUnitName.STORAGE); |
---|
| 23 | this.total = total; |
---|
| 24 | this.used = used; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | public Storage (String resId, int total, int used) { |
---|
| 28 | super(StandardResourceUnitName.STORAGE, resId); |
---|
| 29 | this.total = total; |
---|
| 30 | this.used = used; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | public int getFreeAmount() { |
---|
| 34 | return this.total - this.used; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | public int getUsedAmount(){ |
---|
| 38 | return this.used; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | public void setUsedAmount(int amount){ |
---|
| 42 | if(amount > this.total){ |
---|
| 43 | throw new IllegalArgumentException( |
---|
| 44 | "Used amount can not be grather then total amount."); |
---|
| 45 | } |
---|
| 46 | this.used = amount; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | public int getAmount(){ |
---|
| 50 | return this.total; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | public ResourceUnit toDiscrete() throws ClassNotFoundException { |
---|
| 54 | throw new ClassNotFoundException("There is no distinguish class version for " |
---|
| 55 | + getClass().getName()); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * Comparing two Memory objects is equivalent to |
---|
| 60 | * comparing the free amount of the resource. See {@link #getAmount()} |
---|
| 61 | */ |
---|
| 62 | public int compareTo(ResourceUnit o) { |
---|
| 63 | if(o instanceof Memory){ |
---|
| 64 | int free = total - used; |
---|
| 65 | if(free < o.getFreeAmount()) return -1; |
---|
| 66 | if(free > o.getFreeAmount()) return 1; |
---|
| 67 | return 0; |
---|
| 68 | } else { |
---|
| 69 | throw new IllegalArgumentException(o + " is not an instance of " + |
---|
| 70 | "memory resource unit."); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | public boolean equals(Object o){ |
---|
| 75 | if(o instanceof Memory){ |
---|
| 76 | return super.equals(o); |
---|
| 77 | } else { |
---|
| 78 | return false; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | } |
---|