DescriptorUtils { public static ClassDescriptor findIntersectionType(Collection<ClassDescriptor> classes) throws MetaDataException { List<ClassDescriptor> commonTypes = findCommonClasses(classes); ClassDescriptor commonSuperType = commonTypes.get(0); boolean isLineage = true; Set<ClassDescriptor> copyOfClasses = new HashSet<ClassDescriptor>(classes); ClassDescriptor lastCommonType = commonSuperType; while (isLineage) { copyOfClasses.remove(lastCommonType); if (copyOfClasses.isEmpty()) { break; } ClassDescriptor nextCommonSuperType = findSumType(copyOfClasses); isLineage = nextCommonSuperType != lastCommonType; lastCommonType = nextCommonSuperType; if (!(classes.contains(lastCommonType) || lastCommonType.getAllSuperDescriptors().contains(commonSuperType))) { throw new MetaDataException("Disjoint set."); } } if (isLineage) { ClassDescriptor mostSpecific = sortClassesBySpecificity(classes).get(0); return mostSpecific; } else { return lastCommonType; } } private  DescriptorUtils(); static ClassDescriptor findSumType(Collection<ClassDescriptor> classes); static ClassDescriptor findIntersectionType(Collection<ClassDescriptor> classes); static List<ClassDescriptor> findCommonClasses(Collection<ClassDescriptor> classes);  }
@Test public void testMostSpecificType() throws MetaDataException { withClasses("Employable", "Employee", "Manager"); String common = DescriptorUtils.findIntersectionType(classes).getUnqualifiedName(); assertEquals(common, "Manager"); }
@Test public void testExtendedFamilySpecificType() throws MetaDataException { withClasses("HasAddress", "Employee", "Manager", "Company"); String common = DescriptorUtils.findIntersectionType(classes).getUnqualifiedName(); assertEquals("HasAddress", common); }
@Test public void testCousinsSpecificType() { withClasses("Employee", "Manager", "Address"); try { String common = DescriptorUtils.findIntersectionType(classes).getUnqualifiedName(); fail("Expected an exception. Got " + common); } catch (MetaDataException e) { } }
@Test public void testClansSpecificType() { withClasses("Employee", "Manager", "HasAddress", "Address"); try { String common = DescriptorUtils.findIntersectionType(classes).getUnqualifiedName(); fail("Expected an exception. Got " + common); } catch (MetaDataException e) { } }
@Test public void testBranchingTreeSpecificType() throws MetaDataException { withClasses("Thing", "Employable", "Employee", "Contractor"); String common = DescriptorUtils.findIntersectionType(classes).getUnqualifiedName(); assertEquals("Employable", common); }
@Test public void testBranchingTreeMissingSpecificType() throws MetaDataException { withClasses("Thing", "Employee", "Contractor"); String common = DescriptorUtils.findIntersectionType(classes).getUnqualifiedName(); assertEquals("Employable", common); }