]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/impl/ChangeSet.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / impl / ChangeSet.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.LinkedList;
17 import java.util.List;
18
19 import org.simantics.databoard.accessor.Accessor;
20 import org.simantics.databoard.accessor.event.Event;
21 import org.simantics.databoard.accessor.event.ModificationEvent;
22
23 /**
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.
28  *
29  * @see ModificationEvent
30  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
31  */
32 public class ChangeSet implements Accessor.Listener {
33
34         public List<Event> events = new LinkedList<Event>();
35         
36         public synchronized void addEvent(Event event) {
37                 if (event instanceof ModificationEvent == false) return; 
38                 this.events.add(event);
39         }
40
41         public synchronized void addEvents(Collection<Event> events) {
42                 for (Event e : events) {
43                         if (e instanceof ModificationEvent == false) return; 
44                         this.events.addAll(events);
45                 }
46         }
47                 
48         /**
49          * Returns events and clears change set.
50          * 
51          * @return the list of events
52          */
53         public synchronized List<Event> getAndClearEvents() {
54                 List<Event> result = events;
55                 events = new ArrayList<Event>();
56                 return result;
57         }
58         
59         public synchronized boolean isEmpty() {
60                 return events.isEmpty();
61         }
62         
63         /**
64          * Get a snapshot of events.
65          * 
66          * @return a copy of events
67          */
68         public synchronized List<Event> getEvents() {
69                 return new ArrayList<Event>( events );
70         }
71
72         @Override
73         public void onEvents(Collection<Event> events) {
74                 addEvents(events);
75         }
76         
77         @Override
78         public synchronized String toString() {
79                 StringBuilder sb = new StringBuilder();
80                 sb.append('[');
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());
87                 }
88                 if (len>1) sb.append('\n');
89                 sb.append(']');
90                 return sb.toString();
91         }
92
93         public synchronized void clear() {
94                 events.clear();
95         }
96         
97 }
98