Add getSources and getDestinations

This commit is contained in:
Andrea Cavalli 2020-11-10 23:43:17 +01:00
parent 326f80fc68
commit bd80576306
6 changed files with 78 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package org.warp.commonutils.type;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import java.util.Optional;
import java.util.Set;
@ -101,6 +102,18 @@ public interface Association<T, U> {
*/
int size();
/**
* Get all the sources
* @return Set of sources
*/
ObjectSet<T> getSources();
/**
* Get all the destinations
* @return Set of destinations
*/
ObjectSet<U> getDestinations();
static <T, U> Association<T, U> synchronize(Association<T, U> association) {
return new SynchronizedAssociation(association);
}

View File

@ -1,5 +1,6 @@
package org.warp.commonutils.type;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import java.util.Optional;
/**
@ -100,6 +101,18 @@ public interface BiAssociation<T, U> {
*/
int size();
/**
* Get all the sources
* @return Set of sources
*/
ObjectSet<T> getSources();
/**
* Get all the destinations
* @return Set of destinations
*/
ObjectSet<U> getDestinations();
static <T, U> BiAssociation<T, U> synchronize(BiAssociation<T, U> association) {
return new SynchronizedBiAssociation(association);
}

View File

@ -2,6 +2,7 @@ package org.warp.commonutils.type;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@ -156,6 +157,16 @@ public class HashAssociation<T, U> implements Association<T, U> {
return inverseAssociations.size();
}
@Override
public ObjectSet<T> getSources() {
return associations.clone().keySet();
}
@Override
public ObjectSet<U> getDestinations() {
return inverseAssociations.clone().keySet();
}
@SuppressWarnings("MethodDoesntCallSuperMethod")
@Override
public HashAssociation<T, U> clone() {

View File

@ -1,6 +1,7 @@
package org.warp.commonutils.type;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import java.util.Objects;
import java.util.Optional;
@ -134,6 +135,16 @@ public class HashBiAssociation<T, U> implements BiAssociation<T, U> {
return inverseAssociations.size();
}
@Override
public ObjectSet<T> getSources() {
return associations.clone().keySet();
}
@Override
public ObjectSet<U> getDestinations() {
return inverseAssociations.clone().keySet();
}
@SuppressWarnings("MethodDoesntCallSuperMethod")
@Override
public HashBiAssociation<T, U> clone() {

View File

@ -1,5 +1,6 @@
package org.warp.commonutils.type;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import java.util.Optional;
import java.util.Set;
@ -94,4 +95,18 @@ public final class SynchronizedAssociation<T, U> implements Association<T, U> {
return association.size();
}
}
@Override
public ObjectSet<T> getSources() {
synchronized (lock) {
return association.getSources();
}
}
@Override
public ObjectSet<U> getDestinations() {
synchronized (lock) {
return association.getDestinations();
}
}
}

View File

@ -1,5 +1,6 @@
package org.warp.commonutils.type;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import java.util.Optional;
public final class SynchronizedBiAssociation<T, U> implements BiAssociation<T, U> {
@ -93,4 +94,18 @@ public final class SynchronizedBiAssociation<T, U> implements BiAssociation<T, U
return association.size();
}
}
@Override
public ObjectSet<T> getSources() {
synchronized (lock) {
return association.getSources();
}
}
@Override
public ObjectSet<U> getDestinations() {
synchronized (lock) {
return association.getDestinations();
}
}
}