1 package cz.cuni.amis.utils.flag;
2
3 import java.io.Serializable;
4
5
6
7
8
9
10
11
12
13
14 public class FlagInteger extends Flag<Integer> implements Serializable {
15
16
17
18
19 public static abstract class DoInSyncWithReason<T, R> extends DoInSync<T> {
20
21
22
23
24
25 protected boolean isImmutable() {
26 return flag instanceof ImmutableFlag;
27 }
28
29
30
31
32 protected void setFlag(T value, R reason) {
33 if (flag instanceof ImmutableFlag) throw new UnsupportedOperationException("trying to set flag of the immutable flag!");
34 flag.value = value;
35 flag.notifier.setValue(value);
36 flag.listeners.notify(flag.notifier);
37 }
38
39 protected T getFlag() {
40 return flag.getFlag();
41 }
42
43
44
45
46 public abstract void execute(T flagValue);
47
48
49
50
51 public abstract void execute(T flagValue, R reason);
52
53 }
54
55 public FlagInteger(Integer initial) {
56 super(initial);
57 }
58
59 public void increment(final int number) {
60 inSync(
61 new DoInSync<Integer>() {
62
63 @Override
64 public void execute(Integer flagValue) {
65 setFlag(flagValue+number);
66 }
67
68 }
69 );
70 }
71
72 public void decrement(final int number) {
73 inSync(
74 new DoInSync<Integer>() {
75
76 @Override
77 public void execute(Integer flagValue) {
78 setFlag(flagValue-number);
79 }
80
81 }
82 );
83 }
84
85 }