Define a feature-parent
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / BooleanStringCodec.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.data.impl.codec;
9
10 import com.google.common.annotations.Beta;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.data.api.codec.BooleanCodec;
13 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
14
15 /**
16  * Do not use this class outside of yangtools, its presence does not fall into the API stability contract.
17  */
18 @Beta
19 public final class BooleanStringCodec extends TypeDefinitionAwareCodec<Boolean, BooleanTypeDefinition>
20         implements BooleanCodec<String> {
21     private BooleanStringCodec(final BooleanTypeDefinition typeDef) {
22         super(Boolean.class, typeDef);
23     }
24
25     public static @NonNull BooleanStringCodec from(final BooleanTypeDefinition typeDef) {
26         return new BooleanStringCodec(typeDef);
27     }
28
29     @Override
30     protected Boolean deserializeImpl(final String product) {
31         return switch (product) {
32             case "true" -> Boolean.TRUE;
33             case "false" -> Boolean.FALSE;
34             default -> throw new IllegalArgumentException(
35                 "Invalid value '" + product + "' for boolean type. Allowed values are 'true' and 'false'");
36         };
37     }
38
39     @Override
40     protected String serializeImpl(final Boolean input) {
41         return input.toString();
42     }
43 }