// Path: src/main/java/org/killbill/billing/plugin/simpletax/util/ConcurrentLazyValue.java
// public abstract class ConcurrentLazyValue<T> extends LazyValue<T> {
// 
//     /** Whether the value is initialized. */
//     private boolean initialized = false;
// 
//     /** Stores the managed object. */
//     private T value;
// 
//     /**
//      * Returns the value wrapped by this instance, initializing it on first
//      * access. Subsequent access return the cached value.
//      *
//      * @return The object initialized by this {@code LazyValue}.
//      */
//     @Override
//     public T get() {
//         if (!initialized) {
//             synchronized (this) {
//                 if (!initialized) {
//                     value = initialize();
//                     initialized = true;
//                 }
//             }
//         }
//         return value;
//     }
// 
// }


import com.google.common.collect.Multimap;
import static com.google.common.primitives.Ints.tryParse;
import static java.util.regex.Pattern.compile;
import static org.apache.commons.lang3.StringUtils.substring;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.killbill.billing.plugin.simpletax.util.ConcurrentLazyValue;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMultimap;
/*
 * Copyright 2015 Benjamin Gandon
 *
 * 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.killbill.billing.plugin.simpletax.internal;





/**
 * A validator for VAT Identification Numbers (VATIN), based on the <a
 * href="http://www.braemoor.co.uk/software/vat.shtml">JavaScript EU VAT Number
 * Validation</a>.
 *
 * @author Benjamin Gandon
 */
public class VATINValidator implements Predicate<String> {

    private static final int COUNTRY_PREFIX_LENGTH = 2;
    private static final int VATIN_TAIL_GROUP = 2;

    private static Pair<Pattern, Predicate<String>> pair(Pattern pattern, Predicate<String> validator) {
        return ImmutablePair.of(pattern, validator);
    }

// Path: src/main/java/org/killbill/billing/plugin/simpletax/util/ConcurrentLazyValue.java
// public abstract class ConcurrentLazyValue<T> extends LazyValue<T> {
// 
//     /** Whether the value is initialized. */
//     private boolean initialized = false;
// 
//     /** Stores the managed object. */
//     private T value;
// 
//     /**
//      * Returns the value wrapped by this instance, initializing it on first
//      * access. Subsequent access return the cached value.
//      *
//      * @return The object initialized by this {@code LazyValue}.
//      */
//     @Override
//     public T get() {
//         if (!initialized) {
//             synchronized (this) {
//                 if (!initialized) {
//                     value = initialize();
//                     initialized = true;
//                 }
//             }
//         }
//         return value;
//     }
// 
// }


// Path: src/main/java/org/killbill/billing/plugin/simpletax/internal/VATINValidator.java
import com.google.common.collect.Multimap;
import static com.google.common.primitives.Ints.tryParse;
import static java.util.regex.Pattern.compile;
import static org.apache.commons.lang3.StringUtils.substring;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.killbill.billing.plugin.simpletax.util.ConcurrentLazyValue;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMultimap;

/*
 * Copyright 2015 Benjamin Gandon
 *
 * 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.killbill.billing.plugin.simpletax.internal;





/**
 * A validator for VAT Identification Numbers (VATIN), based on the <a
 * href="http://www.braemoor.co.uk/software/vat.shtml">JavaScript EU VAT Number
 * Validation</a>.
 *
 * @author Benjamin Gandon
 */
public class VATINValidator implements Predicate<String> {

    private static final int COUNTRY_PREFIX_LENGTH = 2;
    private static final int VATIN_TAIL_GROUP = 2;

    private static Pair<Pattern, Predicate<String>> pair(Pattern pattern, Predicate<String> validator) {
        return ImmutablePair.of(pattern, validator);
    }

    private static final Supplier<Multimap<String, Pair<Pattern, Predicate<String>>>> PATTERNS = new ConcurrentLazyValue<Multimap<String, Pair<Pattern, Predicate<String>>>>() {