/*
 * Copyright 2013 Samppa Saarela
 *
 * 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.javersion.util;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collection;
import java.util.Map;

public class Check {

    public static <T> T notNull(T reference, String fieldName) {
        return notNull$(reference, "%s should not be null", fieldName);
    }

    public static <T> T notNull$(T reference, String messageFormat, Object... args) {
        return checkNotNull(reference, messageFormat, args);
    }

    public static void that(boolean expression, String messageFormat, Object... args) {
        checkArgument(expression, messageFormat, args);
    }
    
    public static <T extends Iterable<?>> T notNullOrEmpty(T reference, String fieldName) {
        return notNullOrEmpty$(reference, "%s shoud not be null or empty. Got %s", fieldName, reference);
    }
    
    public static <T extends Iterable<?>> T notNullOrEmpty$(T reference, String messageFormat, Object... args) {
        checkArgument(!(reference == null || !reference.iterator().hasNext()), messageFormat, args);
        return reference;
    }
    
    public static <T extends Collection<?>> T notNullOrEmpty(T reference, String fieldName) {
        return notNullOrEmpty$(reference, "%s shoud not be null or empty. Got %s", fieldName, reference);
    }
    
    public static <T extends Collection<?>> T notNullOrEmpty$(T reference, String messageFormat, Object... args) {
        checkArgument(!(reference == null || reference.isEmpty()), messageFormat, args);
        return reference;
    }
    
    public static <K, V, T extends Map<K, V>> T notNullOrEmpty(T reference, String fieldName) {
        return notNullOrEmpty$(reference, "%s shoud not be null or empty. Got %s", fieldName, reference);
    }
    
    public static <K, V, T extends Map<K, V>> T notNullOrEmpty$(T reference, String messageFormat, Object... args) {
        checkArgument(!(reference == null || reference.isEmpty()), messageFormat, args);
        return reference;
    }
    
    public static String notNullOrEmpty(String reference, String fieldName) {
        return notNullOrEmpty$(reference, "%s shoud not be null or empty. Got %s", fieldName, reference);
    }
    
    public static String notNullOrEmpty$(String reference, String messageFormat, Object... args) {
        checkArgument(!(reference == null || reference.length() == 0), messageFormat, args);
        return reference;
    }
    
}

@@ -824,24 +824,119 @@
 heck %7B%0A%0A    
+private static final String NOT_NULL_OR_EMPTY_FMT = %22%25s shoud not be null or empty. Got %25s%22;%0A%0A%09
 public stati
@@ -976,32 +976,32 @@
 ng fieldName) %7B%0A
-
         return n
@@ -1510,47 +1510,29 @@
 ce, 
-%22%25s shoud not be null or empty. Got %25s%22
+NOT_NULL_OR_EMPTY_FMT
 , fi
@@ -1954,47 +1954,29 @@
 ce, 
-%22%25s shoud not be null or empty. Got %25s%22
+NOT_NULL_OR_EMPTY_FMT
 , fi
@@ -2390,47 +2390,29 @@
 ce, 
-%22%25s shoud not be null or empty. Got %25s%22
+NOT_NULL_OR_EMPTY_FMT
 , fi
@@ -2810,47 +2810,29 @@
 ce, 
-%22%25s shoud not be null or empty. Got %25s%22
+NOT_NULL_OR_EMPTY_FMT
 , fi

Extract constant for not-null-or-empty message