/*
 * Copyright 2015-2018 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v2.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.engine.config;

import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.platform.commons.util.StringUtils;
import org.junit.platform.engine.ConfigurationParameters;

class ClassNamePatternParameterConverter<T> {

	static final String DEACTIVATE_ALL_PATTERN = "*";

	Predicate<T> get(ConfigurationParameters configurationParameters, String key) {
		// @formatter:off
        return configurationParameters.get(key)
                .filter(StringUtils::isNotBlank)
                .map(String::trim)
                .map(patternString -> {
                    if (DEACTIVATE_ALL_PATTERN.equals(patternString)) {
                        return alwaysDeactivated();
                    }
                    return matchesRegex(patternString);
                })
                .orElse(alwaysActivated());
        // @formatter:on
	}

	private Predicate<T> matchesRegex(String patternString) {
		Pattern pattern = Pattern.compile(convertToRegEx(patternString));
		return object -> !pattern.matcher(object.getClass().getName()).matches();
	}

	private Predicate<T> alwaysActivated() {
		return object -> true;
	}

	private Predicate<T> alwaysDeactivated() {
		return object -> false;
	}

	/**
	 * See {@link org.junit.jupiter.engine.Constants#DEACTIVATE_CONDITIONS_PATTERN_PROPERTY_NAME} for
	 * details on the pattern matching syntax.
	 */
	private String convertToRegEx(String pattern) {
		pattern = Matcher.quoteReplacement(pattern);

		// Match "." against "." and "$" since users may declare a "." instead of a
		// "$" as the separator between classes and nested classes.
		pattern = pattern.replace(".", "[.$]");

		// Convert our "*" wildcard into a proper RegEx pattern.
		pattern = pattern.replace("*", ".+");

		return pattern;
	}

}

@@ -762,24 +762,18 @@
 ter:off%0A
-        
+%09%09
 return c
@@ -804,32 +804,20 @@
 et(key)%0A
-                
+%09%09%09%09
 .filter(
@@ -841,32 +841,20 @@
 tBlank)%0A
-                
+%09%09%09%09
 .map(Str
@@ -864,32 +864,20 @@
 ::trim)%0A
-                
+%09%09%09%09
 .map(pat
@@ -892,36 +892,21 @@
 ng -%3E %7B%0A
-                    
+%09%09%09%09%09
 if (DEAC
@@ -953,32 +953,14 @@
 ) %7B%0A
-                        
+%09%09%09%09%09%09
 retu
@@ -987,50 +987,20 @@
 ();%0A
-                    %7D%0A                    
+%09%09%09%09%09%7D%0A%09%09%09%09%09
 retu
@@ -1035,43 +1035,19 @@
 g);%0A
-                %7D)%0A                
+%09%09%09%09%7D)%0A%09%09%09%09
 .orE
@@ -1074,16 +1074,10 @@
 ));%0A
-        
+%09%09
 // @

Fix formatting