blob: 32ba72aa37e6a17269b09a779245f0f9139cf142 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro
* 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:
* Andras Schmidt, Andras Balogh, Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.core.simple;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;
/**
* This class implements an algorithm to test two collections whether they are
* disjunkt or not.
*
* @author Andras Schmidt
*
*/
public class CollectionsDisjunktTester {
/**
* Tests whether the two collections are disjunkt or not. Will work fast
* (TODO) if ordered sets are given (with the same ordering).
*
* @param col1
* @param col2
* @return
*/
public static boolean isDisjunct(Collection col1, Collection col2) {
// TODO try to find out the structure of collections and implement a
// faster search
return isDisjunctBruteForce(col1, col2);
}
private static boolean isDisjunctBruteForce(Collection col1, Collection col2) {
Iterator it = col1.iterator();
while (it.hasNext()) {
if (col2.contains(it.next()))
return false;
}
return true;
}
/**
* returns the shared objects in the two collections. Will work fast (TODO)
* if ordered sets are given (with the same ordering).
*
* @param col1
* @param col2
* @return
*/
public static Collection<Object> sharedElements(Collection col1,
Collection col2) {
TreeSet<Object> ret = new TreeSet<Object>();
Iterator it = col1.iterator();
while (it.hasNext()) {
Object o = it.next();
if (col2.contains(o))
ret.add(o);
}
return ret;
}
}