/*
 * Copyright 2018, 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.code.java;

import io.spine.annotation.Internal;
import io.spine.code.proto.MessageType;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static io.spine.code.java.ClassName.OUTER_CLASS_DELIMITER;
import static io.spine.code.proto.MessageType.VBUILDER_SUFFIX;

/**
 * Obtains a simple class name for a Validating Builder of a message.
 *
 * <p>If a message is top-level, the validating builder name would be concatenation
 * of the message type name and {@link MessageType#VBUILDER_SUFFIX}.
 *
 * <p>For nested messages and messages with an outer class, the name of the validating builder
 * would be concatenation of simple class names of the nesting hierarchy followed with
 * {@link MessageType#VBUILDER_SUFFIX}.
 */
@Internal
public final class VBuilderClassName {

    private final MessageType type;

    /**
     * Obtains the name of a Validating Builder class that corresponds to the passed message.
     *
     * @throws java.lang.IllegalArgumentException
     *         if the passed message type cannot have a validating builder
     */
    public static SimpleClassName of(MessageType type) {
        checkNotNull(type);
        checkArgument(type.isCustom(), "Validating Builder is not available for `%s`", type.name());

        VBuilderClassName name = new VBuilderClassName(type);
        return name.toSimple();
    }

    private VBuilderClassName(MessageType type) {
        this.type = type;
    }

    private SimpleClassName toSimple() {
        ClassName className = type.javaClassName();
        if (type.isTopLevel()) {
            SimpleClassName result = className.toSimple()
                                              .with(VBUILDER_SUFFIX);
            return result;
        }

        // Nested: either with outer class, or with enclosing message, or both.
        String nestedName = ClassName.afterDot(className.value());
        String mergedNames = nestedName.replace(String.valueOf(OUTER_CLASS_DELIMITER), "");
        SimpleClassName result = SimpleClassName.create(mergedNames)
                                                .with(VBUILDER_SUFFIX);
        return result;
    }
}

@@ -2570,24 +2570,62 @@
 oSimple() %7B%0A
+        SimpleClassName topLevelName;%0A
         Clas
@@ -2713,162 +2713,66 @@
     
-SimpleClassName result = className.toSimple()%0A                                              .with(VBUILDER_SUFFIX);%0A            return result;
+topLevelName = className.toSimple();%0A        %7D else %7B
 %0A    
-    %7D%0A%0A
     
@@ -2851,24 +2851,28 @@
 th.%0A        
+  
+  
 String neste
@@ -2918,16 +2918,20 @@
 lue());%0A
+    
         
@@ -3026,30 +3026,24 @@
     
+  
-SimpleClassName result
+  topLevelName
  = S
@@ -3076,16 +3076,17 @@
 edNames)
+;
 %0A       
@@ -3090,93 +3090,59 @@
     
-                                        .with(VBUILDER_SUFFIX);%0A        return result
+%7D%0A        return topLevelName.with(VBUILDER_SUFFIX)
 ;%0A  

Simplify name calc