Sync git svn branch with SVN repository r33249.
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / OperationImpl.java
1 package fi.vtt.simantics.procore.internal;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.Collections;\r
6 import java.util.List;\r
7 \r
8 import org.simantics.db.Operation;\r
9 import org.simantics.db.service.ExternalOperation;\r
10 \r
11 public class OperationImpl implements Operation {\r
12     private final long id;\r
13     private final long csid;\r
14     private Operation combined;\r
15     private final List<ExternalOperation> externals;\r
16     OperationImpl(long id, long csid) {\r
17         this(id, csid, null);\r
18     }\r
19     OperationImpl(long id, long csid, List<ExternalOperation> externals) {\r
20         this.id = id;\r
21         this.csid = csid;\r
22         this.combined = null;\r
23         this.externals = externals.isEmpty() ? null : externals;\r
24     }\r
25     @Override\r
26     public String toString() {\r
27         return "Operation id=" + id + " cs=" + csid + ".";\r
28     }\r
29     @Override\r
30     public long getId() {\r
31         return id;\r
32     }\r
33 \r
34     @Override\r
35     public long getCSId() {\r
36         return csid;\r
37     }\r
38 \r
39     @Override\r
40     public void combine(Operation operation) {\r
41         combined = operation;\r
42     }\r
43 \r
44     @Override\r
45     public List<Operation> getOperations() {\r
46         \r
47         if(combined == null) return Collections.<Operation>singletonList(this);\r
48 \r
49         ArrayList<Operation> result = new ArrayList<Operation>();\r
50         OperationImpl current = this;\r
51         while(current != null) {\r
52                 result.add(current);\r
53                 current = (OperationImpl)(current.combined);\r
54         }\r
55         \r
56         Collections.reverse(result);\r
57         \r
58         return result;\r
59         \r
60     }\r
61     \r
62     @Override\r
63     public Collection<ExternalOperation> getExternalOperations() {\r
64         if(externals == null)\r
65                 return Collections.emptyList();\r
66         else\r
67                 return externals;\r
68     }\r
69     \r
70 }\r