Ensure that callers get a well-behaving Set

Returning a container with a custom add method could confuse callers.
Provide same functionality via Function.
This commit is contained in:
Andrew Gaul 2012-11-04 19:29:47 -08:00
parent 55025c0764
commit ffc2fb0eb4
1 changed files with 12 additions and 13 deletions

View File

@ -22,7 +22,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Resource;
@ -30,8 +29,10 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
@ -158,22 +159,19 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
filesystemContainerNameValidator.validate(container);
// check if container exists
// TODO maybe an error is more appropriate
Set<String> blobNames = Sets.newHashSet();
if (!containerExists(container)) {
return new HashSet<String>();
return blobNames;
}
File containerFile = openFolder(container);
final int containerPathLength = containerFile.getAbsolutePath().length() + 1;
Set<String> blobNames = new HashSet<String>() {
private static final long serialVersionUID = 3152191346558570795L;
populateBlobKeysInContainer(containerFile, blobNames, new Function<String, String>() {
@Override
public boolean add(String e) {
return super.add(e.substring(containerPathLength));
public String apply(String string) {
return string.substring(containerPathLength);
}
};
populateBlobKeysInContainer(containerFile, blobNames);
});
return blobNames;
}
@ -433,13 +431,14 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
return folder;
}
private void populateBlobKeysInContainer(File directory, Set<String> blobNames) {
private static void populateBlobKeysInContainer(File directory, Set<String> blobNames,
Function<String, String> function) {
File[] children = directory.listFiles();
for (File child : children) {
if (child.isFile()) {
blobNames.add(child.getAbsolutePath());
blobNames.add(function.apply(child.getAbsolutePath()));
} else if (child.isDirectory()) {
populateBlobKeysInContainer(child, blobNames);
populateBlobKeysInContainer(child, blobNames, function);
}
}
}