/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.data.impl.codec;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.Beta;
import com.google.common.base.Functions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.data.api.codec.EnumCodec;
import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;

/**
 * Do not use this class outside of yangtools, its presence does not fall into the API stability contract.
 */
@Beta
public final class EnumStringCodec extends TypeDefinitionAwareCodec<String, EnumTypeDefinition>
        implements EnumCodec<String> {
    private final ImmutableMap<String, String> values;

    private EnumStringCodec(final @NonNull EnumTypeDefinition typeDef) {
        super(typeDef, String.class);

        values = Maps.uniqueIndex(typeDef.getValues().stream()
            // Intern the String to get wide reuse
            .map(pair -> pair.getName().intern())
            .collect(Collectors.toList()), Functions.identity());
    }

    public static EnumStringCodec from(final EnumTypeDefinition normalizedType) {
        return new EnumStringCodec(requireNonNull(normalizedType));
    }

    @Override
    protected String deserializeImpl(final String product) {
        // Lookup the serialized string in the values. Returned string is the interned instance, which we want
        // to use as the result.
        final String result = values.get(product);
        checkArgument(result != null, "Invalid value '%s' for enum type. Allowed values are: %s", product,
                values.keySet());
        return result;
    }

    @Override
    protected String serializeImpl(final String input) {
        return input;
    }
}

@@ -609,83 +609,8 @@
 ap;%0A
-import com.google.common.collect.Maps;%0Aimport java.util.stream.Collectors;%0A
 impo
@@ -1211,17 +1211,16 @@
 class);%0A
-%0A
         
@@ -1232,25 +1232,8 @@
 s = 
-Maps.uniqueIndex(
 type
@@ -1269,16 +1269,20 @@
         
+ 
+   
 // Inter
@@ -1324,16 +1324,20 @@
         
+    
 .map(pai
@@ -1378,16 +1378,20 @@
         
+  
+  
 .collect
@@ -1395,27 +1395,55 @@
 ect(
-Collectors.toList()
+ImmutableMap.toImmutableMap(Functions.identity(
 ), F
@@ -1462,16 +1462,17 @@
 ntity())
+)
 ;%0A    %7D%0A

Use ImmutableMap collector in EnumStringCodec