1 package org.vaadin.aceeditor.client;
2
3 import java.util.Collections;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import org.vaadin.aceeditor.client.AceAnnotation.MarkerAnnotation;
8 import org.vaadin.aceeditor.client.AceAnnotation.RowAnnotation;
9 import org.vaadin.aceeditor.client.TransportDiff.TransportSetDiffForMarkerAnnotations;
10 import org.vaadin.aceeditor.client.TransportDiff.TransportSetDiffForRowAnnotations;
11 import org.vaadin.aceeditor.client.TransportDoc.TransportMarkerAnnotation;
12 import org.vaadin.aceeditor.client.TransportDoc.TransportRowAnnotation;
13 import org.vaadin.aceeditor.client.TransportDoc.TransportableAs;
14
15 public class SetDiff<V extends TransportableAs<T>,T> {
16
17 private final Set<V> added;
18 private final Set<V> removed;
19
20 public SetDiff(Set<V> added, Set<V> removed) {
21 this.added = added;
22 this.removed = removed;
23 }
24
25 public SetDiff() {
26 added = Collections.emptySet();
27 removed = Collections.emptySet();
28 }
29
30 public static class Differ<V extends TransportableAs<T>,T> {
31 public SetDiff<V,T> diff(Set<V> s1, Set<V> s2) {
32 Set<V> removed = new HashSet<V>(s1);
33 removed.removeAll(s2);
34
35 Set<V> added = new HashSet<V>(s2);
36 added.removeAll(s1);
37 return new SetDiff<V,T>(added, removed);
38 }
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 }
54
55
56 public static SetDiff<RowAnnotation,TransportRowAnnotation> fromTransport(TransportSetDiffForRowAnnotations tsd) {
57 Set<RowAnnotation> added = new HashSet<RowAnnotation>();
58 for (TransportRowAnnotation t : tsd.added) {
59 added.add(t.fromTransport());
60 }
61 Set<RowAnnotation> removed = new HashSet<RowAnnotation>();
62 for (TransportRowAnnotation t : tsd.removed) {
63 removed.add(t.fromTransport());
64 }
65 return new SetDiff<RowAnnotation,TransportRowAnnotation>(added, removed);
66 }
67
68
69 public static SetDiff<MarkerAnnotation,TransportMarkerAnnotation> fromTransport(TransportSetDiffForMarkerAnnotations tsd) {
70 Set<MarkerAnnotation> added = new HashSet<MarkerAnnotation>();
71 for (TransportMarkerAnnotation t : tsd.added) {
72 added.add(t.fromTransport());
73 }
74 Set<MarkerAnnotation> removed = new HashSet<MarkerAnnotation>();
75 for (TransportMarkerAnnotation t : tsd.removed) {
76 removed.add(t.fromTransport());
77 }
78 return new SetDiff<MarkerAnnotation,TransportMarkerAnnotation>(added, removed);
79 }
80
81 public Set<V> applyTo(Set<V> s1) {
82 Set<V> s2 = new HashSet<V>(s1);
83 s2.removeAll(removed);
84 s2.addAll(added);
85 return s2;
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public TransportSetDiffForRowAnnotations asTransportRowAnnotations() {
102 HashSet<TransportRowAnnotation> ta = new HashSet<TransportRowAnnotation>();
103 for (V v : added) {
104 ta.add((TransportRowAnnotation) v.asTransport());
105 }
106 HashSet<TransportRowAnnotation> tr = new HashSet<TransportRowAnnotation>();
107 for (V v : removed) {
108 tr.add((TransportRowAnnotation) v.asTransport());
109 }
110 return new TransportSetDiffForRowAnnotations(ta, tr);
111 }
112
113
114 public TransportSetDiffForMarkerAnnotations asTransportMarkerAnnotations() {
115 HashSet<TransportMarkerAnnotation> ta = new HashSet<TransportMarkerAnnotation>();
116 for (V v : added) {
117 ta.add((TransportMarkerAnnotation) v.asTransport());
118 }
119 HashSet<TransportMarkerAnnotation> tr = new HashSet<TransportMarkerAnnotation>();
120 for (V v : removed) {
121 tr.add((TransportMarkerAnnotation) v.asTransport());
122 }
123 return new TransportSetDiffForMarkerAnnotations(ta, tr);
124 }
125
126 @Override
127 public String toString() {
128 return "added: " + added + ", removed: " + removed;
129 }
130 }