Skip to content

Instantly share code, notes, and snippets.

@prietopa
Last active August 29, 2015 14:13
Show Gist options
  • Save prietopa/3d31af95c8a93b27a2fb to your computer and use it in GitHub Desktop.
Save prietopa/3d31af95c8a93b27a2fb to your computer and use it in GitHub Desktop.
Compare List easy
public abstract class ListComparator<B> {
/**
* Only compare elements must not change, like id.
*
* @param left
* @param right
* @return
*/
protected abstract boolean isEqualsBeans(B left, B right);
/**
* Compare elements that must change.
*
* @param left
* @param right
* @return
*/
protected abstract boolean isModifyBean(B left, B right);
public List<B> getNewElements(List<B> right, List<B> left) {
return commonNewDel(right, left);
}
public List<B> getDelElements(List<B> left, List<B> right) {
return commonNewDel(left, right);
}
public List<B> getModElements(List<B> listaBase, List<B> listaComprobacion) {
List<B> list = new ArrayList<B>();
for(B b: listaBase){
if(existAndIsModify(listaComprobacion, b)) {
list.add(b);
}
}
return list;
}
private boolean existAndIsModify(List<B> list, B b) {
boolean existe = false;
boolean existAndIsModify = false;
Iterator<B> iterator = list.iterator();
while (iterator.hasNext() && !existe) {
B bean = iterator.next();
existe = isEqualsBeans(bean, b);
if(existe) {
existAndIsModify = isModifyBean(bean, b);
}
}
return existAndIsModify;
}
public List<B> getKeepElements(List<B> left, List<B> right) {
List<B> list = new ArrayList<B>();
for(B rightBean: right){
if(exist(left, rightBean)) {
list.add(rightBean);
}
}
return list;
}
protected List<B> commonNewDel(List<B> listaBase, List<B> listaComprobacion) {
List<B> list = new ArrayList<B>();
for(B b: listaBase){
if( ! exist(listaComprobacion, b)) {
list.add(b);
}
}
return list;
}
public boolean isModify(List<B> left, List<B> right) {
for(B file: left) {
if( ! exist(right, file)) {
return true;
}
}
return false;
}
private boolean exist(List<B> list, B cpv) {
boolean existe = false;
Iterator<B> iterator = list.iterator();
while (iterator.hasNext() && !existe) {
B bean = iterator.next();
existe = isEqualsBeans(bean, cpv);
}
return existe;
}
}
public class ListComparatorTest {
protected class MockBean {
private int id;
private String name;
private String description;
public MockBean(int id, String name, String description) {
super();
this.id = id;
this.name = name;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
private ListComparator<MockBean> testComparator = null;
private List<MockBean> listaLeft = null;
private List<MockBean> listaRight = null;
@Before
public void init() {
testComparator = new ListComparator<MockBean>() {
protected boolean isEqualsBeans(MockBean left, MockBean right) {
return left.getId() == right.getId();
}
@Override
protected boolean isModifyBean(MockBean left, MockBean right) {
if( ! left.getDescription().equals(right.getDescription())) return true;
if( ! left.getName().equals(right.getName())) return true;
return false;
}
};
listaLeft = new ArrayList<MockBean>();
listaLeft.add(new MockBean(0, "Cero", "Cero description"));
listaLeft.add(new MockBean(1, "Uno", "Uno description"));
listaLeft.add(new MockBean(2, "Dos", "Dos description"));
listaLeft.add(new MockBean(3, "Tres", "Tres description"));
listaRight = new ArrayList<MockBean>();
listaRight.add(new MockBean(1, "Uno", "Uno descriptionXXXX"));
listaRight.add(new MockBean(2, "Dos", "Dos description"));
listaRight.add(new MockBean(4, "Cuatro", "Cuatro description"));
}
@Test
public void testGetNewElements() {
assertTrue(testComparator.getNewElements(listaRight, listaLeft).size() == 1);
}
@Test
public void testGetModElements() {
assertTrue(testComparator.getModElements(listaRight, listaLeft).size() == 1);
}
@Test
public void testGetDelElements() {
assertTrue(testComparator.getDelElements(listaLeft, listaRight).size() == 2);
}
@Test
public void testGetKeepElements() {
assertTrue(testComparator.getKeepElements(listaLeft, listaRight).size() == 2);
}
@Test
public void testIsModify() {
assertTrue(testComparator.isModify(listaLeft, listaRight));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment