1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.accessor.impl;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
18 import org.simantics.databoard.accessor.Accessor.Listener;
19 import org.simantics.databoard.accessor.event.Event;
20 import org.simantics.databoard.accessor.event.ModificationEvent;
23 * Event Collection is a {@link Listener} implementation that collects events.
25 * @see ModificationEvent
26 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
28 public class EventCollection implements Listener {
30 public List<Event> events = new ArrayList<Event>();
32 public synchronized void addEvent(Event event) {
33 this.events.add(event);
36 public synchronized void addEvents(Collection<Event> events) {
37 this.events.addAll(events);
41 * Returns events and clears change set.
43 * @return the list of events
45 public synchronized List<Event> getAndClearEvents() {
46 List<Event> result = events;
47 events = new ArrayList<Event>();
51 public synchronized boolean isEmpty() {
52 return events.isEmpty();
56 * Get a snapshot of events.
58 * @return a copy of events
60 public synchronized List<Event> getEvents() {
61 return new ArrayList<Event>( events );
65 public void onEvents(Collection<Event> events) {
70 public synchronized String toString() {
71 StringBuilder sb = new StringBuilder();
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());
80 if (len>1) sb.append('\n');
85 public synchronized void clear() {