LowestCommonSuperclass { public static BuildableRDFSClazz getLowestCommonSuperclass(Collection<BuildableRDFSClazz> clazzes) throws RepositoryException { if(clazzes.size() == 1) { return clazzes.iterator().next(); } Iterator<BuildableRDFSClazz> clazzIter = clazzes.iterator(); Collection<BuildableRDFSClazz> common = getSuperclassClosure(clazzIter.next()); while(clazzIter.hasNext()) { common.retainAll(getSuperclassClosure(clazzIter.next())); } int maximumDistance = -1; BuildableRDFSClazz lowestCommonSuper = null; for (BuildableRDFSClazz commonSuper : common) { int distance = minimumDistanceFromRoot(commonSuper); if (distance > maximumDistance) { maximumDistance = distance; lowestCommonSuper = commonSuper; } } return lowestCommonSuper; }  static BuildableRDFSClazz getLowestCommonSuperclass(Collection<BuildableRDFSClazz> clazzes);  }
@Test public void testLowestCommonSuperclass() throws Exception { Set<BuildableRDFSClazz> cd = new HashSet<>(); cd.add(c); cd.add(d); assertEquals(a, LowestCommonSuperclass.getLowestCommonSuperclass(cd)); Set<BuildableRDFSClazz> ce = new HashSet<>(); ce.add(c); ce.add(e); assertEquals(r, LowestCommonSuperclass.getLowestCommonSuperclass(ce)); Set<BuildableRDFSClazz> ac = new HashSet<>(); ac.add(a); ac.add(c); assertEquals(a, LowestCommonSuperclass.getLowestCommonSuperclass(ac)); Set<BuildableRDFSClazz> aa = new HashSet<>(); aa.add(a); aa.add(a); assertEquals(a, LowestCommonSuperclass.getLowestCommonSuperclass(aa)); Set<BuildableRDFSClazz> de = new HashSet<>(); de.add(d); de.add(e); assertEquals(b, LowestCommonSuperclass.getLowestCommonSuperclass(de)); }