Editor map fixes

This commit is contained in:
BuildTools 2016-02-23 20:24:15 +01:00 committed by Olof Larsson
parent ee907f1d6a
commit 13c5784454
14 changed files with 160 additions and 44 deletions

View File

@ -23,12 +23,19 @@ public class CommandEditContainer<O, V> extends CommandEditAbstract<O, V>
if (property.isEditable())
{
this.addChild(new CommandEditContainerAdd<O, V>(settings, property));
this.addChild(new CommandEditContainerInsert<O, V>(settings, property));
this.addChild(new CommandEditContainerSet<O, V>(settings, property));
// These are not suitable for maps.
if (property.getValueType().isContainerCollection())
{
this.addChild(new CommandEditContainerInsert<O, V>(settings, property));
this.addChild(new CommandEditContainerSet<O, V>(settings, property));
}
this.addChild(new CommandEditContainerRemove<O, V>(settings, property));
this.addChild(new CommandEditContainerRemoveIndex<O, V>(settings, property));
if ( ! property.getValueType().isContainerSorted() && property.getValueType().getContainerComparator() == null)
// The container must not be sorted, and must be ordered.
if ( ! property.getValueType().isContainerSorted() && property.getValueType().getContainerComparator() == null && property.getValueType().isContainerOrdered())
{
this.addChild(new CommandEditContainerMove<O, V>(settings, property));
this.addChild(new CommandEditContainerSwap<O, V>(settings, property));

View File

@ -46,13 +46,18 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
public void perform() throws MassiveException
{
// Create
V container = this.getProperty().getRaw(this.getObject());
List<Object> elements = this.getValueType().getContainerElementsOrdered(container);
V before = this.getProperty().getRaw(this.getObject());
// We must use a container of the correct type.
// Otherwise the elements list for maps,
// could have two entries with the same key.
// That obviously caused issues.
V container = ContainerUtil.getCopy(before);
// Alter
try
{
this.alter(elements);
this.alter(container);
}
catch (MassiveException e)
{
@ -64,8 +69,8 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
}
// After
elements = this.getValueType().getContainerElementsOrdered(elements);
V after = this.getValueType().createNewInstance();
List<Object> elements = this.getValueType().getContainerElementsOrdered(container);
ContainerUtil.addElements(after, elements);
// Apply
@ -135,8 +140,21 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
// Not actually abstract, but one of these must be implemented;
public abstract void alter(List<Object> elements) throws MassiveException;
public void alter(V container) throws MassiveException
{
List<Object> elements = toElements(container);
this.alterElements(elements);
ContainerUtil.setElements(container, elements);
}
public void alterElements(List<Object> elements) throws MassiveException
{
throw new MassiveException().addMsg("<b>Not yet implemented.");
}
// -------------------------------------------- //
// PARAMETER
@ -160,17 +178,17 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
}
else
{
Type<Object> keyType = innerType.getInnerType(0);
Type<Object> valueType = innerType.getInnerType(1);
Type<Object> keyType = innerType.getInnerType(innerType.getIndexUser(0));
Type<Object> valueType = innerType.getInnerType(innerType.getIndexUser(1));
if (strict)
{
this.addParameter(keyType);
this.addParameter(valueType);
this.addParameter(valueType, true);
}
else
{
this.addParameter(null, TypeNullable.get(keyType, "any", "all"), keyType.getTypeName(), "any");
this.addParameter(null, TypeNullable.get(valueType, "any", "all"), valueType.getTypeName(), "any");
this.addParameter(null, TypeNullable.get(valueType, "any", "all"), valueType.getTypeName(), "any", true);
}
}
}
@ -183,10 +201,18 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
}
else
{
Object key = this.readArg();
Object value = this.readArg();
Object key = this.readArgAt(this.getValueInnerType().getIndexTech(0));
Object value = this.readArgAt(this.getValueInnerType().getIndexTech(1));
return new SimpleImmutableEntry<Object, Object>(key, value);
}
}
// -------------------------------------------- //
// ELEMENTS UTIL
// -------------------------------------------- //
public List<Object> toElements(V container)
{
return this.getValueType().getContainerElementsOrdered(container);
}
}

View File

@ -1,8 +1,7 @@
package com.massivecraft.massivecore.command.editor;
import java.util.List;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.util.ContainerUtil;
public class CommandEditContainerAdd<O, V> extends CommandEditContainerAbstract<O, V>
{
@ -27,13 +26,13 @@ public class CommandEditContainerAdd<O, V> extends CommandEditContainerAbstract<
// -------------------------------------------- //
@Override
public void alter(List<Object> elements) throws MassiveException
public void alter(V container) throws MassiveException
{
// Args
Object element = this.readElement();
// Alter
elements.add(element);
ContainerUtil.addElement(container, element);
}
}

