Do some cleanup for the container editor

This commit is contained in:
BuildTools 2016-02-06 14:37:45 +01:00 committed by Olof Larsson
parent 89500be756
commit 251ff4da75
20 changed files with 113 additions and 135 deletions

View File

@ -545,12 +545,27 @@ public class MassiveCommand
// WITHOUT 4
// Without defaultValue, reqFromConsole, defaultDesc & concat .
// Without defaultValue, reqFromConsole, defaultDesc & concat.
public <T> Parameter<T> addParameter(Type<T> type, String name)
{
return this.addParameter(new Parameter<T>(type, name));
}
// Without defaultValue, name, reqFromConsole & defaultDesc.
public <T> Parameter<T> addParameter(Type<T> type, boolean concatFromHere)
{
return this.addParameter(new Parameter<T>(type), concatFromHere);
}
// Without 5
// Without defaultValue, name, reqFromConsole, defaultDesc & concat.
public <T> Parameter<T> addParameter(Type<T> type)
{
return this.addParameter(new Parameter<T>(type));
}
// -------------------------------------------- //
// PREPROCESS
// -------------------------------------------- //

View File

@ -145,6 +145,12 @@ public class Parameter<T>
this(type, REQUIRED_FROM_CONSOLE_DEFAULT, name, DEFAULT_DESC_DEFAULT);
}
// Without defaultValue, name, reqFromConsole and defaultDesc.
public Parameter(Type<T> type)
{
this(type, REQUIRED_FROM_CONSOLE_DEFAULT, type.getTypeName(), DEFAULT_DESC_DEFAULT);
}
// -------------------------------------------- //
// CONVENIENCE
// -------------------------------------------- //

View File

@ -1,13 +1,14 @@
package com.massivecraft.massivecore.command.editor;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.List;
import java.util.AbstractMap.SimpleEntry;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.requirement.RequirementEditorPropertyCreated;
import com.massivecraft.massivecore.command.type.Type;
import com.massivecraft.massivecore.command.type.TypeNullable;
import com.massivecraft.massivecore.util.ContainerUtil;
import com.massivecraft.massivecore.util.Txt;
public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbstract<O, V>
{
@ -38,8 +39,6 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
@Override
public void perform() throws MassiveException
{
// Type
// Create
V container = this.getProperty().getRaw(this.getObject());
List<Object> elements = this.getValueType().getContainerElementsOrdered(container);
@ -59,17 +58,26 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
}
// After
elements = this.getValueType().getContainerElementsOrdered(elements);
V after = this.getValueType().createNewInstance();
ContainerUtil.addElements(after, elements);
// Order
elements = this.getValueType().getContainerElementsOrdered(after);
ContainerUtil.setElements(after, elements);
// Apply
this.attemptSet(after);
}
@Override
public String createCommandAlias()
{
// Split at uppercase letters
String name = this.getClass().getSimpleName();
name = name.substring("CommandEditContainer".length());
final String[] words = name.split("(?=[A-Z])");
String alias = Txt.implode(words, "");
alias = Txt.lowerCaseFirst(alias);
return alias;
}
// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
@ -77,43 +85,33 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
public abstract void alter(List<Object> elements) throws MassiveException;
// -------------------------------------------- //
// MAP
// PARAMETER
// -------------------------------------------- //
public boolean isCollection()
{
Type<V> type = this.getValueType();
if (type.isContainerCollection())
{
return true;
}
else if (type.isContainerMap())
{
return false;
}
else
{
if (type.isContainerCollection()) return true;
if (type.isContainerMap()) return false;
throw new RuntimeException("Neither Collection nor Map.");
}
}
public void addParametersElement(boolean strict)
{
Type<V> type = this.getValueType();
Type<Object> innerType = type.getInnerType();
Type<Object> innerType = this.getValueInnerType();
if (type.isContainerCollection())
if (this.isCollection())
{
this.addParameter(innerType, innerType.getTypeName(), true);
this.addParameter(innerType, true);
}
else if (type.isContainerMap())
else
{
Type<Object> keyType = innerType.getInnerType(0);
Type<Object> valueType = innerType.getInnerType(1);
if (strict)
{
this.addParameter(keyType, keyType.getTypeName());
this.addParameter(valueType, valueType.getTypeName());
this.addParameter(keyType);
this.addParameter(valueType);
}
else
{
@ -121,10 +119,6 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
this.addParameter(null, TypeNullable.get(valueType, "any", "all"), valueType.getTypeName(), "any");
}
}
else
{
throw new RuntimeException("Neither Collection nor Map.");
}
}
public Object readElement() throws MassiveException
@ -137,7 +131,7 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
{
Object key = this.readArg();
Object value = this.readArg();
return new SimpleEntry<Object, Object>(key, value);
return new SimpleImmutableEntry<Object, Object>(key, value);
}
}

View File

@ -16,7 +16,7 @@ public class CommandEditContainerAdd<O, V> extends CommandEditContainerAbstract<
super(settings, property);
// Aliases
this.setAliases("add", "put");
this.addAliases("put");
// Parameters
this.addParametersElement(true);

View File

@ -14,9 +14,6 @@ public class CommandEditContainerClear<O, V> extends CommandEditContainerAbstrac
{
// Super
super(settings, property);
// Aliases
this.setAliases("clear");
}
// -------------------------------------------- //

View File

@ -16,9 +16,6 @@ public class CommandEditContainerInsert<O, V> extends CommandEditContainerAbstra
// Super
super(settings, property);
// Aliases
this.setAliases("insert");
// Parameters
this.addParameter(TypeInteger.get(), "index");
this.addParametersElement(true);

View File

@ -16,9 +16,6 @@ public class CommandEditContainerMove<O, V> extends CommandEditContainerAbstract
// Super
super(settings, property);
// Aliases
this.setAliases("move");
// Parameters
this.addParameter(TypeInteger.get(), "indexFrom");
this.addParameter(TypeInteger.get(), "indexTo");

View File

@ -18,9 +18,6 @@ public class CommandEditContainerRemove<O, V> extends CommandEditContainerAbstra
// Super
super(settings, property);
// Aliases
this.setAliases("remove");
// Parameters
this.addParametersElement(false);
}
@ -52,12 +49,11 @@ public class CommandEditContainerRemove<O, V> extends CommandEditContainerAbstra
Object element = this.readElement();
// Alter
Iterator<Object> iterator = elements.iterator();
while (iterator.hasNext())
for (Iterator<?> it = elements.iterator(); it.hasNext();)
{
Object other = iterator.hasNext();
Object other = it.next();
if ( ! this.getValueInnerType().equals(other, element)) continue;
iterator.remove();
it.remove();
}
}
@ -76,15 +72,14 @@ public class CommandEditContainerRemove<O, V> extends CommandEditContainerAbstra
if (key == null && value == null) throw new MassiveException().addMsg("<b>Please supply key and/or value.");
// Alter
Iterator<Object> iterator = elements.iterator();
while (iterator.hasNext())
for (Iterator<?> it = elements.iterator(); it.hasNext();)
{
Entry<Object, Object> other = (Entry<Object, Object>) iterator.next();
Entry<Object, Object> other = (Entry<Object, Object>) it.next();
if (key != null && ! MUtil.equals(key, other.getKey())) continue;
if (value != null && ! MUtil.equals(value, other.getValue())) continue;
iterator.remove();
it.remove();
}
}

View File

@ -16,9 +16,6 @@ public class CommandEditContainerRemoveIndex<O, V> extends CommandEditContainerA
// Super
super(settings, property);
// Aliases
this.setAliases("removeIndex");
// Parameters
this.addParameter(TypeInteger.get(), "index");
}

View File

@ -16,9 +16,6 @@ public class CommandEditContainerSet<O, V> extends CommandEditContainerAbstract<
// Super
super(settings, property);
// Aliases
this.setAliases("set");
// Parameters
this.addParameter(TypeInteger.get(), "index");
this.addParametersElement(true);

View File

@ -1,5 +1,6 @@
package com.massivecraft.massivecore.command.editor;
import java.util.Collections;
import java.util.List;
import com.massivecraft.massivecore.MassiveException;
@ -16,9 +17,6 @@ public class CommandEditContainerSwap<O, V> extends CommandEditContainerAbstract
// Super
super(settings, property);
// Aliases
this.setAliases("swap");
// Parameters
this.addParameter(TypeInteger.get(), "indexOne");
this.addParameter(TypeInteger.get(), "indexTwo");
@ -36,10 +34,7 @@ public class CommandEditContainerSwap<O, V> extends CommandEditContainerAbstract
int indexTwo = this.readArg();
// Alter
Object elementOne = elements.get(indexOne);
Object elementTwo = elements.get(indexTwo);
elements.set(indexOne, elementTwo);
elements.set(indexTwo, elementOne);
Collections.swap(elements, indexOne, indexTwo);
}
}

View File

@ -25,9 +25,9 @@ public interface Type<T>
// INNER
// -------------------------------------------- //
public <I extends Type<? extends Object>> List<I> getInnerTypes();
public <I extends Type<? extends Object>> I getInnerType(int index);
public <I extends Type<? extends Object>> I getInnerType();
public <I extends Type<?>> List<I> getInnerTypes();
public <I extends Type<?>> I getInnerType(int index);
public <I extends Type<?>> I getInnerType();
public void setInnerTypes(Collection<Type<?>> innerTypes);
public void setInnerTypes(Type<?>... innerTypes);

View File

@ -60,13 +60,13 @@ public abstract class TypeAbstract<T> implements Type<T>
// INNER
// -------------------------------------------- //
protected List<Type<Object>> innerTypes = new MassiveList<Type<Object>>();
protected List<Type<?>> innerTypes = new MassiveList<>();
@SuppressWarnings("unchecked")
public <I extends Type<? extends Object>> List<I> getInnerTypes() { return (List<I>) this.innerTypes; }
public <I extends Type<?>> List<I> getInnerTypes() { return (List<I>) this.innerTypes; }
@SuppressWarnings("unchecked")
public <I extends Type<? extends Object>> I getInnerType(int index) { return (I) this.getInnerTypes().get(index); }
public <I extends Type<? extends Object>> I getInnerType() { return this.getInnerType(0); }
public <I extends Type<?>> I getInnerType(int index) { return (I) this.getInnerTypes().get(index); }
public <I extends Type<?>> I getInnerType() { return this.getInnerType(0); }
@SuppressWarnings({ "unchecked", "rawtypes" })
public void setInnerTypes(Collection<Type<?>> innerTypes) { this.innerTypes = new MassiveList(innerTypes); }

View File

@ -1,15 +1,21 @@
package com.massivecraft.massivecore.command.type.combined;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.AbstractMap.SimpleEntry;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.type.Type;
public class TypeEntry<K, V> extends TypeCombined<Entry<K, V>>
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
private static final Entry<?, ?> ENTRY_EMPTY = new SimpleImmutableEntry<>(null, null);
@SuppressWarnings("unchecked") public static <K, V> Entry<K, V> getEntryEmpty() { return (Entry<K, V>) ENTRY_EMPTY; }
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
@ -41,36 +47,17 @@ public class TypeEntry<K, V> extends TypeCombined<Entry<K, V>>
@Override
public Entry<K, V> combine(List<Object> parts)
{
// Create
K key = null;
V value = null;
if (parts.isEmpty()) return getEntryEmpty();
if (parts.size() == 1) return new SimpleImmutableEntry<>((K)parts.get(0), null);
if (parts.size() == 2) return new SimpleImmutableEntry<>((K)parts.get(0), (V) parts.get(1));
// Fill
for (int i = 0 ; i < parts.size() ; i++)
{
Object part = parts.get(i);
if (i == 0)
{
key = (K)part;
}
else if (i == 1)
{
value = (V)part;
}
}
// Return
return new SimpleEntry<K, V>(key, value);
throw new RuntimeException(parts.size() + " parts");
}
@Override
public List<Object> split(Entry<K, V> entry)
{
return new MassiveList<Object>(
entry.getKey(),
entry.getValue()
);
return Arrays.asList(entry.getKey(), entry.getValue());
}
@Override

View File

