1 | package schedframe.resources.units; |
---|
2 | |
---|
3 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
4 | import test.rewolucja.resources.UnitState; |
---|
5 | |
---|
6 | /** |
---|
7 | * |
---|
8 | * @author Marcin Krystek |
---|
9 | * |
---|
10 | */ |
---|
11 | public class Memory extends ResourceUnit/* implements MemoryProvisioner*/{ |
---|
12 | |
---|
13 | protected int total; |
---|
14 | protected int used; |
---|
15 | |
---|
16 | protected int pending; |
---|
17 | protected Memory parentMemory; |
---|
18 | protected UnitState state; |
---|
19 | |
---|
20 | public Memory(Memory m, int total, int used) { |
---|
21 | super(m); |
---|
22 | this.total = total; |
---|
23 | this.used = used; |
---|
24 | this.pending = 0; |
---|
25 | this.parentMemory = m; |
---|
26 | this.state = UnitState.FREE; |
---|
27 | } |
---|
28 | |
---|
29 | public Memory(Memory m) { |
---|
30 | super(m); |
---|
31 | this.total = m.total; |
---|
32 | this.used = m.used; |
---|
33 | this.pending = m.pending; |
---|
34 | this.state = UnitState.FREE; |
---|
35 | } |
---|
36 | |
---|
37 | public Memory(int total, int used) { |
---|
38 | super(ResourceParameterName.MEMORY); |
---|
39 | this.total = total; |
---|
40 | this.used = used; |
---|
41 | this.pending = 0; |
---|
42 | |
---|
43 | } |
---|
44 | |
---|
45 | public Memory(String resId, int total, int used) { |
---|
46 | super(resId, ResourceParameterName.MEMORY); |
---|
47 | this.total = total; |
---|
48 | this.used = used; |
---|
49 | this.pending = 0; |
---|
50 | } |
---|
51 | |
---|
52 | public int getFreeAmount() { |
---|
53 | return this.total - this.used - this.pending; |
---|
54 | } |
---|
55 | |
---|
56 | public int getUsedAmount(){ |
---|
57 | return this.used; |
---|
58 | } |
---|
59 | |
---|
60 | public void setUsedAmount(int amount){ |
---|
61 | if(amount > this.total){ |
---|
62 | throw new IllegalArgumentException( |
---|
63 | "Used amount can not be grather then total amount."); |
---|
64 | } |
---|
65 | this.used = amount; |
---|
66 | } |
---|
67 | |
---|
68 | public int getAmount(){ |
---|
69 | return this.total; |
---|
70 | } |
---|
71 | |
---|
72 | public ResourceUnit toDiscrete() throws ClassNotFoundException { |
---|
73 | throw new ClassNotFoundException("There is no distinguish class version for " |
---|
74 | + getClass().getName()); |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Comparing two Memory objects is equivalent to |
---|
79 | * comparing the free amount of the resource. See {@link #getAmount()} |
---|
80 | */ |
---|
81 | public int compareTo(ResourceUnit o) { |
---|
82 | if(o instanceof Memory){ |
---|
83 | int free = total - used; |
---|
84 | if(free < o.getFreeAmount()) return -1; |
---|
85 | if(free > o.getFreeAmount()) return 1; |
---|
86 | return 0; |
---|
87 | } else { |
---|
88 | throw new IllegalArgumentException(o + " is not an instance of " + |
---|
89 | "memory resource unit."); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | public boolean equals(Object o){ |
---|
94 | if(o instanceof Memory){ |
---|
95 | return super.equals(o); |
---|
96 | } else { |
---|
97 | return false; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | public int getPending() { |
---|
102 | return pending; |
---|
103 | } |
---|
104 | |
---|
105 | public void setPending(int pending) { |
---|
106 | this.pending = pending; |
---|
107 | } |
---|
108 | |
---|
109 | public UnitState getState() { |
---|
110 | return state; |
---|
111 | } |
---|
112 | |
---|
113 | public void setState(UnitState newState) { |
---|
114 | if(newState == UnitState.FREE){ |
---|
115 | parentMemory.setUsedAmount(parentMemory.getUsedAmount()-getAmount()); |
---|
116 | } else if(newState == UnitState.PENDING){ |
---|
117 | parentMemory.setPending(parentMemory.getPending() + getAmount()); |
---|
118 | state = newState; |
---|
119 | } else if(newState == UnitState.BUSY){ |
---|
120 | parentMemory.setPending(parentMemory.getPending()-getAmount()); |
---|
121 | parentMemory.setUsedAmount(parentMemory.getUsedAmount()+getAmount()); |
---|
122 | } |
---|
123 | state = newState; |
---|
124 | } |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | |
---|