Added public method to remove all members from a group (#1283)

* Added public method to remove all members from a group
* Spelling in itest method names
* File encoding

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2020-01-26 12:54:37 +01:00 committed by Kai Kreuzer
parent 86899daf1a
commit ac537c53d9
2 changed files with 51 additions and 6 deletions

View File

@ -88,10 +88,7 @@ public class GroupItem extends GenericItem implements StateChangeListener {
@Override
public void dispose() {
super.dispose();
for (Item member : getMembers()) {
unregisterStateListener(member);
}
members.clear();
removeAllMembers();
}
/**
@ -220,6 +217,16 @@ public class GroupItem extends GenericItem implements StateChangeListener {
unregisterStateListener(item);
}
/**
* Removes all items from the members of this group item.
*/
public void removeAllMembers() {
for (Item member : getMembers()) {
unregisterStateListener(member);
}
members.clear();
}
@Override
public void setUnitProvider(@Nullable UnitProvider unitProvider) {
super.setUnitProvider(unitProvider);

View File

@ -180,7 +180,7 @@ public class GroupItemOSGiTest extends JavaOSGiTest {
}
@Test
public void testDisposeUnregistersAsListenerFromMembers() {
public void testRemoveMemberUnregistersListenerFromMember() {
GroupItem group = new GroupItem("group");
TestItem member1 = new TestItem("member1");
TestItem member2 = new TestItem("member2");
@ -189,12 +189,50 @@ public class GroupItemOSGiTest extends JavaOSGiTest {
group.addMember(member2);
assertThat(member1.getListeners(), hasSize(1));
assertThat(member2.getListeners(), hasSize(1));
assertThat(group.getMembers(), hasSize(2));
group.removeMember(member1);
assertThat(member1.getListeners(), hasSize(0));
assertThat(member2.getListeners(), hasSize(1));
assertThat(group.getMembers(), hasSize(1));
}
@Test
public void testRemoveAllMembersUnregistersListenerFromAllMembers() {
GroupItem group = new GroupItem("group");
TestItem member1 = new TestItem("member1");
TestItem member2 = new TestItem("member2");
group.addMember(member1);
group.addMember(member2);
assertThat(member1.getListeners(), hasSize(1));
assertThat(member2.getListeners(), hasSize(1));
assertThat(group.getMembers(), hasSize(2));
group.removeAllMembers();
assertThat(member1.getListeners(), hasSize(0));
assertThat(member2.getListeners(), hasSize(0));
assertThat(group.getMembers(), hasSize(0));
}
@Test
public void testDisposeUnregistersListenerFromAllMembers() {
GroupItem group = new GroupItem("group");
TestItem member1 = new TestItem("member1");
TestItem member2 = new TestItem("member2");
group.addMember(member1);
group.addMember(member2);
assertThat(member1.getListeners(), hasSize(1));
assertThat(member2.getListeners(), hasSize(1));
assertThat(group.getMembers(), hasSize(2));
group.dispose();
assertThat(member1.getListeners(), hasSize(0));
assertThat(member1.getListeners(), hasSize(0));
assertThat(member2.getListeners(), hasSize(0));
assertThat(group.getMembers(), hasSize(0));
}