@ -40,7 +40,7 @@ public abstract class TypeContainer<C extends Object, E> extends TypeAbstract<C>
public String getCollectionTypeName()
{
return "Container";
return super.getTypeName();
}
// -------------------------------------------- //
@ -111,8 +111,8 @@ public abstract class TypeContainer<C extends Object, E> extends TypeAbstract<C>
List<String> parts = new MassiveList<String>();
// Fill
List<E> elements = this.getContainerElementsOrdered(container);
Type<E> innerType = this.getInnerType();
List<E> elements = this.getContainerElementsOrdered(container);
for (E element : elements)
{
String part = innerType.getIdInner(element);
@ -146,10 +146,9 @@ public abstract class TypeContainer<C extends Object, E> extends TypeAbstract<C>
}
// Fill
String[] elementArgs = Txt.PATTERN_WHITESPACE.split(arg);
for (String elementArg : elementArgs)
{
Type<E> innerType = this.getInnerType();
for (String elementArg : Txt.PATTERN_WHITESPACE.split(arg))
{
E element = innerType.read(elementArg, sender);
ContainerUtil.addElement(ret, element);
}

View File

@ -25,12 +25,6 @@ public class TypeList<E> extends TypeContainer<List<E>, E>
// OVERRIDE
// -------------------------------------------- //
@Override
public String getCollectionTypeName()
{
return "List";
}
@Override
public List<E> createNewInstance()
{

View File

@ -35,12 +35,6 @@ public class TypeMap<K, V> extends TypeContainer<Map<K, V>, Entry<K, V>>
// OVERRIDE
// -------------------------------------------- //
@Override
public String getCollectionTypeName()
{
return "Map";
}
@Override
public Map<K, V> createNewInstance()
{

View File

@ -25,12 +25,6 @@ public class TypeSet<E> extends TypeContainer<Set<E>, E>
// OVERRIDE
// -------------------------------------------- //
@Override
public String getCollectionTypeName()
{
return "Set";
}
@Override
public Set<E> createNewInstance()
{

View File

@ -2,11 +2,12 @@ package com.massivecraft.massivecore.util;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.Map.Entry;
/**
* The ContainerUtil provides an imaginary super class to Collection and Map.
@ -51,12 +52,12 @@ public class ContainerUtil
public static boolean isOrdered(Object container)
{
return container instanceof List<?> || container instanceof LinkedHashMap<?, ?>;
return container instanceof List || container instanceof LinkedHashMap || container instanceof LinkedHashSet;
}
public static boolean isSorted(Object container)
{
return container instanceof SortedSet<?> || container instanceof SortedMap<?, ?>;
return container instanceof SortedSet || container instanceof SortedMap;
}
// -------------------------------------------- //
@ -64,14 +65,14 @@ public class ContainerUtil
// -------------------------------------------- //
@SuppressWarnings("unchecked")
public static <C extends Collection<? extends Object>> C asCollection(Object container)
public static <C extends Collection<?>> C asCollection(Object container)
{
if ( ! isCollection(container)) return null;
return (C)container;
}
@SuppressWarnings("unchecked")
public static <M extends Map<? extends Object, ? extends Object>> M asMap(Object container)
public static <M extends Map<?, ?>> M asMap(Object container)
{
if ( ! isMap(container)) return null;
return (M)container;
@ -83,6 +84,8 @@ public class ContainerUtil
public static boolean isEmpty(Object container)
{
if (container == null) throw new NullPointerException("container");
Collection<Object> collection = asCollection(container);
if (collection != null)
{
@ -95,11 +98,13 @@ public class ContainerUtil
return map.isEmpty();
}
throw new IllegalArgumentException();
throw new IllegalArgumentException(container.getClass().getName() + " is not a container.");
}
public static int size(Object container)
{
if (container == null) throw new NullPointerException("container");
Collection<Object> collection = asCollection(container);
if (collection != null)
{
@ -112,7 +117,7 @@ public class ContainerUtil
return map.size();
}
throw new IllegalArgumentException();
throw new IllegalArgumentException(container.getClass().getName() + " is not a container.");
}
// -------------------------------------------- //
@ -122,6 +127,8 @@ public class ContainerUtil
@SuppressWarnings("unchecked")
public static <E> Collection<E> getElements(Object container)
{
if (container == null) throw new NullPointerException("container");
Collection<E> collection = asCollection(container);
if (collection != null)
{
@ -134,7 +141,7 @@ public class ContainerUtil
return (Collection<E>) map.entrySet();
}
throw new IllegalArgumentException();
throw new IllegalArgumentException(container.getClass().getName() + " is not a container.");
}
// -------------------------------------------- //
@ -143,6 +150,8 @@ public class ContainerUtil
public static void clear(Object container)
{
if (container == null) throw new NullPointerException("container");
Collection<Object> collection = asCollection(container);
if (collection != null)
{
@ -157,7 +166,7 @@ public class ContainerUtil
return;
}
throw new IllegalArgumentException();
throw new IllegalArgumentException(container.getClass().getName() + " is not a container.");
}
public static void setElements(Object container, Iterable<? extends Object> elements)
@ -169,6 +178,8 @@ public class ContainerUtil
@SuppressWarnings("unchecked")
public static boolean addElement(Object container, Object element)
{
if (container == null) throw new NullPointerException("container");
Collection<Object> collection = asCollection(container);
if (collection != null)
{
@ -185,11 +196,14 @@ public class ContainerUtil
return ! MUtil.equals(after, before);
}
throw new IllegalArgumentException();
throw new IllegalArgumentException(container.getClass().getName() + " is not a container.");
}
public static void addElements(Object container, Iterable<? extends Object> elements)
{
if (container == null) throw new NullPointerException("container");
if (elements == null) throw new NullPointerException("elements");
for (Object element : elements)
{
addElement(container, element);

View File

@ -235,6 +235,12 @@ public class Txt
if (string.length() == 0) return string;
return string.substring(0, 1).toUpperCase() + string.substring(1);
}
public static String lowerCaseFirst(String string)
{
if (string == null) return null;
if (string.length() == 0) return string;
return string.substring(0, 1).toLowerCase() + string.substring(1);
}
public static String repeat(String string, int times)
{