1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.accessor.impl;
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.LinkedList;
\r
17 import java.util.List;
\r
19 import org.simantics.databoard.accessor.Accessor;
\r
20 import org.simantics.databoard.accessor.event.Event;
\r
21 import org.simantics.databoard.accessor.event.ModificationEvent;
\r
24 * ChangeSet is a collection of a modification events. ChangeSet can be applied
25 * to an accessor, or captured from an accessor. To capture a change set
26 * add the ChangeSet instance as a listener to the accessor. ChangeSet does not
27 * capture non-modification events.
29 * @see ModificationEvent
30 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
32 public class ChangeSet implements Accessor.Listener {
34 public List<Event> events = new LinkedList<Event>();
36 public synchronized void addEvent(Event event) {
\r
37 if (event instanceof ModificationEvent == false) return;
\r
38 this.events.add(event);
\r
41 public synchronized void addEvents(Collection<Event> events) {
\r
42 for (Event e : events) {
\r
43 if (e instanceof ModificationEvent == false) return;
\r
44 this.events.addAll(events);
\r
49 * Returns events and clears change set.
51 * @return the list of events
53 public synchronized List<Event> getAndClearEvents() {
54 List<Event> result = events;
55 events = new ArrayList<Event>();
59 public synchronized boolean isEmpty() {
60 return events.isEmpty();
64 * Get a snapshot of events.
66 * @return a copy of events
68 public synchronized List<Event> getEvents() {
69 return new ArrayList<Event>( events );
73 public void onEvents(Collection<Event> events) {
78 public synchronized String toString() {
79 StringBuilder sb = new StringBuilder();
81 int len = events.size();
82 if (len>1) sb.append("\n ");
83 for (int i=0; i<len; i++) {
84 Event e = events.get(i);
85 if (i>0) sb.append("\n ");
86 sb.append(e.toString());
88 if (len>1) sb.append('\n');
93 public synchronized void clear() {