Again refinements in Edm type management
This commit is contained in:
parent
848976bbbc
commit
027e135583
|
@ -25,10 +25,12 @@ import java.util.Collections;
|
|||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.olingo.odata4.client.api.edm.xml.EnumType;
|
||||
import org.apache.olingo.odata4.client.api.edm.xml.Member;
|
||||
import org.apache.olingo.odata4.commons.api.edm.Edm;
|
||||
import org.apache.olingo.odata4.commons.api.edm.EdmEnumType;
|
||||
import org.apache.olingo.odata4.commons.api.edm.EdmException;
|
||||
import org.apache.olingo.odata4.commons.api.edm.EdmMember;
|
||||
import org.apache.olingo.odata4.commons.api.edm.EdmPrimitiveType;
|
||||
import org.apache.olingo.odata4.commons.api.edm.FullQualifiedName;
|
||||
|
@ -37,6 +39,14 @@ import org.apache.olingo.odata4.commons.core.edm.primitivetype.EdmPrimitiveTypeK
|
|||
|
||||
public class EdmEnumTypeImpl extends AbstractEdmEnumType implements EdmEnumType {
|
||||
|
||||
private static final EdmPrimitiveTypeKind[] VALID_UNDERLYING_TYPES = new EdmPrimitiveTypeKind[] {
|
||||
EdmPrimitiveTypeKind.Byte,
|
||||
EdmPrimitiveTypeKind.SByte,
|
||||
EdmPrimitiveTypeKind.Int16,
|
||||
EdmPrimitiveTypeKind.Int32,
|
||||
EdmPrimitiveTypeKind.Int64
|
||||
};
|
||||
|
||||
private final EdmPrimitiveType underlyingType;
|
||||
|
||||
private final List<String> memberNames;
|
||||
|
@ -49,9 +59,11 @@ public class EdmEnumTypeImpl extends AbstractEdmEnumType implements EdmEnumType
|
|||
if (xmlEnumType.getUnderlyingType() == null) {
|
||||
this.underlyingType = EdmPrimitiveTypeKind.Int32.getEdmPrimitiveTypeInstance();
|
||||
} else {
|
||||
this.underlyingType = EdmPrimitiveTypeKind.fromString(
|
||||
xmlEnumType.getUnderlyingType()).getEdmPrimitiveTypeInstance();
|
||||
// TODO: Should we validate that the underlying type is of byte, sbyte, in16, int32 or int64?
|
||||
this.underlyingType = EdmPrimitiveTypeKind.valueOfFQN(xmlEnumType.getUnderlyingType()).
|
||||
getEdmPrimitiveTypeInstance();
|
||||
if (!ArrayUtils.contains(VALID_UNDERLYING_TYPES, this.underlyingType.getKind())) {
|
||||
throw new EdmException("Not allowed as underlying type: " + this.underlyingType.getKind());
|
||||
}
|
||||
}
|
||||
|
||||
final List<? extends Member> xmlMembers = xmlEnumType.getMembers();
|
||||
|
|
|
@ -38,10 +38,9 @@ public class EdmTypeDefinitionImpl extends AbstractEdmTypeDefinition implements
|
|||
|
||||
super(edm, typeDefinitionName);
|
||||
this.typeDefinition = typeDefinition;
|
||||
// TODO: Should we check for edmNamespace in the underlying type name?
|
||||
try {
|
||||
edmPrimitiveTypeInstance = EdmPrimitiveTypeKind.fromString(
|
||||
typeDefinition.getUnderlyingType()).getEdmPrimitiveTypeInstance();
|
||||
edmPrimitiveTypeInstance = EdmPrimitiveTypeKind.valueOfFQN(typeDefinition.getUnderlyingType()).
|
||||
getEdmPrimitiveTypeInstance();
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new EdmException("Invalid underlying type: " + typeDefinition.getUnderlyingType(), e);
|
||||
}
|
||||
|
|
|
@ -86,13 +86,10 @@ public enum EdmPrimitiveTypeKind {
|
|||
* @param value string value type.
|
||||
* @return <tt>EdmPrimitiveTypeKind</tt> object.
|
||||
*/
|
||||
public static EdmPrimitiveTypeKind fromString(final String value) {
|
||||
final String noNsValue = value.substring(4);
|
||||
for (EdmPrimitiveTypeKind edmSimpleType : EdmPrimitiveTypeKind.values()) {
|
||||
if (edmSimpleType.name().equals(noNsValue)) {
|
||||
return edmSimpleType;
|
||||
}
|
||||
public static EdmPrimitiveTypeKind valueOfFQN(final String value) {
|
||||
if (!value.startsWith(EdmPrimitiveType.EDM_NAMESPACE + ".")) {
|
||||
throw new IllegalArgumentException(value + " does not look like an Edm primitive type");
|
||||
}
|
||||
throw new IllegalArgumentException(value);
|
||||
return valueOf(value.substring(4));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue