/*
 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
 *
 * 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.
 */

/*
 * This file was derived from com.google.common.base.Preconditions
 * https://github.com/google/guava/blob/v18.0/guava/src/com/google/common/base/Preconditions.java
 *
 * The original file is
 * Copyright (C) 2007 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 *
 * The modifications to the original file are
 *  - used only a subset of check* methods
 *  - throw BadRequestException in all check* methods
 *  - modify code style and variable names
 */

package com.palantir.remoting.http.server;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import javax.annotation.Nullable;
import javax.ws.rs.BadRequestException;

/**
 * Wrapper around some of Guava's {@link Preconditions} to make its exceptions more http-friendly exceptions. It
 * includes other useful web-oriented checks like {@link #checkNotEmpty(String)} and excludes less relevant ones (like
 * {@link Preconditions#checkState(boolean)}.
 */
public final class WebPreconditions {

    private WebPreconditions() {}

    /**
     * See {@link Preconditions#checkArgument(boolean, Object)}.
     *
     * @throws BadRequestException
     *         when <code>expression</code> is false
     */
    public static void checkArgument(boolean expression, @Nullable Object message) {
        if (!expression) {
            throw new BadRequestException(String.valueOf(message));
        }
    }

    /**
     * See {@link Preconditions#checkArgument(boolean, String, Object...)}.
     *
     * @throws BadRequestException
     *         when <code>expression</code> is false
     */
    public static void checkArgument(boolean expression, @Nullable String messageTemplate,
            @Nullable Object... messageArgs) {
        if (!expression) {
            throw new BadRequestException(format(messageTemplate, messageArgs));
        }
    }

    /**
     * See {@link Preconditions#checkArgument(boolean, Object)} and {@link Strings#isNullOrEmpty(String)}.
     *
     * @throws BadRequestException
     *         when <code>reference</code> is null or empty
     */
    public static String checkNotEmpty(String string, @Nullable Object message) {
        checkArgument(!Strings.isNullOrEmpty(string), message);

        return string;
    }

    /**
     * See {@link Preconditions#checkArgument(boolean, String, Object...)} and {@link Strings#isNullOrEmpty(String)}.
     *
     * @throws BadRequestException
     *         when <code>reference</code> is null or empty
     */
    public static String checkNotEmpty(String string, @Nullable String messageTemplate,
            @Nullable Object... messageArgs) {
        checkArgument(!Strings.isNullOrEmpty(string), messageTemplate, messageArgs);

        return string;
    }

    /**
     * See {@link Preconditions#checkNotNull(Object, Object)}.
     *
     * @throws BadRequestException
     *         when <code>reference</code> is null
     */
    public static <T> T checkNotNull(T reference, @Nullable Object message) {
        checkArgument(reference != null, message);

        return reference;
    }

    /**
     * See {@link Preconditions#checkNotNull(Object, Object)}.
     *
     * @throws BadRequestException
     *         when <code>reference</code> is null
     */
    public static <T> T checkNotNull(T reference, @Nullable String messageTemplate, @Nullable Object... messageArgs) {
        checkArgument(reference != null, messageTemplate, messageArgs);

        return reference;
    }

    /*
     * Taken almost verbatim from Guava's Preconditions#format(String, Object...) modulo some variable name
     * tweaks to make checkstyle happy.
     */
    private static String format(String template, @Nullable Object... args) {
        String nonNullTemplate = String.valueOf(template); // null -> "null"

        // start substituting the arguments into the '%s' placeholders
        StringBuilder builder = new StringBuilder(nonNullTemplate.length() + 16 * args.length);
        int templateStart = 0;
        int index = 0;
        while (index < args.length) {
            int placeholderStart = nonNullTemplate.indexOf("%s", templateStart);
            if (placeholderStart == -1) {
                break;
            }
            builder.append(nonNullTemplate.substring(templateStart, placeholderStart));
            builder.append(args[index++]);
            templateStart = placeholderStart + 2;
        }
        builder.append(nonNullTemplate.substring(templateStart));

        // if we run out of placeholders, append the extra args in square braces
        if (index < args.length) {
            builder.append(" [");
            builder.append(args[index++]);
            while (index < args.length) {
                builder.append(", ");
                builder.append(args[index++]);
            }
            builder.append(']');
        }

        return builder.toString();
    }
}

@@ -1522,16 +1522,24 @@
 y(String
+, Object
 )%7D and e
@@ -1568,17 +1568,17 @@
 ones
+%0A *
  (like
-%0A *
  %7B@l

Fix Javadoc so that it is valid