pl.edu.icm.unity.stdext.attr.EnumAttributeSyntax Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unity-server-std-plugins Show documentation
Show all versions of unity-server-std-plugins Show documentation
Standard plugins which are distributed with the system:
attribute syntaxes, identity types, credentials
/*
* Copyright (c) 2013 ICM Uniwersytet Warszawski All rights reserved.
* See LICENCE.txt file for licensing information.
*/
package pl.edu.icm.unity.stdext.attr;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import pl.edu.icm.unity.Constants;
import pl.edu.icm.unity.engine.api.attributes.AbstractAttributeValueSyntaxFactory;
import pl.edu.icm.unity.exceptions.IllegalAttributeValueException;
/**
* Enumeration attribute syntax. Accepts strings which are from a defined set.
* Case sensitive.
* @author K. Benedyczak
*/
public class EnumAttributeSyntax extends AbstractStringAttributeSyntax
{
public static final String ID = "enumeration";
private Set allowed;
public EnumAttributeSyntax()
{
}
public EnumAttributeSyntax(String... allowed)
{
setAllowed(allowed);
}
public EnumAttributeSyntax(Set allowed)
{
setAllowed(allowed);
}
/**
* Utility: converts string values to enums list
* @param type
* @param vals
* @return
*/
public static > List getEnumValues(Class type, List vals)
{
if (vals == null)
return null;
List ret = new ArrayList();
Map enumVals = new HashMap();
T[] constants = type.getEnumConstants();
for (T label: constants)
enumVals.put(label.name(), label);
for (String val: vals)
ret.add(enumVals.get(val));
return ret;
}
public Set getAllowed()
{
return allowed;
}
public void setAllowed(String... allowed)
{
if (allowed.length == 0)
throw new IllegalArgumentException("At least one enumeration value must be defined");
this.allowed = new HashSet(allowed.length);
for (String allow: allowed)
this.allowed.add(allow);
this.allowed = Collections.unmodifiableSet(this.allowed);
}
public void setAllowed(Set allowed)
{
if (allowed.isEmpty())
throw new IllegalArgumentException("At least one enumeration value must be defined");
this.allowed = new HashSet(allowed.size());
for (String allow: allowed)
this.allowed.add(allow);
this.allowed = Collections.unmodifiableSet(this.allowed);
}
@Override
public JsonNode getSerializedConfiguration()
{
ObjectNode main = Constants.MAPPER.createObjectNode();
ArrayNode allow = main.putArray("allowed");
for (String a: allowed)
allow.add(a);
return main;
}
@Override
public void setSerializedConfiguration(JsonNode jsonN)
{
ArrayNode allow = (ArrayNode) jsonN.get("allowed");
this.allowed = new HashSet(allow.size());
for (int i=0; i
{
public Factory()
{
super(EnumAttributeSyntax.ID, EnumAttributeSyntax::new);
}
}
}