浏览代码

Updated MPCModule to return a SortedSet and switched collector

See: https://github.com/cryptomator/cryptomator/pull/1307#discussion_r474452898
JaniruTEC 4 年之前
父节点
当前提交
9657a13912

+ 6 - 4
main/commons/src/main/java/org/cryptomator/common/mountpoint/MountPointChooserModule.java

@@ -1,6 +1,6 @@
 package org.cryptomator.common.mountpoint;
 
-import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSortedSet;
 import dagger.Binds;
 import dagger.Module;
 import dagger.Provides;
@@ -8,7 +8,9 @@ import dagger.multibindings.IntoSet;
 import org.cryptomator.common.vaults.PerVault;
 
 import javax.inject.Named;
+import java.util.Comparator;
 import java.util.Set;
+import java.util.SortedSet;
 
 /**
  * Dagger-Module for {@link MountPointChooser MountPointChoosers.}<br>
@@ -42,8 +44,8 @@ public abstract class MountPointChooserModule {
 	@Provides
 	@PerVault
 	@Named("orderedValidMountPointChoosers")
-	public static Set<MountPointChooser> provideOrderedValidMountPointChoosers(Set<MountPointChooser> choosers) {
-		//Sorted Set
-		return choosers.stream().sorted().filter(MountPointChooser::isApplicable).collect(ImmutableSet.toImmutableSet());
+	public static SortedSet<MountPointChooser> provideOrderedValidMountPointChoosers(Set<MountPointChooser> choosers) {
+		//The natural order is defined by MountPointChooser#compareTo
+		return choosers.stream().filter(MountPointChooser::isApplicable).collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder()));
 	}
 }

+ 5 - 3
main/commons/src/main/java/org/cryptomator/common/vaults/AbstractVolume.java

@@ -7,11 +7,11 @@ import org.cryptomator.common.mountpoint.MountPointChooser;
 
 import java.nio.file.Path;
 import java.util.Optional;
-import java.util.Set;
+import java.util.SortedSet;
 
 public abstract class AbstractVolume implements Volume {
 
-	private final Set<MountPointChooser> choosers;
+	private final SortedSet<MountPointChooser> choosers;
 
 	protected Path mountPoint;
 
@@ -19,7 +19,7 @@ public abstract class AbstractVolume implements Volume {
 	private boolean cleanupRequired;
 	private MountPointChooser usedChooser;
 
-	public AbstractVolume(Set<MountPointChooser> choosers) {
+	public AbstractVolume(SortedSet<MountPointChooser> choosers) {
 		this.choosers = choosers;
 	}
 
@@ -34,6 +34,8 @@ public abstract class AbstractVolume implements Volume {
 			this.usedChooser = chooser;
 			return chosenPath.get();
 		}
+		//SortedSet#stream() should return a sorted stream (that's what it's docs and the docs of #spliterator() say, even if they are not 100% clear for me.)
+		//We want to keep that order, that's why we use ImmutableSet#toImmutableSet() to collect (even if it doesn't implement SortedSet, it's docs promise use encounter ordering.)
 		String tried = Joiner.on(", ").join(this.choosers.stream().map((mpc) -> mpc.getClass().getTypeName()).collect(ImmutableSet.toImmutableSet()));
 		throw new InvalidMountPointException(String.format("No feasible MountPoint found! Tried %s", tried));
 	}

+ 2 - 2
main/commons/src/main/java/org/cryptomator/common/vaults/DokanyVolume.java

@@ -12,7 +12,7 @@ import org.slf4j.LoggerFactory;
 
 import javax.inject.Inject;
 import javax.inject.Named;
-import java.util.Set;
+import java.util.SortedSet;
 import java.util.concurrent.ExecutorService;
 
 public class DokanyVolume extends AbstractVolume {
@@ -27,7 +27,7 @@ public class DokanyVolume extends AbstractVolume {
 	private Mount mount;
 
 	@Inject
-	public DokanyVolume(VaultSettings vaultSettings, ExecutorService executorService, @Named("orderedValidMountPointChoosers") Set<MountPointChooser> choosers) {
+	public DokanyVolume(VaultSettings vaultSettings, ExecutorService executorService, @Named("orderedValidMountPointChoosers") SortedSet<MountPointChooser> choosers) {
 		super(choosers);
 		this.vaultSettings = vaultSettings;
 		this.mountFactory = new MountFactory(executorService);

+ 2 - 2
main/commons/src/main/java/org/cryptomator/common/vaults/FuseVolume.java

@@ -17,7 +17,7 @@ import org.slf4j.LoggerFactory;
 import javax.inject.Inject;
 import javax.inject.Named;
 import java.nio.file.Path;
-import java.util.Set;
+import java.util.SortedSet;
 
 public class FuseVolume extends AbstractVolume {
 
@@ -26,7 +26,7 @@ public class FuseVolume extends AbstractVolume {
 	private Mount mount;
 
 	@Inject
-	public FuseVolume(@Named("orderedValidMountPointChoosers") Set<MountPointChooser> choosers) {
+	public FuseVolume(@Named("orderedValidMountPointChoosers") SortedSet<MountPointChooser> choosers) {
 		super(choosers);
 	}