1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.support.lastchange;
17
18 import org.fourthline.cling.model.types.UnsignedIntegerFourBytes;
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24
25
26
27 public class Event {
28
29 protected List<InstanceID> instanceIDs = new ArrayList<>();
30
31 public Event() {
32 }
33
34 public Event(List<InstanceID> instanceIDs) {
35 this.instanceIDs = instanceIDs;
36 }
37
38 public List<InstanceID> getInstanceIDs() {
39 return instanceIDs;
40 }
41
42 public InstanceID getInstanceID(UnsignedIntegerFourBytes id) {
43 for (InstanceID instanceID : instanceIDs) {
44 if (instanceID.getId().equals(id)) return instanceID;
45 }
46 return null;
47 }
48
49 public void clear() {
50 instanceIDs = new ArrayList<>();
51 }
52
53 public void setEventedValue(UnsignedIntegerFourBytes id, EventedValue ev) {
54 InstanceID instanceID = null;
55 for (InstanceID i : getInstanceIDs()) {
56 if (i.getId().equals(id)) {
57 instanceID = i;
58 }
59 }
60 if (instanceID == null) {
61 instanceID = new InstanceID(id);
62 getInstanceIDs().add(instanceID);
63 }
64
65 Iterator<EventedValue> it = instanceID.getValues().iterator();
66 while (it.hasNext()) {
67 EventedValue existingEv = it.next();
68 if (existingEv.getClass().equals(ev.getClass())) {
69 it.remove();
70 }
71 }
72 instanceID.getValues().add(ev);
73 }
74
75 public <EV extends EventedValue> EV getEventedValue(UnsignedIntegerFourBytes id, Class<EV> type) {
76 for (InstanceID instanceID : getInstanceIDs()) {
77 if (instanceID.getId().equals(id)) {
78 for (EventedValue eventedValue : instanceID.getValues()) {
79 if (eventedValue.getClass().equals(type))
80 return (EV) eventedValue;
81 }
82 }
83 }
84 return null;
85 }
86
87 public boolean hasChanges() {
88 for (InstanceID instanceID : instanceIDs) {
89 if (instanceID.getValues().size() > 0) return true;
90 }
91 return false;
92 }
93
94 }