static Callable<EnumerationCodec> loader(final Class<?> returnType, final EnumTypeDefinition enumSchema) { Preconditions.checkArgument(Enum.class.isAssignableFrom(returnType)); @SuppressWarnings({ "rawtypes", "unchecked" }) final Class<? extends Enum<?>> enumType = (Class) returnType; return () -> { final Map<String, Enum<?>> nameToValue = new HashMap<>(); for (final Enum<?> enumValue : enumType.getEnumConstants()) { nameToValue.put(enumValue.toString(), enumValue); } final Map<String, Enum<?>> yangNameToBinding = new HashMap<>(); for (final EnumPair yangValue : enumSchema.getValues()) { final String bindingName = JavaIdentifierNormalizer.normalizeSpecificIdentifier(yangValue.getName(), JavaIdentifier.ENUM_VALUE); final Enum<?> bindingVal = nameToValue.get(bindingName); yangNameToBinding.put(yangValue.getName(), bindingVal); } return new EnumerationCodec(enumType, yangNameToBinding); }; }
@Override public EnumTypeDefinition buildType() { final Map<String, EnumPair> map = builder.build(); final Map<Integer, EnumPair> positionMap = new HashMap<>(); for (EnumPair p : map.values()) { final EnumPair conflict = positionMap.put(p.getValue(), p); if (conflict != null) { throw new InvalidEnumDefinitionException(p, "Enum '%s' conflicts on value with enum ", conflict); } } return getBaseType() == null ? new BaseEnumerationType(getPath(), getUnknownSchemaNodes(), map.values()) : new RestrictedEnumerationType(getBaseType(), getPath(), getUnknownSchemaNodes(), map.values()); } }
@Override public final void updateEnumPairsFromEnumTypeDef(final EnumTypeDefinition enumTypeDef) { final List<EnumPair> enums = enumTypeDef.getValues(); final Map<String, String> valueIds = BindingMapping.mapEnumAssignedNames(enums.stream().map(EnumPair::getName) .collect(Collectors.toList())); for (EnumPair enumPair : enums) { addValue(enumPair.getName(), valueIds.get(enumPair.getName()), enumPair.getValue(), enumPair.getStatus(), enumPair.getDescription().orElse(null), enumPair.getReference().orElse(null)); } }
public static CharSequence writeEnumPair(final EnumTypeDefinition.EnumPair pair) { CharSequence _xblockexpression = null; { Integer _value = pair.getValue(); boolean hasEnumPairValue = (!Objects.equal(_value, null)); StringConcatenation _builder = new StringConcatenation(); _builder.append("enum "); String _name = pair.getName(); _builder.append(_name, ""); { if ((!hasEnumPairValue)) { _builder.append(";"); } else { _builder.append("{"); _builder.newLineIfNotEmpty(); _builder.append(" "); _builder.append("value "); Integer _value_1 = pair.getValue(); _builder.append(_value_1, " "); _builder.append(";"); _builder.newLineIfNotEmpty(); _builder.append("}"); _builder.newLine(); } } _xblockexpression = _builder; } return _xblockexpression; }
private static void validateRestrictedEnum(final @NonNull EnumPair item, final @NonNull EnumTypeDefinition base) { boolean isASubsetOfBaseEnums = false; for (EnumPair baseTypeEnumPair : base.getValues()) { if (item.getName().equals(baseTypeEnumPair.getName())) { if (item.getValue() != baseTypeEnumPair.getValue()) { throw new InvalidEnumDefinitionException(item, "Value of enum '%s' must be the same as the value" + " of corresponding enum in the base enumeration type %s.", item.getName(), base.getQName()); } isASubsetOfBaseEnums = true; break; } } if (!isASubsetOfBaseEnums) { throw new InvalidEnumDefinitionException(item, "Enum '%s' is not a subset of its base enumeration type %s.", item.getName(), base.getQName()); } }
private static void validateRestrictedEnum(final @NonNull EnumPair item, final @NonNull EnumTypeDefinition base) { boolean isASubsetOfBaseEnums = false; for (EnumPair baseTypeEnumPair : base.getValues()) { if (item.getName().equals(baseTypeEnumPair.getName())) { if (item.getValue() != baseTypeEnumPair.getValue()) { throw new InvalidEnumDefinitionException(item, "Value of enum '%s' must be the same as the value" + " of corresponding enum in the base enumeration type %s.", item.getName(), base.getQName()); } isASubsetOfBaseEnums = true; break; } } if (!isASubsetOfBaseEnums) { throw new InvalidEnumDefinitionException(item, "Enum '%s' is not a subset of its base enumeration type %s.", item.getName(), base.getQName()); } }
private BiMap<String, String> getEnumMapping(final Entry<GeneratedType, Object> typeWithSchema) { final TypeDefinition<?> typeDef = (TypeDefinition<?>) typeWithSchema.getValue(); final EnumerationType enumType; if(typeDef instanceof ExtendedType) { enumType = (EnumerationType) ((ExtendedType) typeDef).getBaseType(); } else { Preconditions.checkArgument(typeDef instanceof EnumerationType); enumType = (EnumerationType) typeDef; } final HashBiMap<String, String> mappedEnums = HashBiMap.create(); for (final EnumTypeDefinition.EnumPair enumPair : enumType.getValues()) { mappedEnums.put(enumPair.getName(), BindingMapping.getClassName(enumPair.getName())); } // TODO cache these maps for future use return mappedEnums; }
@Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (!(obj instanceof EnumPair)) { return false; } EnumPair other = (EnumPair) obj; if (!Objects.equals(name, other.getName())) { return false; } return value == other.getValue() && Objects.equals(unknownSchemaNodes, other.getUnknownSchemaNodes()); }
private EnumStringCodec(final Optional<EnumTypeDefinition> typeDef) { super(typeDef, String.class); if (typeDef.isPresent()) { final Builder<String, String> b = ImmutableMap.builder(); for (final EnumPair pair : typeDef.get().getValues()) { // Intern the String to get wide reuse final String v = pair.getName().intern(); b.put(v, v); } values = b.build(); } else { values = null; } }
@Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (!(obj instanceof EnumPair)) { return false; } EnumPair other = (EnumPair) obj; if (!Objects.equals(name, other.getName())) { return false; } return value == other.getValue() && Objects.equals(unknownSchemaNodes, other.getUnknownSchemaNodes()); }
@Override public EnumTypeDefinition buildType() { final Map<String, EnumPair> map = builder.build(); final Map<Integer, EnumPair> positionMap = new HashMap<>(); for (EnumPair p : map.values()) { final EnumPair conflict = positionMap.put(p.getValue(), p); if (conflict != null) { throw new InvalidEnumDefinitionException(p, "Enum '%s' conflicts on value with enum ", conflict); } } return getBaseType() == null ? new BaseEnumerationType(getPath(), getUnknownSchemaNodes(), map.values()) : new RestrictedEnumerationType(getBaseType(), getPath(), getUnknownSchemaNodes(), map.values()); } }
private EnumStringCodec(final Optional<EnumTypeDefinition> typeDef) { super(typeDef, String.class); if (typeDef.isPresent()) { final Builder<String, String> b = ImmutableMap.builder(); for (final EnumPair pair : typeDef.get().getValues()) { // Intern the String to get wide reuse final String v = pair.getName().intern(); b.put(v, v); } values = b.build(); } else { values = null; } }
private static int getEnumValue(final EnumTypeDefinition enumerationType, final String enumName) { for (final EnumTypeDefinition.EnumPair enumPair : enumerationType.getValues()) { if (enumName.equals(enumPair.getName())) { return enumPair.getValue(); } } throw new IllegalStateException(String.format("Enum %s does not belong to enumeration %s.", enumName, enumerationType)); }
private static BiMap<String, String> getEnumMapping(final Entry<GeneratedType, Object> typeWithSchema) { final TypeDefinition<?> typeDef = (TypeDefinition<?>) typeWithSchema.getValue(); Preconditions.checkArgument(typeDef instanceof EnumTypeDefinition); final EnumTypeDefinition enumType = (EnumTypeDefinition) typeDef; final HashBiMap<String, String> mappedEnums = HashBiMap.create(); for (final EnumTypeDefinition.EnumPair enumPair : enumType.getValues()) { mappedEnums.put(enumPair.getName(), JavaIdentifierNormalizer.normalizeSpecificIdentifier(enumPair.getName(), JavaIdentifier.CLASS)); } // TODO cache these maps for future use return mappedEnums; }
private static void processEnumType(final EnumTypeDefinition enumLeafType, final JSONObject property) throws JSONException { List<EnumPair> enumPairs = enumLeafType.getValues(); List<String> enumNames = new ArrayList<>(); for (EnumPair enumPair : enumPairs) { enumNames.add(enumPair.getName()); } property.putOpt(ENUM, new JSONArray(enumNames)); }
public EnumerationTypeBuilder addEnum(final @NonNull EnumPair item) { // in case we are dealing with a restricted enumeration type, validate if the enum is a subset of its base type final EnumTypeDefinition base = getBaseType(); if (base != null) { validateRestrictedEnum(item, base); } builder.put(item.getName(), item); touch(); return this; }
public EnumerationTypeBuilder addEnum(final @NonNull EnumPair item) { // in case we are dealing with a restricted enumeration type, validate if the enum is a subset of its base type final EnumTypeDefinition base = getBaseType(); if (base != null) { validateRestrictedEnum(item, base); } builder.put(item.getName(), item); touch(); return this; }
private static BiMap<String, String> getEnumMapping(final Entry<GeneratedType, WithStatus> typeWithSchema) { final TypeDefinition<?> typeDef = (TypeDefinition<?>) typeWithSchema.getValue(); Preconditions.checkArgument(typeDef instanceof EnumTypeDefinition); final EnumTypeDefinition enumType = (EnumTypeDefinition) typeDef; final HashBiMap<String, String> mappedEnums = HashBiMap.create(); for (final EnumTypeDefinition.EnumPair enumPair : enumType.getValues()) { mappedEnums.put(enumPair.getName(), BindingMapping.getClassName(enumPair.getName())); } // TODO cache these maps for future use return mappedEnums; }