]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop/src/org/simantics/interop/test/NameComparator.java
Use named Comparator return values for most common cases
[simantics/interop.git] / org.simantics.interop / src / org / simantics / interop / test / NameComparator.java
1 package org.simantics.interop.test;
2
3 import org.simantics.databoard.Bindings;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.layer0.Layer0;
8
9 /**
10  * Object comparator that uses type and name of objects to check if objects are comparable.
11  * If objects have no name, compare returns unreliable as result.
12  *  
13  * 
14  * Difference value is amount of properties that have different values.
15  * 
16  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
17  *
18  */
19 public class NameComparator extends TypeComparator {
20         
21         @Override
22         public int compare(ReadGraph g, Resource o1, Resource o2)
23                         throws DatabaseException {
24                 if (!compareType(g,o1, o2)) {
25                         return NO_MATCH;
26                 }
27                 if (hasName(g, o1, o2)) {
28                         if(!compareName(g, o1, o2))
29                                 return NO_MATCH;
30                         return propsDiffCount(g,o1, o2);
31                 }
32                 return UNDECISIVE;
33                 
34         }
35         
36         protected boolean compareName(ReadGraph g, Resource o1, Resource o2) throws DatabaseException {
37                 Layer0 l0 = Layer0.getInstance(g);
38                 String n1 = g.getPossibleRelatedValue(o1, l0.HasName, Bindings.STRING);
39                 String n2 = g.getPossibleRelatedValue(o2, l0.HasName, Bindings.STRING);
40                 if (n1 != null && n2 != null)
41                         return n1.equals(n2);
42                 if (n1 == null && n2 == null)
43                         return true;
44                 return false;
45                         
46         }
47         
48         protected boolean hasName(ReadGraph g, Resource o1, Resource o2) throws DatabaseException {
49                 Layer0 l0 = Layer0.getInstance(g);
50                 String n1 = g.getPossibleRelatedValue(o1, l0.HasName, Bindings.STRING);
51                 String n2 = g.getPossibleRelatedValue(o2, l0.HasName, Bindings.STRING);
52                 if (n1 != null && n2 != null)
53                         return true;
54                 return false;
55                         
56         }
57
58 }