/*
 * Copyright 2015-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.docksidestage.mylasta.di.naming.styling;

import org.dbflute.util.Srl;
import org.lastaflute.di.naming.styling.StylingFreedomInterfaceMapper;

/**
 * @author jflute
 */
public class FortressStylingFreedomInterfaceMapper implements StylingFreedomInterfaceMapper {

    // ===================================================================================
    //                                                                          Definition
    //                                                                          ==========
    private static final String ONIONARC_DOMAIN_REPOSITORY = ".onionarc.domain.repository.";
    private static final String ONIONARC_INFRASTRUCTURE = ".onionarc.infrastructure.";

    private static final String REPOSITORY = "Repository";
    private static final String INFRA_REPOSITORY = "InfraRepository";

    // ===================================================================================
    //                                                                   to Implementation
    //                                                                   =================
    @Override
    public String toImplementationClassName(String interfaceClassName) {
        if (isDomainRepository(interfaceClassName)) {
            return convertToInfraRepository(interfaceClassName);
        }
        return null;
    }

    private boolean isDomainRepository(String interfaceClassName) {
        return determineClassByKeyword(interfaceClassName, ONIONARC_DOMAIN_REPOSITORY, REPOSITORY);
    }

    private String convertToInfraRepository(String name) {
        name = replace(name, ONIONARC_DOMAIN_REPOSITORY, ONIONARC_INFRASTRUCTURE);
        name = replace(name, REPOSITORY, INFRA_REPOSITORY);
        return name;
    }

    // ===================================================================================
    //                                                                        to Interface
    //                                                                        ============
    @Override
    public String toInterfaceClassName(String implementationClassName) {
        if (isInfraRepository(implementationClassName)) {
            return convertToDomainRepository(implementationClassName);
        }
        return null;
    }

    private boolean isInfraRepository(String implementationClassName) {
        return determineClassByKeyword(implementationClassName, ONIONARC_INFRASTRUCTURE, INFRA_REPOSITORY);
    }

    private String convertToDomainRepository(String name) {
        name = replace(name, ONIONARC_INFRASTRUCTURE, ONIONARC_DOMAIN_REPOSITORY);
        name = replace(name, INFRA_REPOSITORY, REPOSITORY);
        return name;
    }

    // ===================================================================================
    //                                                                        Small Helper
    //                                                                        ============
    private boolean determineClassByKeyword(String name, String pkgKeyword, String suffix) {
        return name.contains(pkgKeyword) && name.endsWith(suffix);
    }

    private String replace(String str, String fromStr, String toStr) {
        return Srl.replace(str, fromStr, toStr);
    }
}

@@ -773,16 +773,144 @@
 r;%0A%0A/**%0A
+ * The mapper of freedom interface styling your naming convention. %3Cbr%3E%0A * This class name is specified in lasta_di.properties.%0A
  * @auth
@@ -1283,32 +1283,309 @@
      ==========%0A
+    // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/%0A    // e.g.%0A    //  biz.onionarc.domain.repository.OnionarcSeaRepository (interface)%0A    //  biz.onionarc.infrastructure.OnionarcSeaInfraRepository (implementation)%0A    // _/_/_/_/_/_/_/_/_/_/%0A
     private stat
@@ -1627,24 +1627,28 @@
 OSITORY = %22.
+biz.
 onionarc.dom
@@ -1725,16 +1725,20 @@
 URE = %22.
+biz.
 onionarc
@@ -2055,203 +2055,887 @@
     
-to Implementation%0A    //                                                                   =================%0A    @Override%0A    public String toImplementationClassName(String interfaceClassName) %7B
+      Constructor%0A    //                                                                         ===========%0A    // default constructor is required so don't change constructor argument definition%0A%0A    // ===================================================================================%0A    //                                                                   to Implementation%0A    //                                                                   =================%0A    @Override%0A    public String toImplementationClassName(String interfaceClassName) %7B%0A        // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/%0A        // e.g.%0A        //  from: ...biz.onionarc.domain.repository.OnionarcSeaRepository (interface)%0A        //    to: ...biz.onionarc.infrastructure.OnionarcSeaInfraRepository (implementation)%0A        // _/_/_/_/_/_/_/_/_/_/
 %0A   
@@ -3845,32 +3845,355 @@
 ionClassName) %7B%0A
+        // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/%0A        // e.g.%0A        //  from: ...biz.onionarc.infrastructure.OnionarcSeaInfraRepository (implementation)%0A        //    to: ...biz.onionarc.domain.repository.OnionarcSeaRepository (interface)%0A        // _/_/_/_/_/_/_/_/_/_/%0A
         if (isIn

improve comments