net.clementlevallois.utils.FindAllPairs Maven / Gradle / Ivy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.clementlevallois.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
*
* C.Levallois
* @param
*/
public class FindAllPairs> {
private T t;
/**
*
* @param setObjects
* @return
*/
public Set> getAllDirectedPairs(Set setObjects) {
Set setObjectsProcessed = new HashSet();
Set> setPairs = new HashSet();
Iterator setObjectsIteratorA = setObjects.iterator();
Iterator setObjectsIteratorB;
T currTA;
T currTB;
while (setObjectsIteratorA.hasNext()) {
currTA = setObjectsIteratorA.next();
setObjectsIteratorB = setObjects.iterator();
while (setObjectsIteratorB.hasNext()) {
currTB = setObjectsIteratorB.next();
if (!setObjectsProcessed.contains(currTB) && !currTA.equals(currTB)) {
setPairs.add(new DirectedPair(currTA, currTB));
}
}
setObjectsProcessed.add(currTA);
}
return setPairs;
}
/**
*
* @param setSources
* @param setTargets
* @return
*/
public Set> getAllDirectedPairsFromTwoSets(Set setSources, Set setTargets) {
Set> setPairs = new TreeSet();
Iterator setSourcesIterator = setSources.iterator();
Iterator setTargetsIterator;
T source;
T target;
while (setSourcesIterator.hasNext()) {
source = setSourcesIterator.next();
setTargetsIterator = setTargets.iterator();
while (setTargetsIterator.hasNext()) {
target = setTargetsIterator.next();
if (!source.equals(target)) {
setPairs.add(new DirectedPair(source, target));
}
}
}
return setPairs;
}
/**
*
* @param setObjects
* @return
*/
public Set> getAllUndirectedPairs(Set setObjects) {
Set> setPairs;
setPairs = new HashSet();
if (setObjects == null || setObjects.size() < 2) {
return setPairs;
}
Set setObjectsProcessed = new HashSet();
Iterator setObjectsIteratorA = setObjects.iterator();
Iterator setObjectsIteratorB;
T currTA;
T currTB;
while (setObjectsIteratorA.hasNext()) {
currTA = setObjectsIteratorA.next();
setObjectsIteratorB = setObjects.iterator();
while (setObjectsIteratorB.hasNext()) {
currTB = setObjectsIteratorB.next();
if (!setObjectsProcessed.contains(currTB) && !currTA.equals(currTB)) {
setPairs.add(new UnDirectedPair(currTA, currTB));
}
}
setObjectsProcessed.add(currTA);
}
return setPairs;
}
/**
*
* @param listObjects
* @return
*/
public Set> getAllUndirectedPairsFromList(List listObjects) {
Set> setPairs;
setPairs = new HashSet();
if (listObjects.size() < 2) {
return setPairs;
}
Set setObjectsProcessed = new HashSet();
Iterator setObjectsIteratorA = listObjects.iterator();
Iterator setObjectsIteratorB;
T currTA;
T currTB;
while (setObjectsIteratorA.hasNext()) {
currTA = setObjectsIteratorA.next();
setObjectsIteratorB = listObjects.iterator();
while (setObjectsIteratorB.hasNext()) {
currTB = setObjectsIteratorB.next();
if (!setObjectsProcessed.contains(currTB) && !currTA.equals(currTB)) {
setPairs.add(new UnDirectedPair(currTA, currTB));
}
}
setObjectsProcessed.add(currTA);
}
return setPairs;
}
/**
*
* @param setObjects
* @return
*/
public List