ExceptionSet equals and hash code. MetadataSimple improvements.

This commit is contained in:
Olof Larsson 2016-05-19 17:49:51 +02:00
parent 0c625a1ff4
commit b44750a64c
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
2 changed files with 45 additions and 4 deletions

View File

@ -46,23 +46,37 @@ public class MetadataSimple extends MetadataValueAdapter
// -------------------------------------------- // // -------------------------------------------- //
public static void set(Plugin plugin, Metadatable metadatable, String key, Object value) public static void set(Plugin plugin, Metadatable metadatable, String key, Object value)
{
if (value != null)
{ {
metadatable.setMetadata(key, new MetadataSimple(plugin, value)); metadatable.setMetadata(key, new MetadataSimple(plugin, value));
} }
else
{
metadatable.removeMetadata(key, plugin);
}
}
public static MetadataValue getMeta(Metadatable metadatable, String key) public static MetadataValue getMeta(Metadatable metadatable, String key)
{ {
if (metadatable == null) return null;
List<MetadataValue> metaValues = metadatable.getMetadata(key); List<MetadataValue> metaValues = metadatable.getMetadata(key);
if (metaValues == null) return null; if (metaValues == null) return null;
if (metaValues.size() < 1) return null; if (metaValues.isEmpty()) return null;
return metaValues.get(0); return metaValues.get(0);
} }
public static Object get(Metadatable metadatable, String key) @SuppressWarnings("unchecked")
public static <T> T get(Metadatable metadatable, String key)
{ {
MetadataValue metaValue = getMeta(metadatable, key); MetadataValue metaValue = getMeta(metadatable, key);
if (metaValue == null) return null; if (metaValue == null) return null;
return metaValue.value(); return (T) metaValue.value();
}
public static boolean has(Metadatable metadatable, String key)
{
return get(metadatable, key) != null;
} }
} }

View File

@ -2,10 +2,12 @@ package com.massivecraft.massivecore.collections;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Objects;
import com.massivecraft.massivecore.command.editor.annotation.EditorType; import com.massivecraft.massivecore.command.editor.annotation.EditorType;
import com.massivecraft.massivecore.command.type.container.TypeMassiveTreeSetInsensitive; import com.massivecraft.massivecore.command.type.container.TypeMassiveTreeSetInsensitive;
import com.massivecraft.massivecore.comparator.ComparatorCaseInsensitive; import com.massivecraft.massivecore.comparator.ComparatorCaseInsensitive;
import com.massivecraft.massivecore.util.MUtil;
public class ExceptionSet public class ExceptionSet
{ {
@ -104,4 +106,29 @@ public class ExceptionSet
return ret; return ret;
} }
// -------------------------------------------- //
// EQUALS & HASH CODE
// -------------------------------------------- //
@Override
public boolean equals(Object object)
{
if ( ! (object instanceof ExceptionSet)) return false;
ExceptionSet that = (ExceptionSet)object;
return MUtil.equals(
this.standard, that.standard,
this.exceptions, that.exceptions
);
}
@Override
public int hashCode()
{
return Objects.hash(
this.standard,
this.exceptions
);
}
} }