Update FileMirrorsManager.java and MirrorAvailabilityManager.java

This commit is contained in:
Andrea Cavalli 2020-12-04 18:15:49 +01:00
parent a25ad89f4b
commit e489bd05d7
2 changed files with 24 additions and 22 deletions

View File

@ -18,7 +18,6 @@
package org.warp.filesponge; package org.warp.filesponge;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -29,7 +28,7 @@ import org.warp.filesponge.value.MirrorURI;
@AllArgsConstructor() @AllArgsConstructor()
public class FileMirrorsManager { public class FileMirrorsManager {
private final Set<MirrorURI> availableMirrors = new HashSet<>(); private final MirrorAvailabilityManager mirrorAvailabilityManager;
/** /**
* This map must be persistent * This map must be persistent
@ -40,12 +39,12 @@ public class FileMirrorsManager {
return fileMirrors return fileMirrors
.getLinks(fileURI) .getLinks(fileURI)
.stream() .stream()
.filter(this::isMirrorAvailable) .filter(mirrorAvailabilityManager::isMirrorAvailable)
.collect(Collectors.toUnmodifiableSet()); .collect(Collectors.toUnmodifiableSet());
} }
public synchronized boolean hasAnyAvailableReceiver(FileURI uri) { public synchronized boolean hasAnyAvailableMirror(FileURI uri) {
return fileMirrors.getLinks(uri).stream().anyMatch(this::isMirrorAvailable); return fileMirrors.getLinks(uri).stream().anyMatch(mirrorAvailabilityManager::isMirrorAvailable);
} }
public synchronized void addMirror(FileURI uri, MirrorURI mirrorURI) { public synchronized void addMirror(FileURI uri, MirrorURI mirrorURI) {
@ -59,20 +58,4 @@ public class FileMirrorsManager {
public synchronized void unsetAllFiles() { public synchronized void unsetAllFiles() {
fileMirrors.clear(); fileMirrors.clear();
} }
public synchronized void setAllMirrorsAsUnavailable() {
availableMirrors.clear();
}
public synchronized void setReceiverAvailability(MirrorURI mirrorURI, boolean available) {
if (available) {
availableMirrors.add(mirrorURI);
} else {
availableMirrors.remove(mirrorURI);
}
}
public synchronized boolean isMirrorAvailable(MirrorURI mirrorURI) {
return this.availableMirrors.contains(mirrorURI);
}
} }

View File

@ -18,11 +18,30 @@
package org.warp.filesponge; package org.warp.filesponge;
import java.util.HashSet;
import java.util.Set;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.warp.filesponge.value.MirrorURI;
@AllArgsConstructor(access = AccessLevel.PUBLIC) @AllArgsConstructor(access = AccessLevel.PUBLIC)
public class MirrorAvailabilityManager { public class MirrorAvailabilityManager {
private final FileMirrorsManager fileMirrorsManager; private final Set<MirrorURI> availableMirrors = new HashSet<>();
public synchronized void setAllMirrorsAsUnavailable() {
availableMirrors.clear();
}
public synchronized void setMirrorAvailability(MirrorURI mirrorURI, boolean available) {
if (available) {
availableMirrors.add(mirrorURI);
} else {
availableMirrors.remove(mirrorURI);
}
}
public synchronized boolean isMirrorAvailable(MirrorURI mirrorURI) {
return this.availableMirrors.contains(mirrorURI);
}
} }