]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/impl/EventCollection.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / impl / EventCollection.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.accessor.impl;
13
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.List;\r
17 \r
18 import org.simantics.databoard.accessor.Accessor.Listener;\r
19 import org.simantics.databoard.accessor.event.Event;\r
20 import org.simantics.databoard.accessor.event.ModificationEvent;\r
21
22 /**
23  * Event Collection is a {@link Listener} implementation that collects events.
24  *
25  * @see ModificationEvent
26  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
27  */
28 public class EventCollection implements Listener {
29
30         public List<Event> events = new ArrayList<Event>();
31         
32         public synchronized void addEvent(Event event) {\r
33                 this.events.add(event);\r
34         }\r
35                 \r
36         public synchronized void addEvents(Collection<Event> events) {\r
37                 this.events.addAll(events);\r
38         }\r
39                 \r
40         /**
41          * Returns events and clears change set.
42          * 
43          * @return the list of events
44          */
45         public synchronized List<Event> getAndClearEvents() {
46                 List<Event> result = events;
47                 events = new ArrayList<Event>();
48                 return result;
49         }
50         
51         public synchronized boolean isEmpty() {
52                 return events.isEmpty();
53         }
54         
55         /**
56          * Get a snapshot of events.
57          * 
58          * @return a copy of events
59          */
60         public synchronized List<Event> getEvents() {
61                 return new ArrayList<Event>( events );
62         }
63
64         @Override
65         public void onEvents(Collection<Event> events) {
66                 addEvents(events);
67         }
68         
69         @Override
70         public synchronized String toString() {
71                 StringBuilder sb = new StringBuilder();
72                 sb.append('[');
73                 int len = events.size();
74                 if (len>1) sb.append("\n ");
75                 for (int i=0; i<len; i++) {
76                         Event e = events.get(i);
77                         if (i>0) sb.append("\n ");
78                         sb.append(e.toString());
79                 }
80                 if (len>1) sb.append('\n');
81                 sb.append(']');
82                 return sb.toString();
83         }
84
85         public synchronized void clear() {
86                 events.clear();
87         }
88         
89 }
90