/*
 * Copyright 2015 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.gradle.jvm.internal;

import com.google.common.collect.Sets;

import java.util.Set;

/**
 * An immutable representation of a valid Java package name.
 *
 * <p>While {@code JvmPackageName} values are guaranteed to be valid per JLS package
 * naming rules, no validation is performed to ensure that the named package actually
 * exists.</p>
 *
 * <p>Note that due to vagaries inherent in Java naming, a valid package name is also
 * always a valid type name.</p>
 *
 * @see #of(String)
 * @since 2.10
 */
public class JvmPackageName {
    private static final char DELIMITER = '.';

    private static final Set<String> INVALID_PACKAGE_KEYWORDS = Sets.newHashSet(
        "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue",
        "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if",
        "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private",
        "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this",
        "throw", "throws", "transient", "try", "void", "volatile", "while", "_",
        "true", "false",
        "null",
        ""
    );

    private static final String UNNAMED_PACKAGE = "";

    private final String value;

    private JvmPackageName(String value) {
        this.value = value;
    }

    /**
     * The underlying package name value, e.g. "com.example.p1".
     */
    public String getValue() {
        return value;
    }

    /**
     * Validate, create and return a new {@code PackageName} instance from the given
     * package name.
     *
     * <p>See JLS sections:
     * <ul>
     *   <li><em><a href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8">
     *     3.8. Identifiers</a></em></li>
     *   <li><em><a href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.2">
     *     6.2. Names and Identifiers</a></em></li>
     *   <li><em><a href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.4">
     *     7.4. Package Declarations</a></em></li>
     * </ul>
     * </p>
     *
     * @param value the candidate package name, e.g.: {@code com.example.p1} or an empty
     * string indicating an <em>unnamed package</em>
     * @return the validated package name instance, never null
     * @throws IllegalArgumentException if value is null or does not conform to valid
     * package naming per the JLS
     */
    public static JvmPackageName of(String value) {
        if (!isValidPackageName(value)) {
            throw new IllegalArgumentException(String.format("'%s' is not a valid package name", value));
        }
        return new JvmPackageName(value);
    }

    private static boolean isValidPackageName(String value) {
        if (UNNAMED_PACKAGE.equals(value)) {
            return true;
        } else if (value == null) {
            return false;
        } else {
            return fragmentsAreValid(value);
        }
    }

    private static boolean fragmentsAreValid(String value) {
        int i = -1;
        for (int j = value.indexOf(DELIMITER);
             j > i;
             i = j, j = value.indexOf(DELIMITER, i + 1)
        ) {
            String token = value.substring(i + 1, j);
            if (!isIdentifier(token)) {
                return false;
            }
        }
        String lastToken = value.substring(i + 1);
        return isIdentifier(lastToken);
    }

    private static boolean isIdentifier(String token) {
        return !INVALID_PACKAGE_KEYWORDS.contains(token)
            && isValidJavaIdentifier(token);
    }

    private static boolean isValidJavaIdentifier(String token) {
        if (!Character.isJavaIdentifierStart(token.charAt(0))) {
            return false;
        } else {
            for (int i = 1; i < token.length(); i++) {
                if (!Character.isJavaIdentifierPart(token.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        JvmPackageName that = (JvmPackageName) o;
        return value.equals(that.value);
    }

    @Override
    public int hashCode() {
        return value.hashCode();
    }
}

@@ -673,20 +673,68 @@
 mon.
-collect.
+base.Splitter;%0Aimport com.google.common.collect.Immutable
 Set
-s
 ;%0A%0Ai
@@ -1250,16 +1250,95 @@
  = '.';%0A
+    private static Splitter packageFragmentSplitter = Splitter.on(DELIMITER);%0A%0A
 %0A    pri
@@ -1398,23 +1398,23 @@
 S = 
-Sets.newHashSet
+ImmutableSet.of
 (%0A  
@@ -3879,208 +3879,77 @@
     
-int i = -1;%0A        for (int j = value.indexOf(DELIMITER);%0A             j %3E i;%0A             i = j, j = value.indexOf(DELIMITER, i + 1)%0A        ) %7B%0A            String token = value.substring(i + 1, j);
+for (String packageFragment : packageFragmentSplitter.split(value)) %7B
 %0A   
@@ -3975,21 +3975,31 @@
 ntifier(
-tok
+packageFragm
 en
+t
 )) %7B%0A   
@@ -4061,89 +4061,19 @@
     
-String lastToken = value.substring(i + 1);%0A        return isIdentifier(lastToken)
+return true
 ;%0A  

Use `ImmutableSet` for INVALID_PACKAGE_KEYWORDS and `Splitter` for `fragmentsAreValid` in `JvmPackageName`