mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-15 10:18:01 +00:00
add methods to search for nested folders and create them
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user