public OneOfAttributeBuilder addAlternative(final Attribute newAttribute) { if (newAttribute != null) { if (result.getAlternativeByTypeQulifier(newAttribute.getType()) != null) { throw new IllegalArgumentException("Cannot add more than one oneOf alternative Attribute with the same Type."); } result.alternatives.add(newAttribute); } return this; }
@Override public String toString() { return getQualifier() + ":" + getType(); }
@Override public Map<String, ?> resolve(final Attribute attribute) { final ImmutableMap.Builder<String, Object> attributeMeta = ImmutableMap.builder(); if (attribute.getType() instanceof Enumeration) { final Enumeration enumValues = (Enumeration) attribute.getType(); attributeMeta.put("enum", SchemaJSONUtils.processEnumValues(enumValues)); } else if (attribute.getType() instanceof Dto) { final Dto attributeType = (Dto) attribute.getType(); attributeMeta.put("$ref", attributeType.getSchemaName()); } else { attributeMeta.put("type", SchemaJSONUtils.resolveJSONPrimitiveType(attribute)); } if (attribute.getDocumentation() != null) { attributeMeta.put("description", attribute.getDocumentation()); } return attributeMeta.build(); } }
public static String resolveJSONPrimitiveType(final Attribute attr) { final Type type = attr.getType(); // TODO adjust it for all JSON types if (String.class.getSimpleName().equalsIgnoreCase(type.getQualifier())) { return type.getQualifier().toLowerCase(); } else if (Integer.class.getSimpleName().equalsIgnoreCase(type.getQualifier())) { return type.getQualifier().toLowerCase(); } else if (Object.class.getSimpleName().equalsIgnoreCase(type.getQualifier())) { return type.getQualifier().toLowerCase(); } return type.getQualifier(); }
/** * Convenience method that retrieves the alternative with a given {@code Type}. * * @see #getAlternatives() */ public Attribute getAlternativeByTypeQulifier(final Type alternativeType) { for (final Attribute attr : getAlternatives()) { if (attr.getType().getQualifier().equals(alternativeType.getQualifier())) { return attr; } } return null; }