1eba0f84527e016cf4f5b808fd31d801ead06db3
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / EffectiveTypeUtil.java
1 /*
2  * Copyright (c) 2017 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.parser.rfc7950.stmt.type;
9
10 import static com.google.common.base.Verify.verify;
11
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.common.Uint32;
15 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
16 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
17 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
19 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
20 import org.opendaylight.yangtools.yang.model.util.type.BitBuilder;
21 import org.opendaylight.yangtools.yang.model.util.type.EnumPairBuilder;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.enum_.EnumEffectiveStatementImpl;
23
24 @Beta
25 final class EffectiveTypeUtil {
26     private EffectiveTypeUtil() {
27         // Hidden on purpose
28     }
29
30     static @NonNull Bit buildBit(final @NonNull BitEffectiveStatement stmt, final Uint32 effectivePos) {
31         verify(stmt instanceof DocumentedNode.WithStatus);
32         final DocumentedNode.WithStatus bit = (WithStatus) stmt;
33
34         // TODO: code duplication with EnumPairBuilder is indicating we could use a common Builder<?> interface
35         final BitBuilder builder = BitBuilder.create(stmt.argument(), effectivePos).setStatus(bit.getStatus());
36         bit.getDescription().ifPresent(builder::setDescription);
37         bit.getReference().ifPresent(builder::setReference);
38
39         return builder.build();
40     }
41
42     static @NonNull EnumPair buildEnumPair(final @NonNull EnumEffectiveStatementImpl stmt, final int effectiveValue) {
43         final EnumPairBuilder builder = EnumPairBuilder.create(stmt.getName(), effectiveValue)
44                 .setStatus(stmt.getStatus()).setUnknownSchemaNodes(stmt.getUnknownSchemaNodes());
45         stmt.getDescription().ifPresent(builder::setDescription);
46         stmt.getReference().ifPresent(builder::setReference);
47
48         return builder.build();
49     }
50 }