]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateList.java
Process changes in smaller chunks
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateList.java
1 package org.simantics.interop.update.model;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.List;
7
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.Statement;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.interop.test.GraphChanges;
13 import org.simantics.interop.test.GraphChanges.Modification;
14 import org.simantics.utils.datastructures.Pair;
15
16 public class UpdateList {
17         private List<PropertyChange> changes;
18         
19         public UpdateList() {
20                 changes = new ArrayList<>();
21         }
22         
23         public UpdateList(Collection<PropertyChange> changes) {
24                 this.changes = new ArrayList<>(changes);
25         }
26         
27         public UpdateList(GraphChanges graphChanges, Collection<Modification> coll) {
28                 this.changes = new ArrayList<>();
29                 for (Modification p : coll) {
30                         addChange(create(graphChanges, p.getLeftSub(),p.getRightSub(),new Pair<Statement, Statement>(p.getLeftStm(), p.getRightStm())));
31                 }
32         }
33         
34         protected PropertyChange create(GraphChanges changes, Resource left, Resource right, Pair<Statement, Statement> change) {
35                 return new PropertyChange(changes, left,right, change);
36         }
37         
38         public List<PropertyChange> getChanges() {
39                 return changes;
40         }
41
42         public void addChange(PropertyChange change) {
43             if (!changes.contains(change))
44                 changes.add(change);
45         }
46         
47         public void removeChange(PropertyChange change) {
48                 changes.remove(change);
49         }
50         
51         public Collection<PropertyChange> getSelected() {
52                 HashSet<PropertyChange> selected = new HashSet<>();
53                 for (PropertyChange c : selected) {
54                         if (c.selected())
55                                 selected.add(c);
56                 }
57                 return selected;
58         }
59         
60         public void clearSelected() {
61                 for (PropertyChange c : changes)
62                         c.select(false);
63         }
64         
65         public PropertyChange getChange(ReadGraph g, Statement s) throws DatabaseException {
66                 for (PropertyChange c : changes) {
67                         if (s.equals(c.pair.first))
68                                 return c;
69                         if (s.equals(c.pair.second))
70                                 return c;
71                 }
72                 return null;
73         }
74         
75         public Collection<PropertyChange> getChanges(ReadGraph g, Resource r) throws DatabaseException{
76                 List<PropertyChange> list = new ArrayList<>();
77                 for (PropertyChange pair : changes) {
78                         if (pair.getFirst() != null) {
79                                 if (pair.getFirst().getSubject().equals(r)) {
80                                         list.add(pair);
81                                         continue;
82                                 }
83                                 if (pair.getFirst().getObject().equals(r)) {
84                                         list.add(pair);
85                                         continue;
86                                 }
87                         }
88                         if (pair.getSecond() != null) {
89                                 if (pair.getSecond().getSubject().equals(r)) {
90                                         list.add(pair);
91                                         continue;
92                                 }
93                                 if (pair.getSecond().getObject().equals(r)) {
94                                         list.add(pair);
95                                         continue;
96                                 }
97                         }
98                 }
99                 return list;
100         }
101 }