View File

@ -1,8 +1,7 @@
package com.massivecraft.massivecore.command.editor;
import java.util.List;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.util.ContainerUtil;
public class CommandEditContainerClear<O, V> extends CommandEditContainerAbstract<O, V>
{
@ -21,10 +20,10 @@ public class CommandEditContainerClear<O, V> extends CommandEditContainerAbstrac
// -------------------------------------------- //
@Override
public void alter(List<Object> elements) throws MassiveException
public void alter(V container) throws MassiveException
{
// Apply
elements.clear();
ContainerUtil.clear(container);
}
}

View File

@ -26,7 +26,7 @@ public class CommandEditContainerInsert<O, V> extends CommandEditContainerAbstra
// -------------------------------------------- //
@Override
public void alter(List<Object> elements) throws MassiveException
public void alterElements(List<Object> elements) throws MassiveException
{
// Args
int index = this.readArg();

View File

@ -26,7 +26,7 @@ public class CommandEditContainerMove<O, V> extends CommandEditContainerAbstract
// -------------------------------------------- //
@Override
public void alter(List<Object> elements) throws MassiveException
public void alterElements(List<Object> elements) throws MassiveException
{
// Args
int indexFrom = this.readArg();

View File

@ -2,6 +2,7 @@ package com.massivecraft.massivecore.command.editor;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.massivecraft.massivecore.MassiveException;
@ -27,15 +28,15 @@ public class CommandEditContainerRemove<O, V> extends CommandEditContainerAbstra
// -------------------------------------------- //
@Override
public void alter(List<Object> elements) throws MassiveException
public void alter(V container) throws MassiveException
{
if (this.isCollection())
{
this.alterCollection(elements);
this.alterCollection((List<?>) container);
}
else
{
this.alterMap(elements);
this.alterMap((Map<?, ?>)container);
}
}
@ -43,7 +44,7 @@ public class CommandEditContainerRemove<O, V> extends CommandEditContainerAbstra
// OVERRIDE > COLLECTION
// -------------------------------------------- //
public void alterCollection(List<Object> elements) throws MassiveException
public void alterCollection(List<?> elements) throws MassiveException
{
// Args
Object element = this.readElement();
@ -62,7 +63,7 @@ public class CommandEditContainerRemove<O, V> extends CommandEditContainerAbstra
// -------------------------------------------- //
@SuppressWarnings("unchecked")
public void alterMap(List<Object> elements) throws MassiveException
public void alterMap(Map<?, ?> elements) throws MassiveException
{
// Args
Object key = this.readArg();
@ -72,7 +73,7 @@ public class CommandEditContainerRemove<O, V> extends CommandEditContainerAbstra
if (key == null && value == null) throw new MassiveException().addMsg("<b>Please supply key and/or value.");
// Alter
for (Iterator<?> it = elements.iterator(); it.hasNext();)
for (Iterator<?> it = elements.entrySet().iterator(); it.hasNext();)
{
Entry<Object, Object> other = (Entry<Object, Object>) it.next();

View File

@ -25,7 +25,7 @@ public class CommandEditContainerRemoveIndex<O, V> extends CommandEditContainerA
// -------------------------------------------- //
@Override
public void alter(List<Object> elements) throws MassiveException
public void alterElements(List<Object> elements) throws MassiveException
{
// Args
int index = this.readArg();

View File

@ -26,7 +26,7 @@ public class CommandEditContainerSet<O, V> extends CommandEditContainerAbstract<
// -------------------------------------------- //
@Override
public void alter(List<Object> elements) throws MassiveException
public void alterElements(List<Object> elements) throws MassiveException
{
// Args
int index = this.readArg();

View File

@ -27,7 +27,7 @@ public class CommandEditContainerSwap<O, V> extends CommandEditContainerAbstract
// -------------------------------------------- //
@Override
public void alter(List<Object> elements) throws MassiveException
public void alterElements(List<Object> elements) throws MassiveException
{
// Args
int indexOne = this.readArg();

View File

@ -35,6 +35,11 @@ public interface Type<T>
public void setInnerTypes(Type<?>... innerTypes);
public void setInnerType(Type<?> innerType);
public void setUserOrder(List<Integer> userOrder);
public void setUserOrder(Integer... userOrder);
public int getIndexUser(int indexTechy);
public int getIndexTech(int indexUser);
// -------------------------------------------- //
// WRITE VISUAL COLOR
// -------------------------------------------- //

View File

@ -81,6 +81,22 @@ public abstract class TypeAbstract<T> implements Type<T>
public void setInnerTypes(Type<?>... innerTypes) { this.setInnerTypes(Arrays.asList(innerTypes)); };
public void setInnerType(Type<?> innerType) { this.setInnerTypes(innerType); }
private List<Integer> userOrder = null;
@Override public void setUserOrder(List<Integer> userOrder) { this.userOrder = userOrder; }
@Override public void setUserOrder(Integer... userOrder) { this.setUserOrder(Arrays.asList(userOrder)); }
@Override
public int getIndexUser(int indexTechy)
{
if (this.userOrder == null) return indexTechy;
return userOrder.get(indexTechy);
}
@Override
public int getIndexTech(int indexUser)
{
if (this.userOrder == null) return indexUser;
return userOrder.indexOf(indexUser);
}
// -------------------------------------------- //
// WRITE VISUAL COLOR
// -------------------------------------------- //

View File

@ -139,6 +139,7 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
// WRITE VISUAL
// -------------------------------------------- //
@SuppressWarnings("unchecked")
@Override
public String getVisualInner(T value, CommandSender sender)
{
@ -146,9 +147,10 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
List<String> parts = new MassiveList<String>();
// Fill
for (Entry<Type<?>, Object> entry : this.splitEntries(value))
List<Entry<Type<?>, Object>> entries = this.splitEntries(value);
for (int i = 0; i < entries.size(); i++)
{
@SuppressWarnings("unchecked")
Entry<Type<?>, Object> entry = entries.get(this.getIndexTech(i));
Type<Object> type = (Type<Object>) entry.getKey();
String part = type.getVisual(entry.getValue(), sender);
if ( ! this.isVisualNullIncluded() && part == null) continue;
@ -231,7 +233,7 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
for (int i = 0; i < innerArgs.size(); i++)
{
String innerArg = innerArgs.get(i);
Type<?> innerType = this.getInnerTypes().get(i);
Type<?> innerType = this.getInnerTypes().get(getIndexUser(i));
Object part = innerType.read(innerArg, sender);
ret.add(part);
}
@ -287,7 +289,7 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
List<String> args = this.getArgs(string);
if (args.isEmpty()) return null;
if (args.size() > this.getInnerTypes().size()) return null;
return this.getInnerType(args.size() - 1);
return this.getInnerType(getIndexTech(args.size() - 1));
}
// -------------------------------------------- //

View File

@ -5,14 +5,15 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import com.massivecraft.massivecore.collections.MassiveSet;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.collections.MassiveSet;
/**
* The ContainerUtil provides an imaginary super class to Collection and Map.
* In Java they do not have a common interface yet many methods are similar and exists in both.
@ -37,12 +38,22 @@ public class ContainerUtil
public static boolean isCollection(Object container)
{
return container instanceof Collection<?>;
return container instanceof Collection;
}
public static boolean isMap(Object container)
{
return container instanceof Map<?, ?>;
return container instanceof Map;
}
public static boolean isList(Object container)
{
return container instanceof List;
}
public static boolean isSet(Object container)
{
return container instanceof Set;
}
// -------------------------------------------- //
@ -82,6 +93,20 @@ public class ContainerUtil
return (M)container;
}
@SuppressWarnings("unchecked")
public static <S extends Set<?>> S asSet(Object container)
{
if ( ! isSet(container)) return null;
return (S)container;
}
@SuppressWarnings("unchecked")
public static <L extends List<?>> L asList(Object container)
{
if ( ! isList(container)) return null;
return (L)container;
}
// -------------------------------------------- //
// METHODS > SIZE
// -------------------------------------------- //
@ -234,4 +259,40 @@ public class ContainerUtil
return ret;
}
// -------------------------------------------- //
// COPY
// -------------------------------------------- //
// For this method we must make a distinction between list and set.
@SuppressWarnings("unchecked")
public static <V> V getCopy(V container)
{
List<Object> list = asList(container);
if (list != null)
{
return (V) new MassiveList<>(list);
}
Set<Object> set = asSet(container);
if (set != null)
{
return (V) new MassiveSet<>(set);
}
Collection<Object> collection = asCollection(container);
if (collection != null)
{
// Use list as fallback, when neither list nor set.
return (V) new MassiveList<>(collection);
}
Map<Object, Object> map = asMap(container);
if (map != null)
{
return (V) new MassiveMap<>(map);
}
return null;
}
}