Populate model/ hierarchy
[yangtools.git] / model / yang-model-ri / src / main / java / org / opendaylight / yangtools / yang / model / ri / type / EnumerationTypeBuilder.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.model.ri.type;
9
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.ImmutableMap.Builder;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
18
19 public final class EnumerationTypeBuilder extends AbstractRestrictedTypeBuilder<EnumTypeDefinition> {
20     private final Builder<String, EnumPair> builder = ImmutableMap.builder();
21
22     EnumerationTypeBuilder(final QName qname) {
23         super(null, qname);
24     }
25
26     EnumerationTypeBuilder(final EnumTypeDefinition baseType, final QName qname) {
27         super(baseType, qname);
28     }
29
30     public EnumerationTypeBuilder addEnum(final @NonNull EnumPair item) {
31         // in case we are dealing with a restricted enumeration type, validate if the enum is a subset of its base type
32         final EnumTypeDefinition base = getBaseType();
33         if (base != null) {
34             validateRestrictedEnum(item, base);
35         }
36
37         builder.put(item.getName(), item);
38         touch();
39         return this;
40     }
41
42     private static void validateRestrictedEnum(final @NonNull EnumPair item, final @NonNull EnumTypeDefinition base) {
43         boolean isASubsetOfBaseEnums = false;
44         for (EnumPair baseTypeEnumPair : base.getValues()) {
45             if (item.getName().equals(baseTypeEnumPair.getName())) {
46                 if (item.getValue() != baseTypeEnumPair.getValue()) {
47                     throw new InvalidEnumDefinitionException(item, "Value of enum '%s' must be the same as the value"
48                             + " of corresponding enum in the base enumeration type %s.", item.getName(),
49                             base.getQName());
50                 }
51                 isASubsetOfBaseEnums = true;
52                 break;
53             }
54         }
55
56         if (!isASubsetOfBaseEnums) {
57             throw new InvalidEnumDefinitionException(item, "Enum '%s' is not a subset of its base enumeration type %s.",
58                     item.getName(), base.getQName());
59         }
60     }
61
62     @Override
63     public EnumTypeDefinition buildType() {
64         final Map<String, EnumPair> map = builder.build();
65         final Map<Integer, EnumPair> positionMap = new HashMap<>();
66
67         for (EnumPair p : map.values()) {
68             final EnumPair conflict = positionMap.put(p.getValue(), p);
69             if (conflict != null) {
70                 throw new InvalidEnumDefinitionException(p, "Enum '%s' conflicts on value with enum ", conflict);
71             }
72         }
73
74         return getBaseType() == null ? new BaseEnumerationType(getQName(), getUnknownSchemaNodes(), map.values())
75                 : new RestrictedEnumerationType(getBaseType(), getQName(), getUnknownSchemaNodes(), map.values());
76     }
77 }