1 | package de.uka.ipd.sdq.scheduler.priority.impl; |
2 | |
3 | import de.uka.ipd.sdq.scheduler.priority.IPriority; |
4 | import de.uka.ipd.sdq.scheduler.priority.IPriorityManager; |
5 | |
6 | public class PriorityImpl implements IPriority { |
7 | |
8 | protected PriorityManagerImpl manager; |
9 | protected int value; |
10 | protected int absolute_value; |
11 | |
12 | |
13 | protected PriorityImpl(int value, PriorityManagerImpl manager) { |
14 | this.manager = manager; |
15 | this.setValue(value); |
16 | } |
17 | |
18 | |
19 | |
20 | public int absoluteValue() { |
21 | return absolute_value; |
22 | } |
23 | |
24 | |
25 | public IPriorityManager getManager() { |
26 | return manager; |
27 | } |
28 | |
29 | |
30 | public boolean greaterThan(IPriority prio) { |
31 | return manager.direction > 0 ? this.getValue() > prio.getValue() : this.getValue() < prio.getValue(); |
32 | } |
33 | |
34 | |
35 | public boolean lessOrEqual(IPriority prio) { |
36 | return manager.direction > 0 ? this.getValue() <= prio.getValue() : this.getValue() >= prio.getValue(); |
37 | } |
38 | |
39 | |
40 | public IPriority increase() { |
41 | return new PriorityImpl(value + manager.direction, manager); |
42 | } |
43 | |
44 | |
45 | public IPriority decrease() { |
46 | return new PriorityImpl(value - manager.direction, manager); } |
47 | |
48 | |
49 | |
50 | public int distance(IPriority prio) { |
51 | return Math.abs(this.getValue() - prio.getValue()); |
52 | } |
53 | |
54 | |
55 | public int getValue() { |
56 | return value; |
57 | } |
58 | |
59 | |
60 | public void setValue(int value) { |
61 | this.value = value; |
62 | this.absolute_value = Math.abs(value - manager.lowest_value); |
63 | } |
64 | |
65 | |
66 | public void setTo(IPriority priority) { |
67 | this.setValue(priority.getValue()); |
68 | } |
69 | |
70 | |
71 | public IPriority addBonus(int bonus) { |
72 | bonus *= manager.direction; |
73 | int new_value = this.getValue() + bonus; |
74 | if (manager.direction > 0){ |
75 | new_value = Math.max(new_value, manager.lowest_value); |
76 | new_value = Math.min(new_value, manager.highest_value); |
77 | } else { |
78 | new_value = Math.min(new_value, manager.lowest_value); |
79 | new_value = Math.max(new_value, manager.highest_value); |
80 | } |
81 | return new PriorityImpl(new_value,manager); |
82 | } |
83 | |
84 | @Override |
85 | public int hashCode() { |
86 | return absolute_value; |
87 | } |
88 | |
89 | @Override |
90 | public boolean equals(Object obj) { |
91 | if (obj instanceof PriorityImpl) { |
92 | PriorityImpl prio = (PriorityImpl) obj; |
93 | return prio.value == this.value; |
94 | } |
95 | return false; |
96 | } |
97 | |
98 | @Override |
99 | public IPriority clone() { |
100 | return new PriorityImpl(this.value,this.manager); |
101 | } |
102 | |
103 | @Override |
104 | public String toString() { |
105 | return ""+value; |
106 | } |
107 | } |