Convert AbstractEnumStatementSupport
[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.WithStatus;
16 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
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
23 @Beta
24 final class EffectiveTypeUtil {
25     private EffectiveTypeUtil() {
26         // Hidden on purpose
27     }
28
29     static @NonNull Bit buildBit(final @NonNull BitEffectiveStatement stmt, final Uint32 effectivePos) {
30         verify(stmt instanceof WithStatus);
31         final WithStatus bit = (WithStatus) stmt;
32
33         // TODO: code duplication with EnumPairBuilder is indicating we could use a common Builder<?> interface
34         final BitBuilder builder = BitBuilder.create(stmt.argument(), effectivePos).setStatus(bit.getStatus());
35         bit.getDescription().ifPresent(builder::setDescription);
36         bit.getReference().ifPresent(builder::setReference);
37
38         return builder.build();
39     }
40
41     static @NonNull EnumPair buildEnumPair(final @NonNull EnumEffectiveStatement stmt, final int effectiveValue) {
42         verify(stmt instanceof WithStatus);
43         final WithStatus node = (WithStatus) stmt;
44
45         final EnumPairBuilder builder = EnumPairBuilder.create(stmt.getDeclared().rawArgument(), effectiveValue)
46                 .setStatus(node.getStatus()).setUnknownSchemaNodes(node.getUnknownSchemaNodes());
47         node.getDescription().ifPresent(builder::setDescription);
48         node.getReference().ifPresent(builder::setReference);
49
50         return builder.build();
51     }
52 }