org.hpccsystems.ws.client.utils.CollectionDelta Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2014 HPCC Systems. All rights reserved. This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: HPCC Systems - initial API and implementation
******************************************************************************/
package org.hpccsystems.ws.client.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
/**
* Used internally to monitor and track changes in generic collections.
*
*/
public class CollectionDelta
{
private ArrayList before;
protected String cause;
protected ArrayList added;
protected ArrayList unchanged;
protected ArrayList removed;
public CollectionDelta(String cause)
{
this.cause = cause;
before = new ArrayList();
}
public CollectionDelta(String cause, Collection before)
{
this.cause = cause;
this.before = new ArrayList(before);
}
public CollectionDelta calcChanges(T[] after)
{
return calcChanges(new ArrayList(Arrays.asList(after)));
}
public CollectionDelta calcChanges(Collection after)
{
new ArrayList(after);
added = new ArrayList();
unchanged = new ArrayList();
removed = new ArrayList(before);
for (T item : after)
{
if (removed.contains(item))
{
unchanged.add(item);
removed.remove(item);
}
else
{
added.add(item);
}
}
return this;
}
public boolean hasChanged()
{
return !removed.isEmpty() || !added.isEmpty();
}
public boolean exists(DataSingleton item)
{
return added.contains(item) || unchanged.contains(item);
}
public String getCause()
{
return cause;
}
public boolean removeContains(DataSingleton item)
{
return removed.contains(item);
}
public ArrayList getAdded()
{
return added;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy