package de.retest.recheck.review.ignore.matcher;

import java.util.Objects;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;

import de.retest.recheck.review.ignore.io.RegexLoader;
import de.retest.recheck.ui.descriptors.Element;

public class ElementClassMatcher implements Matcher<Element> {

	public static final String CLASS_KEY = "class";

	private final String classValue;

	public ElementClassMatcher( final Element element ) {
		this( Objects.toString( element.getIdentifyingAttributes().get( CLASS_KEY ), "" ) );
	}

	private ElementClassMatcher( final String classValue ) {
		this.classValue = classValue;
	}

	@Override
	public boolean test( final Element element ) {
		final String classValue = element.getIdentifyingAttributes().get( CLASS_KEY );
		return classValue != null && this.classValue.contains( classValue );
	}

	@Override
	public String toString() {
		return String.format( ElementClassMatcherLoader.FORMAT, classValue );
	}

	public static final class ElementClassMatcherLoader extends RegexLoader<ElementClassMatcher> {

		private static final String CLASS = CLASS_KEY + "=";

		private static final String FORMAT = CLASS + "%s";
		private static final Pattern REGEX = Pattern.compile( CLASS + "(.+)" );

		public ElementClassMatcherLoader() {
			super( REGEX );
		}

		@Override
		protected ElementClassMatcher load( final MatchResult matcher ) {
			final String classValue = matcher.group( 1 );
			return new ElementClassMatcher( classValue );
		}
	}
}

@@ -42,32 +42,39 @@
 atcher;%0A%0Aimport 
+static 
 java.util.Object
@@ -67,23 +67,125 @@
 va.util.
-Objects
+stream.Collectors.toCollection;%0A%0Aimport java.util.Collections;%0Aimport java.util.HashSet;%0Aimport java.util.Set
 ;%0Aimport
@@ -245,16 +245,48 @@
 Pattern;
+%0Aimport java.util.stream.Stream;
 %0A%0Aimport
@@ -509,30 +509,35 @@
 ivate final 
+Set%3C
 String
+%3E
  classValue;
@@ -535,16 +535,17 @@
 assValue
+s
 ;%0A%0A%09publ
@@ -606,81 +606,55 @@
 is( 
-Objects.toString( element.getIdentifyingAttributes().get( CLASS_KEY ), %22%22
+toClassValuesSet( toClassValueString( element )
  ) )
@@ -691,30 +691,35 @@
 cher( final 
+Set%3C
 String
+%3E
  classValue 
@@ -717,16 +717,17 @@
 assValue
+s
  ) %7B%0A%09%09t
@@ -740,16 +740,17 @@
 assValue
+s
  = class
@@ -754,16 +754,17 @@
 assValue
+s
 ;%0A%09%7D%0A%0A%09@
@@ -820,38 +820,43 @@
 ent ) %7B%0A%09%09final 
+Set%3C
 String
+%3E
  classValue = el
@@ -854,94 +854,125 @@
 alue
+s
  = 
-element.getIdentifyingAttributes().get( CLASS_KEY );%0A%09%09return classValue != null &&
+toClassValuesSet( toClassValueString( element ) );%0A%09%09if ( classValues.isEmpty() ) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%09return
  thi
@@ -983,16 +983,17 @@
 assValue
+s
 .contain
@@ -993,16 +993,19 @@
 contains
+All
 ( classV
@@ -1000,32 +1000,33 @@
 sAll( classValue
+s
  );%0A%09%7D%0A%0A%09@Overri
@@ -1120,24 +1120,25 @@
 , classValue
+s
  );%0A%09%7D%0A%0A%09pub
@@ -1640,16 +1640,34 @@
 Matcher(
+ toClassValuesSet(
  classVa
@@ -1669,16 +1669,18 @@
 assValue
+ )
  );%0A%09%09%7D%0A
@@ -1681,11 +1681,379 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%0A%09private static String toClassValueString( final Element element ) %7B%0A%09%09return element.getIdentifyingAttributes().get( CLASS_KEY );%0A%09%7D%0A%0A%09private static Set%3CString%3E toClassValuesSet( final String classValue ) %7B%0A%09%09if ( classValue == null ) %7B%0A%09%09%09return Collections.emptySet();%0A%09%09%7D%0A%09%09return Stream.of( classValue.split( %22 %22 ) ).collect( toCollection( HashSet::new ) );%0A%09%7D
 %0A%7D%0A

Fix tests