/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.type;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.DescriptorProtos.DescriptorProto;
import com.google.protobuf.Descriptors.Descriptor;

import java.util.Optional;

import static io.spine.type.TypeName.NESTED_TYPE_SEPARATOR;

public final class NestedTypeName {

    private static final Joiner simpleNameJoiner = Joiner.on(NESTED_TYPE_SEPARATOR);
    private static final Joiner underscoreNameJoiner = Joiner.on("_");

    private final ImmutableList<String> names;

    private NestedTypeName(ImmutableList<String> names) {
        this.names = names;
    }

    static NestedTypeName of(Type<?, ?> type) {
        ImmutableList.Builder<String> names = ImmutableList.builder();
        String unqualified = type.descriptor()
                                 .getName();
        names.add(unqualified);
        Optional<Type<Descriptor, DescriptorProto>> parent = type.containingType();
        while (parent.isPresent()) {
            Type<Descriptor, DescriptorProto> containingType = parent.get();
            names.add(containingType.descriptor()
                                    .getName());
            parent = containingType.containingType();
        }
        ImmutableList<String> fullSimpleName = names.build()
                                                    .reverse();
        return new NestedTypeName(fullSimpleName);
    }

    public String value() {
        return simpleNameJoiner.join(names);
    }

    public String joinWithUnderscore() {
        return underscoreNameJoiner.join(names);
    }

    @Override
    public String toString() {
        return value();
    }
}

@@ -1227,16 +1227,55 @@
 criptor;
+%0Aimport io.spine.value.StringTypeValue;
 %0A%0Aimport
@@ -1361,43 +1361,173 @@
 R;%0A%0A
-public final class NestedTypeName %7B
+/**%0A * A simple name of a nested Protobuf type.%0A */%0Apublic final class NestedTypeName extends StringTypeValue %7B%0A%0A    private static final long serialVersionUID = 0L;
 %0A%0A  
@@ -1799,34 +1799,156 @@
     
-this.names = names;%0A    %7D%0A
+super(simpleNameJoiner.join(names));%0A        this.names = names;%0A    %7D%0A%0A    /**%0A     * Obtains the %7B@code NestedTypeName%7D of the given type.%0A     */
 %0A   
@@ -2739,83 +2739,129 @@
     
-public String value() %7B%0A        return simpleNameJoiner.join(names);
+/**%0A     * Obtains the name joined with underscores (%7B@code _%7D), as used in generated code some%0A     * languages.
 %0A    
-%7D%0A
+ */
 %0A   
@@ -2956,83 +2956,7 @@
    %7D
-%0A%0A    @Override%0A    public String toString() %7B%0A        return value();%0A    %7D
 %0A%7D%0A

Add doc