View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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   * @author Christian Bauer
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  }