@Test public void testAddFewMembers() throws Exception { Group g = createGroup(); g.addMembers(createGroup().getID(), createUser().getID()); assertFalse(getTree(g).hasChild(REP_MEMBERS_LIST)); }
@NotNull Set<String> addMembers(@NotNull Tree groupTree, @NotNull Map<String, String> memberIds) { Tree membersList = groupTree.getChild(UserConstants.REP_MEMBERS_LIST); Iterator<Tree> trees = Iterators.concat( Iterators.singletonIterator(groupTree), membersList.getChildren().iterator() ); Set<String> failed = new HashSet<>(memberIds.size()); int bestCount = membershipSizeThreshold; PropertyState bestProperty = null; Tree bestTree = null; while (trees.hasNext() && !memberIds.isEmpty()) { Tree t = trees.next(); PropertyState refs = t.getProperty(UserConstants.REP_MEMBERS); if (refs != null) { int numRefs = 0; for (String ref : refs.getValue(Type.WEAKREFERENCES)) { String id = memberIds.remove(ref); if (id != null) { failed.add(id); if (memberIds.isEmpty()) { break; } } numRefs++; } if (numRefs < bestCount) { bestCount = numRefs; bestProperty = refs; bestTree = t; } } } if (!memberIds.isEmpty()) { PropertyBuilder<String> propertyBuilder; int propCnt; if (bestProperty == null) { if (!groupTree.hasProperty(UserConstants.REP_MEMBERS)) { bestTree = groupTree; } else { bestTree = createMemberRefTree(groupTree, membersList); } propertyBuilder = PropertyBuilder.array(Type.WEAKREFERENCE, UserConstants.REP_MEMBERS); propCnt = 0; } else { propertyBuilder = PropertyBuilder.copy(Type.WEAKREFERENCE, bestProperty); propCnt = bestCount; } if ((propCnt + memberIds.size()) > membershipSizeThreshold) { while (!memberIds.isEmpty()) { Set<String> s = new HashSet<>(); Iterator<String> it = memberIds.keySet().iterator(); while (propCnt < membershipSizeThreshold && it.hasNext()) { s.add(it.next()); it.remove(); propCnt++; } propertyBuilder.addValues(s); bestTree.setProperty(propertyBuilder.getPropertyState()); if (it.hasNext()) { propCnt = 0; bestTree = createMemberRefTree(groupTree, membersList); propertyBuilder = PropertyBuilder.array(Type.WEAKREFERENCE, UserConstants.REP_MEMBERS); } } } else { propertyBuilder.addValues(memberIds.keySet()); bestTree.setProperty(propertyBuilder.getPropertyState()); } } return failed; }
MembershipWriter { @NotNull Set<String> addMembers(@NotNull Tree groupTree, @NotNull Map<String, String> memberIds) { Tree membersList = groupTree.getChild(UserConstants.REP_MEMBERS_LIST); Iterator<Tree> trees = Iterators.concat( Iterators.singletonIterator(groupTree), membersList.getChildren().iterator() ); Set<String> failed = new HashSet<>(memberIds.size()); int bestCount = membershipSizeThreshold; PropertyState bestProperty = null; Tree bestTree = null; while (trees.hasNext() && !memberIds.isEmpty()) { Tree t = trees.next(); PropertyState refs = t.getProperty(UserConstants.REP_MEMBERS); if (refs != null) { int numRefs = 0; for (String ref : refs.getValue(Type.WEAKREFERENCES)) { String id = memberIds.remove(ref); if (id != null) { failed.add(id); if (memberIds.isEmpty()) { break; } } numRefs++; } if (numRefs < bestCount) { bestCount = numRefs; bestProperty = refs; bestTree = t; } } } if (!memberIds.isEmpty()) { PropertyBuilder<String> propertyBuilder; int propCnt; if (bestProperty == null) { if (!groupTree.hasProperty(UserConstants.REP_MEMBERS)) { bestTree = groupTree; } else { bestTree = createMemberRefTree(groupTree, membersList); } propertyBuilder = PropertyBuilder.array(Type.WEAKREFERENCE, UserConstants.REP_MEMBERS); propCnt = 0; } else { propertyBuilder = PropertyBuilder.copy(Type.WEAKREFERENCE, bestProperty); propCnt = bestCount; } if ((propCnt + memberIds.size()) > membershipSizeThreshold) { while (!memberIds.isEmpty()) { Set<String> s = new HashSet<>(); Iterator<String> it = memberIds.keySet().iterator(); while (propCnt < membershipSizeThreshold && it.hasNext()) { s.add(it.next()); it.remove(); propCnt++; } propertyBuilder.addValues(s); bestTree.setProperty(propertyBuilder.getPropertyState()); if (it.hasNext()) { propCnt = 0; bestTree = createMemberRefTree(groupTree, membersList); propertyBuilder = PropertyBuilder.array(Type.WEAKREFERENCE, UserConstants.REP_MEMBERS); } } } else { propertyBuilder.addValues(memberIds.keySet()); bestTree.setProperty(propertyBuilder.getPropertyState()); } } return failed; } }
MembershipWriter { @NotNull Set<String> addMembers(@NotNull Tree groupTree, @NotNull Map<String, String> memberIds) { Tree membersList = groupTree.getChild(UserConstants.REP_MEMBERS_LIST); Iterator<Tree> trees = Iterators.concat( Iterators.singletonIterator(groupTree), membersList.getChildren().iterator() ); Set<String> failed = new HashSet<>(memberIds.size()); int bestCount = membershipSizeThreshold; PropertyState bestProperty = null; Tree bestTree = null; while (trees.hasNext() && !memberIds.isEmpty()) { Tree t = trees.next(); PropertyState refs = t.getProperty(UserConstants.REP_MEMBERS); if (refs != null) { int numRefs = 0; for (String ref : refs.getValue(Type.WEAKREFERENCES)) { String id = memberIds.remove(ref); if (id != null) { failed.add(id); if (memberIds.isEmpty()) { break; } } numRefs++; } if (numRefs < bestCount) { bestCount = numRefs; bestProperty = refs; bestTree = t; } } } if (!memberIds.isEmpty()) { PropertyBuilder<String> propertyBuilder; int propCnt; if (bestProperty == null) { if (!groupTree.hasProperty(UserConstants.REP_MEMBERS)) { bestTree = groupTree; } else { bestTree = createMemberRefTree(groupTree, membersList); } propertyBuilder = PropertyBuilder.array(Type.WEAKREFERENCE, UserConstants.REP_MEMBERS); propCnt = 0; } else { propertyBuilder = PropertyBuilder.copy(Type.WEAKREFERENCE, bestProperty); propCnt = bestCount; } if ((propCnt + memberIds.size()) > membershipSizeThreshold) { while (!memberIds.isEmpty()) { Set<String> s = new HashSet<>(); Iterator<String> it = memberIds.keySet().iterator(); while (propCnt < membershipSizeThreshold && it.hasNext()) { s.add(it.next()); it.remove(); propCnt++; } propertyBuilder.addValues(s); bestTree.setProperty(propertyBuilder.getPropertyState()); if (it.hasNext()) { propCnt = 0; bestTree = createMemberRefTree(groupTree, membersList); propertyBuilder = PropertyBuilder.array(Type.WEAKREFERENCE, UserConstants.REP_MEMBERS); } } } else { propertyBuilder.addValues(memberIds.keySet()); bestTree.setProperty(propertyBuilder.getPropertyState()); } } return failed; }  }
MembershipWriter { @NotNull Set<String> addMembers(@NotNull Tree groupTree, @NotNull Map<String, String> memberIds) { Tree membersList = groupTree.getChild(UserConstants.REP_MEMBERS_LIST); Iterator<Tree> trees = Iterators.concat( Iterators.singletonIterator(groupTree), membersList.getChildren().iterator() ); Set<String> failed = new HashSet<>(memberIds.size()); int bestCount = membershipSizeThreshold; PropertyState bestProperty = null; Tree bestTree = null; while (trees.hasNext() && !memberIds.isEmpty()) { Tree t = trees.next(); PropertyState refs = t.getProperty(UserConstants.REP_MEMBERS); if (refs != null) { int numRefs = 0; for (String ref : refs.getValue(Type.WEAKREFERENCES)) { String id = memberIds.remove(ref); if (id != null) { failed.add(id); if (memberIds.isEmpty()) { break; } } numRefs++; } if (numRefs < bestCount) { bestCount = numRefs; bestProperty = refs; bestTree = t; } } } if (!memberIds.isEmpty()) { PropertyBuilder<String> propertyBuilder; int propCnt; if (bestProperty == null) { if (!groupTree.hasProperty(UserConstants.REP_MEMBERS)) { bestTree = groupTree; } else { bestTree = createMemberRefTree(groupTree, membersList); } propertyBuilder = PropertyBuilder.array(Type.WEAKREFERENCE, UserConstants.REP_MEMBERS); propCnt = 0; } else { propertyBuilder = PropertyBuilder.copy(Type.WEAKREFERENCE, bestProperty); propCnt = bestCount; } if ((propCnt + memberIds.size()) > membershipSizeThreshold) { while (!memberIds.isEmpty()) { Set<String> s = new HashSet<>(); Iterator<String> it = memberIds.keySet().iterator(); while (propCnt < membershipSizeThreshold && it.hasNext()) { s.add(it.next()); it.remove(); propCnt++; } propertyBuilder.addValues(s); bestTree.setProperty(propertyBuilder.getPropertyState()); if (it.hasNext()) { propCnt = 0; bestTree = createMemberRefTree(groupTree, membersList); propertyBuilder = PropertyBuilder.array(Type.WEAKREFERENCE, UserConstants.REP_MEMBERS); } } } else { propertyBuilder.addValues(memberIds.keySet()); bestTree.setProperty(propertyBuilder.getPropertyState()); } } return failed; }   }
MembershipWriter { @NotNull Set<String> addMembers(@NotNull Tree groupTree, @NotNull Map<String, String> memberIds) { Tree membersList = groupTree.getChild(UserConstants.REP_MEMBERS_LIST); Iterator<Tree> trees = Iterators.concat( Iterators.singletonIterator(groupTree), membersList.getChildren().iterator() ); Set<String> failed = new HashSet<>(memberIds.size()); int bestCount = membershipSizeThreshold; PropertyState bestProperty = null; Tree bestTree = null; while (trees.hasNext() && !memberIds.isEmpty()) { Tree t = trees.next(); PropertyState refs = t.getProperty(UserConstants.REP_MEMBERS); if (refs != null) { int numRefs = 0; for (String ref : refs.getValue(Type.WEAKREFERENCES)) { String id = memberIds.remove(ref); if (id != null) { failed.add(id); if (memberIds.isEmpty()) { break; } } numRefs++; } if (numRefs < bestCount) { bestCount = numRefs; bestProperty = refs; bestTree = t; } } } if (!memberIds.isEmpty()) { PropertyBuilder<String> propertyBuilder; int propCnt; if (bestProperty == null) { if (!groupTree.hasProperty(UserConstants.REP_MEMBERS)) { bestTree = groupTree; } else { bestTree = createMemberRefTree(groupTree, membersList); } propertyBuilder = PropertyBuilder.array(Type.WEAKREFERENCE, UserConstants.REP_MEMBERS); propCnt = 0; } else { propertyBuilder = PropertyBuilder.copy(Type.WEAKREFERENCE, bestProperty); propCnt = bestCount; } if ((propCnt + memberIds.size()) > membershipSizeThreshold) { while (!memberIds.isEmpty()) { Set<String> s = new HashSet<>(); Iterator<String> it = memberIds.keySet().iterator(); while (propCnt < membershipSizeThreshold && it.hasNext()) { s.add(it.next()); it.remove(); propCnt++; } propertyBuilder.addValues(s); bestTree.setProperty(propertyBuilder.getPropertyState()); if (it.hasNext()) { propCnt = 0; bestTree = createMemberRefTree(groupTree, membersList); propertyBuilder = PropertyBuilder.array(Type.WEAKREFERENCE, UserConstants.REP_MEMBERS); } } } else { propertyBuilder.addValues(memberIds.keySet()); bestTree.setProperty(propertyBuilder.getPropertyState()); } } return failed; }   static final int DEFAULT_MEMBERSHIP_THRESHOLD; }