mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
No effective change - code cleanup - while to foreach on BeanList etc
This commit is contained in:
@@ -343,41 +343,41 @@ public final class BeanList<E> extends AbstractBeanCollection<E> implements List
|
||||
return list.remove(o);
|
||||
}
|
||||
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
public boolean removeAll(Collection<?> beans) {
|
||||
checkReadOnly();
|
||||
init();
|
||||
if (modifyRemoveListening) {
|
||||
boolean changed = false;
|
||||
Iterator<?> it = c.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object o = it.next();
|
||||
if (list.remove(o)) {
|
||||
modifyRemoval(o);
|
||||
for (Object bean : beans) {
|
||||
if (list.remove(bean)) {
|
||||
// register this bean as having been removed
|
||||
modifyRemoval(bean);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
return list.removeAll(c);
|
||||
return list.removeAll(beans);
|
||||
}
|
||||
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
public boolean retainAll(Collection<?> retainBeans) {
|
||||
checkReadOnly();
|
||||
init();
|
||||
if (modifyRemoveListening) {
|
||||
boolean changed = false;
|
||||
Iterator<E> it = list.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object o = it.next();
|
||||
if (!c.contains(o)) {
|
||||
Object bean = it.next();
|
||||
if (!retainBeans.contains(bean)) {
|
||||
// removing this bean
|
||||
it.remove();
|
||||
modifyRemoval(o);
|
||||
modifyRemoval(bean);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
return list.retainAll(c);
|
||||
return list.retainAll(retainBeans);
|
||||
}
|
||||
|
||||
public E set(int index, E element) {
|
||||
|
||||
@@ -195,9 +195,9 @@ public final class BeanMap<K, E> extends AbstractBeanCollection<E> implements Ma
|
||||
checkReadOnly();
|
||||
initClear();
|
||||
if (modifyRemoveListening) {
|
||||
for (K key : map.keySet()) {
|
||||
E o = map.remove(key);
|
||||
modifyRemoval(o);
|
||||
// add all beans to the removal list
|
||||
for (E bean : map.values()) {
|
||||
modifyRemoval(bean);
|
||||
}
|
||||
}
|
||||
map.clear();
|
||||
@@ -249,9 +249,12 @@ public final class BeanMap<K, E> extends AbstractBeanCollection<E> implements Ma
|
||||
checkReadOnly();
|
||||
init();
|
||||
if (modifyListening) {
|
||||
Object o = map.put(key, value);
|
||||
modifyAddition(value);
|
||||
modifyRemoval(o);
|
||||
Object oldBean = map.put(key, value);
|
||||
if (value != oldBean) {
|
||||
// register the add of the new and the removal of the old
|
||||
modifyAddition(value);
|
||||
modifyRemoval(oldBean);
|
||||
}
|
||||
}
|
||||
return map.put(key, value);
|
||||
}
|
||||
|
||||
@@ -192,32 +192,29 @@ public final class BeanSet<E> extends AbstractBeanCollection<E> implements Set<E
|
||||
return set.add(o);
|
||||
}
|
||||
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
public boolean addAll(Collection<? extends E> addCollection) {
|
||||
checkReadOnly();
|
||||
init();
|
||||
if (modifyAddListening) {
|
||||
boolean changed = false;
|
||||
Iterator<? extends E> it = c.iterator();
|
||||
while (it.hasNext()) {
|
||||
E o = it.next();
|
||||
if (set.add(o)) {
|
||||
modifyAddition(o);
|
||||
for (E bean : addCollection) {
|
||||
if (set.add(bean)) {
|
||||
// register the addition of the bean
|
||||
modifyAddition(bean);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
return set.addAll(c);
|
||||
return set.addAll(addCollection);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
checkReadOnly();
|
||||
initClear();
|
||||
if (modifyRemoveListening) {
|
||||
Iterator<E> it = set.iterator();
|
||||
while (it.hasNext()) {
|
||||
E e = it.next();
|
||||
modifyRemoval(e);
|
||||
for (E bean : set) {
|
||||
modifyRemoval(bean);
|
||||
}
|
||||
}
|
||||
set.clear();
|
||||
@@ -262,41 +259,40 @@ public final class BeanSet<E> extends AbstractBeanCollection<E> implements Set<E
|
||||
return set.remove(o);
|
||||
}
|
||||
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
public boolean removeAll(Collection<?> beans) {
|
||||
checkReadOnly();
|
||||
init();
|
||||
if (modifyRemoveListening) {
|
||||
boolean changed = false;
|
||||
Iterator<?> it = c.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object o = it.next();
|
||||
if (set.remove(o)) {
|
||||
modifyRemoval(o);
|
||||
for (Object bean : beans) {
|
||||
if (set.remove(bean)) {
|
||||
modifyRemoval(bean);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
return set.removeAll(c);
|
||||
return set.removeAll(beans);
|
||||
}
|
||||
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
public boolean retainAll(Collection<?> beans) {
|
||||
checkReadOnly();
|
||||
init();
|
||||
if (modifyRemoveListening) {
|
||||
boolean changed = false;
|
||||
Iterator<?> it = set.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object o = it.next();
|
||||
if (!c.contains(o)) {
|
||||
Object bean = it.next();
|
||||
if (!beans.contains(bean)) {
|
||||
// not retaining this bean so add it to the removal list
|
||||
it.remove();
|
||||
modifyRemoval(o);
|
||||
modifyRemoval(bean);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
return set.retainAll(c);
|
||||
return set.retainAll(beans);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
|
||||
@@ -40,9 +40,7 @@ class ModifyCollection<E> implements Collection<E> {
|
||||
|
||||
public boolean addAll(Collection<? extends E> collection) {
|
||||
boolean changed = false;
|
||||
Iterator<? extends E> it = collection.iterator();
|
||||
while (it.hasNext()) {
|
||||
E o = it.next();
|
||||
for (E o : collection) {
|
||||
if (c.add(o)) {
|
||||
owner.modifyAddition(o);
|
||||
changed = true;
|
||||
@@ -51,27 +49,33 @@ class ModifyCollection<E> implements Collection<E> {
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
c.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return c.contains(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> collection) {
|
||||
return c.containsAll(collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return c.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
Iterator<E> it = c.iterator();
|
||||
return new ModifyIterator<E>(owner, it);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
if (c.remove(o)) {
|
||||
owner.modifyRemoval(o);
|
||||
@@ -80,41 +84,45 @@ class ModifyCollection<E> implements Collection<E> {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> collection) {
|
||||
boolean changed = false;
|
||||
Iterator<?> it = collection.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object o = (Object) it.next();
|
||||
if (c.remove(o)) {
|
||||
owner.modifyRemoval(o);
|
||||
for (Object bean : collection) {
|
||||
if (c.remove(bean)) {
|
||||
owner.modifyRemoval(bean);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> collection) {
|
||||
boolean changed = false;
|
||||
Iterator<?> it = c.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object o = (Object) it.next();
|
||||
if (!collection.contains(o)) {
|
||||
Object bean = it.next();
|
||||
if (!collection.contains(bean)) {
|
||||
// not retaining this bean so add to removals
|
||||
it.remove();
|
||||
owner.modifyRemoval(o);
|
||||
owner.modifyRemoval(bean);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return c.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return c.toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
return c.toArray(a);
|
||||
}
|
||||
|
||||
@@ -39,12 +39,10 @@ class ModifyList<E> extends ModifyCollection<E> implements List<E> {
|
||||
owner.modifyAddition(element);
|
||||
}
|
||||
|
||||
public boolean addAll(int index, Collection<? extends E> co) {
|
||||
if (list.addAll(index, co)) {
|
||||
Iterator<? extends E> it = co.iterator();
|
||||
while (it.hasNext()) {
|
||||
E o = it.next();
|
||||
owner.modifyAddition(o);
|
||||
public boolean addAll(int index, Collection<? extends E> addCollection) {
|
||||
if (list.addAll(index, addCollection)) {
|
||||
for (E bean : addCollection) {
|
||||
owner.modifyAddition(bean);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
package com.avaje.ebean.common;
|
||||
|
||||
import com.avaje.ebean.bean.BeanCollection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
|
||||
public class BeanListTest {
|
||||
|
||||
Object object1 = new Object();
|
||||
Object object2 = new Object();
|
||||
Object object3 = new Object();
|
||||
|
||||
@NotNull
|
||||
private List<Object> all() {
|
||||
List<Object> all = new ArrayList<Object>();
|
||||
all.add(object1);
|
||||
all.add(object2);
|
||||
all.add(object3);
|
||||
return all;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<Object> some() {
|
||||
List<Object> some = new ArrayList<Object>();
|
||||
some.add(object2);
|
||||
some.add(object3);
|
||||
return some;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>();
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
list.add(object1);
|
||||
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(list.getModifyRemovals()).isEmpty();
|
||||
|
||||
list.add(object1);
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object1);
|
||||
|
||||
list.add(object2);
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object1, object2);
|
||||
|
||||
list.remove(object1);
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object2);
|
||||
assertThat(list.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAll_given_emptyStart() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>();
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
list.addAll(all());
|
||||
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
assertThat(list.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd_given_someAlreadyIn() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>(some());
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
assertThat(list.contains(object1)).isFalse();
|
||||
list.add(object1);
|
||||
assertThat(list.contains(object2)).isTrue();
|
||||
list.add(object2); // object2 added as List allows duplicates
|
||||
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object1, object2);
|
||||
assertThat(list.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddSome_given_someAlreadyIn() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>(some());
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
list.addAll(all());
|
||||
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
assertThat(list.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove_given_beansInAdditions() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>();
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
list.addAll(all());
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
|
||||
// act
|
||||
list.remove(object2);
|
||||
list.remove(object3);
|
||||
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(list.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAll_given_beansInAdditions() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>();
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
list.addAll(all());
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
|
||||
// act
|
||||
list.removeAll(some());
|
||||
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(list.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove_given_beansNotInAdditions() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>(all());
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
list.remove(object2);
|
||||
list.remove(object3);
|
||||
|
||||
// assert
|
||||
assertThat(list.getModifyAdditions()).isEmpty();
|
||||
assertThat(list.getModifyRemovals()).containsExactly(object2, object3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAll_given_beansNotInAdditions() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>(all());
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
list.removeAll(some());
|
||||
|
||||
// assert
|
||||
assertThat(list.getModifyAdditions()).isEmpty();
|
||||
assertThat(list.getModifyRemovals()).containsExactly(object2, object3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>(all());
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
list.clear();
|
||||
|
||||
//assert
|
||||
assertThat(list.getModifyRemovals()).containsExactly(object1, object2, object3);
|
||||
assertThat(list.getModifyAdditions()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear_given_someBeansInAdditions() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>();
|
||||
list.add(object1);
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
list.add(object2);
|
||||
list.add(object3);
|
||||
|
||||
// act
|
||||
list.clear();
|
||||
|
||||
//assert
|
||||
assertThat(list.getModifyRemovals()).containsExactly(object1);
|
||||
assertThat(list.getModifyAdditions()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRetainAll_given_beansInAdditions() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>();
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
list.addAll(all());
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
|
||||
// act
|
||||
list.retainAll(some());
|
||||
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object2, object3);
|
||||
assertThat(list.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRetainAll_given_someBeansInAdditions() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>();
|
||||
list.add(object1);
|
||||
list.add(object2);
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
list.add(object3);
|
||||
|
||||
// act
|
||||
list.retainAll(some());
|
||||
|
||||
assertThat(list.getModifyAdditions()).containsExactly(object3);
|
||||
assertThat(list.getModifyRemovals()).containsExactly(object1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRetainAll_given_noBeansInAdditions() throws Exception {
|
||||
|
||||
BeanList<Object> list = new BeanList<Object>(all());
|
||||
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
list.retainAll(some());
|
||||
|
||||
assertThat(list.getModifyRemovals()).containsExactly(object1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package com.avaje.ebean.common;
|
||||
|
||||
import com.avaje.ebean.bean.BeanCollection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class BeanMapTest {
|
||||
|
||||
Object object1 = new Object();
|
||||
Object object2 = new Object();
|
||||
Object object3 = new Object();
|
||||
|
||||
@NotNull
|
||||
private Map<String, Object> all() {
|
||||
Map<String, Object> all = new LinkedHashMap<String, Object>();
|
||||
all.put("1", object1);
|
||||
all.put("2", object2);
|
||||
all.put("3", object3);
|
||||
return all;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Map<String, Object> some() {
|
||||
Map<String, Object> all = new LinkedHashMap<String, Object>();
|
||||
all.put("2", object2);
|
||||
all.put("3", object3);
|
||||
return all;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() throws Exception {
|
||||
|
||||
BeanMap<String,Object> map = new BeanMap<String,Object>();
|
||||
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
map.put("1", object1);
|
||||
map.put("4", null);
|
||||
|
||||
assertThat(map.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(map.getModifyRemovals()).isEmpty();
|
||||
|
||||
map.put("1", object1);
|
||||
map.put("4", null);
|
||||
assertThat(map.getModifyAdditions()).containsExactly(object1);
|
||||
|
||||
map.put("2", object2);
|
||||
assertThat(map.getModifyAdditions()).containsExactly(object1, object2);
|
||||
|
||||
map.remove("1");
|
||||
assertThat(map.getModifyAdditions()).containsExactly(object2);
|
||||
assertThat(map.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAll_given_emptyStart() throws Exception {
|
||||
|
||||
BeanMap<String,Object> set = new BeanMap<String,Object>();
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
set.putAll(all());
|
||||
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
assertThat(set.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd_given_someAlreadyIn() throws Exception {
|
||||
|
||||
BeanMap<String,Object> map = new BeanMap<String,Object>(some());
|
||||
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
assertThat(map.values().contains(object1)).isFalse();
|
||||
map.put("1", object1);
|
||||
assertThat(map.values().contains(object2)).isTrue();
|
||||
map.put("2", object2);
|
||||
|
||||
assertThat(map.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(map.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddSome_given_someAlreadyIn() throws Exception {
|
||||
|
||||
BeanMap<String,Object> map = new BeanMap<String,Object>(some());
|
||||
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
map.putAll(all());
|
||||
|
||||
assertThat(map.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(map.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove_given_beansInAdditions() throws Exception {
|
||||
|
||||
BeanMap<String,Object> map = new BeanMap<String,Object>();
|
||||
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
map.putAll(all());
|
||||
assertThat(map.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
|
||||
// act
|
||||
map.remove("2");
|
||||
map.remove("3");
|
||||
|
||||
assertThat(map.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(map.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAll_given_beansInAdditions() throws Exception {
|
||||
|
||||
BeanMap<String,Object> map = new BeanMap<String,Object>();
|
||||
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
map.putAll(all());
|
||||
assertThat(map.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
|
||||
// act
|
||||
map.remove("2");
|
||||
map.remove("3");
|
||||
|
||||
assertThat(map.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(map.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove_given_beansNotInAdditions() throws Exception {
|
||||
|
||||
BeanMap<String,Object> map = new BeanMap<String,Object>(all());
|
||||
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
map.remove("2");
|
||||
map.remove("3");
|
||||
|
||||
// assert
|
||||
assertThat(map.getModifyAdditions()).isEmpty();
|
||||
assertThat(map.getModifyRemovals()).containsExactly(object2, object3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAll_given_beansNotInAdditions() throws Exception {
|
||||
|
||||
BeanMap<String,Object> map = new BeanMap<String,Object>(all());
|
||||
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
map.remove("2");
|
||||
map.remove("3");
|
||||
|
||||
// assert
|
||||
assertThat(map.getModifyAdditions()).isEmpty();
|
||||
assertThat(map.getModifyRemovals()).containsExactly(object2, object3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() throws Exception {
|
||||
|
||||
BeanMap<String,Object> map = new BeanMap<String,Object>(all());
|
||||
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
map.clear();
|
||||
|
||||
//assert
|
||||
assertThat(map.getModifyRemovals()).containsExactly(object1, object2, object3);
|
||||
assertThat(map.getModifyAdditions()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear_given_someBeansInAdditions() throws Exception {
|
||||
|
||||
BeanMap<String,Object> map = new BeanMap<String,Object>();
|
||||
map.put("1",object1);
|
||||
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
map.put("2", object2);
|
||||
map.put("3", object3);
|
||||
|
||||
// act
|
||||
map.clear();
|
||||
|
||||
//assert
|
||||
assertThat(map.getModifyRemovals()).containsExactly(object1);
|
||||
assertThat(map.getModifyAdditions()).isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
package com.avaje.ebean.common;
|
||||
|
||||
import com.avaje.ebean.bean.BeanCollection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class BeanSetTest {
|
||||
|
||||
Object object1 = new Object();
|
||||
Object object2 = new Object();
|
||||
Object object3 = new Object();
|
||||
|
||||
@NotNull
|
||||
private Set<Object> all() {
|
||||
Set<Object> all = new LinkedHashSet<Object>();
|
||||
all.add(object1);
|
||||
all.add(object2);
|
||||
all.add(object3);
|
||||
return all;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Set<Object> some() {
|
||||
Set<Object> some = new LinkedHashSet<Object>();
|
||||
some.add(object2);
|
||||
some.add(object3);
|
||||
return some;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>();
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
set.add(object1);
|
||||
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(set.getModifyRemovals()).isEmpty();
|
||||
|
||||
set.add(object1);
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1);
|
||||
|
||||
set.add(object2);
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1, object2);
|
||||
|
||||
set.remove(object1);
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object2);
|
||||
assertThat(set.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAll_given_emptyStart() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>();
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
set.addAll(all());
|
||||
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
assertThat(set.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd_given_someAlreadyIn() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>(some());
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
assertThat(set.contains(object1)).isFalse();
|
||||
set.add(object1);
|
||||
assertThat(set.contains(object2)).isTrue();
|
||||
set.add(object2);
|
||||
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(set.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddSome_given_someAlreadyIn() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>(some());
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
set.addAll(all());
|
||||
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(set.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove_given_beansInAdditions() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>();
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
set.addAll(all());
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
|
||||
// act
|
||||
set.remove(object2);
|
||||
set.remove(object3);
|
||||
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(set.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAll_given_beansInAdditions() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>();
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
set.addAll(all());
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
|
||||
// act
|
||||
set.removeAll(some());
|
||||
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1);
|
||||
assertThat(set.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove_given_beansNotInAdditions() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>(all());
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
set.remove(object2);
|
||||
set.remove(object3);
|
||||
|
||||
// assert
|
||||
assertThat(set.getModifyAdditions()).isEmpty();
|
||||
assertThat(set.getModifyRemovals()).containsExactly(object2, object3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAll_given_beansNotInAdditions() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>(all());
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
set.removeAll(some());
|
||||
|
||||
// assert
|
||||
assertThat(set.getModifyAdditions()).isEmpty();
|
||||
assertThat(set.getModifyRemovals()).containsExactly(object2, object3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>(all());
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
set.clear();
|
||||
|
||||
//assert
|
||||
assertThat(set.getModifyRemovals()).containsExactly(object1, object2, object3);
|
||||
assertThat(set.getModifyAdditions()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear_given_someBeansInAdditions() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>();
|
||||
set.add(object1);
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
set.add(object2);
|
||||
set.add(object3);
|
||||
|
||||
// act
|
||||
set.clear();
|
||||
|
||||
//assert
|
||||
assertThat(set.getModifyRemovals()).containsExactly(object1);
|
||||
assertThat(set.getModifyAdditions()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRetainAll_given_beansInAdditions() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>();
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
set.addAll(all());
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object1, object2, object3);
|
||||
|
||||
// act
|
||||
set.retainAll(some());
|
||||
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object2, object3);
|
||||
assertThat(set.getModifyRemovals()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRetainAll_given_someBeansInAdditions() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>();
|
||||
set.add(object1);
|
||||
set.add(object2);
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
set.add(object3);
|
||||
|
||||
// act
|
||||
set.retainAll(some());
|
||||
|
||||
assertThat(set.getModifyAdditions()).containsExactly(object3);
|
||||
assertThat(set.getModifyRemovals()).containsExactly(object1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRetainAll_given_noBeansInAdditions() throws Exception {
|
||||
|
||||
BeanSet<Object> set = new BeanSet<Object>(all());
|
||||
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
|
||||
|
||||
// act
|
||||
set.retainAll(some());
|
||||
|
||||
assertThat(set.getModifyRemovals()).containsExactly(object1);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user