add methods to search for nested folders and create them

This commit is contained in:
Maxmtg
2014-01-22 04:22:31 +00:00
parent d5647c9e98
commit 8b8b9f0cf5
3 changed files with 49 additions and 9 deletions

View File

@@ -29,21 +29,14 @@ import forge.util.IHasName;
* @param <T> the generic type
*/
public interface IStorage<T> extends Iterable<T>, IHasName {
T get(final String name);
T find(final Predicate<T> condition);
// todo: find(final Predicate<T> condition, boolean recursive).
Collection<String> getItemNames();
boolean contains(final String name);
int size();
void add(final T deck);
void delete(final String deckName);
IStorage<IStorage<T>> getFolders();
IStorage<T> tryGetFolder(String path);
IStorage<T> getFolderOrCreate(String path);
}

View File

@@ -108,4 +108,14 @@ public class StorageBase<T> implements IStorage<T> {
// TODO Auto-generated method stub
return name;
}
@Override
public IStorage<T> tryGetFolder(String path) {
throw new UnsupportedOperationException("This storage does not support subfolders");
}
@Override
public IStorage<T> getFolderOrCreate(String path) {
throw new UnsupportedOperationException("This storage does not support subfolders");
}
}

View File

@@ -22,6 +22,7 @@ import java.io.File;
import com.google.common.base.Function;
import forge.util.IItemSerializer;
import forge.util.TextUtil;
/**
* <p>
@@ -89,4 +90,40 @@ public class StorageImmediatelySerialized<T> extends StorageBase<T> {
public IStorage<IStorage<T>> getFolders() {
return subfolders == null ? super.getFolders() : subfolders;
}
@Override
public IStorage<T> tryGetFolder(String path) {
String[] parts = TextUtil.split(path, '/', 2);
switch( parts.length ) {
case 0: return this;
case 1: return parts[0].equals(".") ? this : getFolders().get(parts[0]);
case 2:
IStorage<T> subFolder = getFolders().get(parts[0]);
return subFolder == null ? null : subFolder.tryGetFolder(parts[1]);
}
// should not reach this unless split is broken
throw new IllegalArgumentException(path);
}
@Override
public IStorage<T> getFolderOrCreate(String path) {
String[] parts = TextUtil.split(path, '/', 2);
switch( parts.length ) {
case 0: return this;
case 1: return parts[0].equals(".") ? this : getOrCreateSubfolder(parts[0]);
case 2: return getOrCreateSubfolder(parts[0]).getFolderOrCreate(parts[1]);
}
// should not reach this unless split is broken
throw new IllegalArgumentException(path);
}
private IStorage<T> getOrCreateSubfolder(String name) {
// Have to filter name for incorrect symbols
IStorage<T> storage = getFolders().get(name);
if( null == storage ) {
storage = new StorageImmediatelySerialized<>(name, serializer);
subfolders.add(storage);
}
return storage;
}
}