]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/impl/EventCollection.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / impl / EventCollection.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.accessor.impl;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
18 import org.simantics.databoard.accessor.Accessor.Listener;
19 import org.simantics.databoard.accessor.event.Event;
20 import org.simantics.databoard.accessor.event.ModificationEvent;
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) {
33                 this.events.add(event);
34         }
35                 
36         public synchronized void addEvents(Collection<Event> events) {
37                 this.events.addAll(events);
38         }
39                 
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