1 package uk.ac.ebi.intenz.domain.history;
2
3 import uk.ac.ebi.intenz.domain.constants.EventConstant;
4
5 import java.util.*;
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 public class HistoryGraph {
23
24
25
26 private HistoryNode rootNode;
27
28
29
30
31
32
33
34 public HistoryGraph (HistoryNode rootNode) {
35 if ( rootNode == null ) throw new NullPointerException("Parameter 'rootNode' must not be null.");
36 this.rootNode = rootNode;
37 }
38
39
40
41
42
43
44
45
46
47
48
49 public boolean isDeletedRootNode () {
50 return this.getLatestHistoryEventOfRoot().getEventClass().equals(EventConstant.DELETION)&&
51 this.getLatestHistoryEventOfRoot().getBeforeNode() != null &&
52 this.getLatestHistoryEventOfRoot().getBeforeNode().equals(rootNode);
53 }
54
55
56
57
58
59
60
61
62
63
64
65 public boolean isTransferredRootNode () {
66 return this.getLatestHistoryEventOfRoot().getEventClass().equals(EventConstant.TRANSFER) &&
67 this.getLatestHistoryEventOfRoot().getBeforeNode() != null &&
68 this.getLatestHistoryEventOfRoot().getBeforeNode().equals(rootNode);
69 }
70
71
72
73
74
75 public HistoryNode getRootNode () {
76 return rootNode;
77 }
78
79 public SortedSet<HistoryEvent> getEdges () {
80 return retrieveEdges(rootNode, new HashSet<HistoryNode>());
81 }
82
83
84
85
86
87
88 public HistoryEvent getLatestHistoryEventOfAll () {
89 SortedSet<HistoryEvent> edges = retrieveEdges(rootNode, new HashSet<HistoryNode>());
90 if ( edges.size() == 0 )
91 return new HistoryEvent();
92 return edges.last();
93 }
94
95
96
97
98
99
100 public HistoryEvent getLatestHistoryEventOfRoot () {
101 SortedSet<HistoryEvent> edges = new TreeSet<HistoryEvent>(rootNode.getEdges());
102 if ( edges.size() == 0 )
103 return new HistoryEvent();
104 return edges.last();
105 }
106
107
108
109
110
111
112 public HistoryEvent getLatestRelevantHistoryEventOfRoot () {
113 SortedSet<HistoryEvent> edges = new TreeSet<HistoryEvent>(rootNode.getEdges());
114 if ( edges.size() == 0 )
115 return new HistoryEvent();
116 List<HistoryEvent> list = new ArrayList<HistoryEvent>(edges);
117
118 for ( int iii = list.size()-1; iii > -1; iii-- ) {
119 HistoryEvent event = list.get(iii);
120
121 if ( event.getEventClass().equals(EventConstant.TRANSFER) ){
122 if ( isTransferredRootNode(event) ) {
123 return event;
124 }
125 continue;
126 }
127
128
129
130
131
132
133
134 return event;
135 }
136 return new HistoryEvent();
137 }
138
139
140
141
142
143
144
145
146
147 private SortedSet<HistoryEvent> retrieveEdges (HistoryNode node, Set<HistoryNode> visitedEdges) {
148 assert node != null;
149 visitedEdges.add(node);
150 SortedSet<HistoryEvent> edges = new TreeSet<HistoryEvent>();
151
152 List<HistoryEvent> currentEdges = node.getEdges();
153 for (Iterator<HistoryEvent> it = currentEdges.iterator(); it.hasNext();) {
154 HistoryEvent event = it.next();
155 HistoryNode relative = event.getRelative(node);
156
157 if ( relative != null ) {
158 if ( visitedEdges.contains(relative) ) return edges;
159 edges.addAll(retrieveEdges(relative, visitedEdges));
160 }
161
162 edges.add(event);
163 }
164
165 return edges;
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179 private boolean isTransferredRootNode (HistoryEvent event) {
180 return event.getEventClass().equals(EventConstant.TRANSFER) &&
181 event.getBeforeNode() != null &&
182 event.getBeforeNode().equals(rootNode);
183 }
184
185 private boolean isModifiedRootNode (HistoryEvent event) {
186 return event.getEventClass().equals(EventConstant.MODIFICATION) &&
187 event.getBeforeNode() != null &&
188 event.getAfterNode().equals(rootNode);
189 }
190
191 static class HistoryGraphGrid {
192
193 private TreeMap<List<Integer>, Object> elements;
194 private TreeSet<Integer> xCoordinates;
195 private TreeSet<Integer> yCoordinates;
196
197 public HistoryGraphGrid () {
198 elements = new TreeMap<List<Integer>, Object>(new Comparator<List<Integer>>() {
199 public int compare (List<Integer> al1, List<Integer> al2) {
200 int x1 = al1.get(0).intValue();
201 int y1 = al1.get(1).intValue();
202
203 int x2 = al2.get(0).intValue();
204 int y2 = al2.get(1).intValue();
205
206 if ( x1 < x2 ) return -1;
207 if ( x1 > x2 ) return 1;
208 if ( y1 < y2 ) return -1;
209 if ( y1 > y2 ) return 1;
210
211 return 0;
212 }
213 });
214 xCoordinates = new TreeSet<Integer>();
215 yCoordinates = new TreeSet<Integer>();
216 }
217
218 public TreeMap<List<Integer>, Object> getElements () {
219 return elements;
220 }
221
222 public boolean contains (int x, int y) {
223 List<Integer> coordinates = new ArrayList<Integer>();
224 coordinates.add(new Integer(x));
225 coordinates.add(new Integer(y));
226 return elements.containsKey(coordinates);
227 }
228
229 public void put (int x, int y, Object value) {
230 List<Integer> coordinates = new ArrayList<Integer>();
231 coordinates.add(new Integer(x));
232 coordinates.add(new Integer(y));
233 elements.put(coordinates, value);
234 xCoordinates.add(new Integer(x));
235 yCoordinates.add(new Integer(y));
236 }
237
238 public Object getValue (int x, int y) {
239 List<Integer> coordinates = new ArrayList<Integer>();
240 coordinates.add(new Integer(x));
241 coordinates.add(new Integer(y));
242 return elements.get(coordinates);
243 }
244
245 public void remove (int x, int y) {
246 List<Integer> coordinates = new ArrayList<Integer>();
247 coordinates.add(new Integer(x));
248 coordinates.add(new Integer(y));
249 elements.remove(coordinates);
250 xCoordinates.remove(new Integer(x));
251 yCoordinates.remove(new Integer(y));
252 }
253
254 public int getMinX () {
255 return xCoordinates.first().intValue();
256 }
257
258 public int getMaxX () {
259 return xCoordinates.last().intValue();
260 }
261
262 public int getMinY () {
263 return yCoordinates.first().intValue();
264 }
265
266 public int getMaxY () {
267 return yCoordinates.last().intValue();
268 }
269
270 }
271
272 }