<commit_before>package org.carlos.snippets.matcher_builder;

import static org.hamcrest.Matchers.comparesEqualTo;
import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS;
import static org.hamcrest.beans.PropertyUtil.propertyDescriptorsFor;
import static org.hamcrest.core.IsEqual.equalTo;

import static com.google.common.primitives.Primitives.allPrimitiveTypes;
import static com.google.common.primitives.Primitives.allWrapperTypes;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

import com.google.common.base.Preconditions;

public class MatcherBuilder<T> {

	public static class MatcherBuilder2<T> extends MatcherBuilder<T> {

		private Object expected;
		private Set<String> excludeProperty = new HashSet<>();
		private Map<String, String> mapProperty = new HashMap<>();

		public MatcherBuilder2( Object expected ) {
			Preconditions.checkNotNull( expected );
			this.expected = expected;
		}

		public MatcherBuilder2<T> excludeProperty( String propertyName ) {
			Preconditions.checkNotNull( propertyName, "propertyName is null" );
			excludeProperty.add( propertyName );
			return this;
		}

		public MatcherBuilder2<T> excludeProperties( String propertyName,
				String... otherPropertyNames ) {
			Preconditions.checkNotNull( propertyName, "propertyName is null" );
			excludeProperty( propertyName );
			for ( String otherPropertyName : otherPropertyNames ) {
				excludeProperty( otherPropertyName );
			}
			return this;
		}

		public MatcherBuilder2<T> mapProperty( String fromPropertyName, String toPropertyName ) {
			Preconditions.checkNotNull( fromPropertyName, "fromPropertyName is null" );
			Preconditions.checkNotNull( toPropertyName, "toPropertyName is null" );
			mapProperty.put( fromPropertyName, toPropertyName );
			return this;
		}

		@Override
		public MatcherBuilder<T> hasProperty( String propertyName, Matcher<?> valueMatcher ) {
			return matcherBuilder().hasProperty( propertyName, valueMatcher );
		}

		@Override
		public MatcherBuilder<T> hasProperty( String propertyName, Object expected ) {
			return matcherBuilder().hasProperty( propertyName, expected );
		}

		@Override
		public Matcher<T> build() {
			return matcherBuilder().build();
		}

		private MatcherBuilder<T> matcherBuilder() {
			MatcherBuilder<T> matcherBuilder = new MatcherBuilder<T>();

			PropertyDescriptor[] descriptors = propertyDescriptorsFor( expected, Object.class );

			for ( PropertyDescriptor propertyDescriptor : descriptors ) {
				String propertyName = propertyDescriptor.getName();
				if ( excludeProperty.contains( propertyName ) )
					continue;
				Object propertyValue = readProperty( propertyDescriptor.getReadMethod(), expected );
				if ( mapProperty.containsKey( propertyName ) )
					matcherBuilder.hasProperty( mapProperty.get( propertyName ), propertyValue );
				else
					matcherBuilder.hasProperty( propertyName, propertyValue );
			}
			return matcherBuilder;

		}
	}

	public static <T> MatcherBuilder<T> builder() {
		return new MatcherBuilder<T>();
	}

	public static <T> MatcherBuilder2<T> samePropertyValuesAs( Object expected ) {
		return new MatcherBuilder2<T>( expected );
	}

	private static Object readProperty( Method method, Object target ) {
		try {
			return method.invoke( target, NO_ARGUMENTS );
		} catch ( Exception e ) {
			throw new IllegalArgumentException( "Could not invoke " + method + " on " + target, e );
		}
	}

	private Map<String, Matcher<? super T>> matchers = new LinkedHashMap<String, Matcher<? super T>>();

	public MatcherBuilder() {
		super();
	}

	public MatcherBuilder<T> hasProperty( String propertyName, Object expected ) {
		Preconditions.checkNotNull( propertyName, "propertyName is null" );
		if ( expected == null ) {
			return hasProperty( propertyName, nullValueMatcher() );
		} else if ( allPrimitiveTypes().contains( expected.getClass() ) || allWrapperTypes()
				.contains( expected.getClass() ) ) {
			return hasProperty( propertyName, equalTo( expected ) );
		} else if ( expected instanceof String ) {
			return hasProperty( propertyName, equalTo( expected ) );
//		} else if ( expected instanceof Comparable<?> ) {
//			return hasProperty( propertyName, comparesEqualToMatcher( expected ) );
		} else {
			return hasProperty( propertyName, samePropertyValuesAs( expected ).build() );
		}
	}

	@SuppressWarnings("unchecked")
	private Matcher<T> nullValueMatcher() {
		return (Matcher<T>) Matchers.nullValue();
	}
<commit_msg>clean up<commit_after>package org.carlos.snippets.matcher_builder;

import static org.hamcrest.Matchers.comparesEqualTo;
import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS;
import static org.hamcrest.beans.PropertyUtil.propertyDescriptorsFor;
import static org.hamcrest.core.IsEqual.equalTo;

import static com.google.common.primitives.Primitives.allPrimitiveTypes;
import static com.google.common.primitives.Primitives.allWrapperTypes;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

import com.google.common.base.Preconditions;

public class MatcherBuilder<T> {

	public static class MatcherBuilder2<T> extends MatcherBuilder<T> {

		private Object expected;
		private Set<String> excludeProperty = new HashSet<>();
		private Map<String, String> mapProperty = new HashMap<>();

		public MatcherBuilder2( Object expected ) {
			Preconditions.checkNotNull( expected );
			this.expected = expected;
		}

		public MatcherBuilder2<T> excludeProperty( String propertyName ) {
			Preconditions.checkNotNull( propertyName, "propertyName is null" );
			excludeProperty.add( propertyName );
			return this;
		}

		public MatcherBuilder2<T> excludeProperties( String propertyName,
				String... otherPropertyNames ) {
			Preconditions.checkNotNull( propertyName, "propertyName is null" );
			excludeProperty( propertyName );
			for ( String otherPropertyName : otherPropertyNames ) {
				excludeProperty( otherPropertyName );
			}
			return this;
		}

		public MatcherBuilder2<T> mapProperty( String fromPropertyName, String toPropertyName ) {
			Preconditions.checkNotNull( fromPropertyName, "fromPropertyName is null" );
			Preconditions.checkNotNull( toPropertyName, "toPropertyName is null" );
			mapProperty.put( fromPropertyName, toPropertyName );
			return this;
		}

		@Override
		public MatcherBuilder<T> hasProperty( String propertyName, Matcher<?> valueMatcher ) {
			return matcherBuilder().hasProperty( propertyName, valueMatcher );
		}

		@Override
		public MatcherBuilder<T> hasProperty( String propertyName, Object expected ) {
			return matcherBuilder().hasProperty( propertyName, expected );
		}

		@Override
		public Matcher<T> build() {
			return matcherBuilder().build();
		}

		private MatcherBuilder<T> matcherBuilder() {
			MatcherBuilder<T> matcherBuilder = new MatcherBuilder<T>();

			PropertyDescriptor[] descriptors = propertyDescriptorsFor( expected, Object.class );

			for ( PropertyDescriptor propertyDescriptor : descriptors ) {
				String propertyName = propertyDescriptor.getName();
				if ( excludeProperty.contains( propertyName ) )
					continue;
				Object propertyValue = readProperty( propertyDescriptor.getReadMethod(), expected );
				if ( mapProperty.containsKey( propertyName ) )
					matcherBuilder.hasProperty( mapProperty.get( propertyName ), propertyValue );
				else
					matcherBuilder.hasProperty( propertyName, propertyValue );
			}
			return matcherBuilder;

		}
	}

	public static <T> MatcherBuilder<T> builder() {
		return new MatcherBuilder<T>();
	}

	public static <T> MatcherBuilder2<T> samePropertyValuesAs( Object expected ) {
		return new MatcherBuilder2<T>( expected );
	}

	private static Object readProperty( Method method, Object target ) {
		try {
			return method.invoke( target, NO_ARGUMENTS );
		} catch ( Exception e ) {
			throw new IllegalArgumentException( "Could not invoke " + method + " on " + target, e );
		}
	}

	private Map<String, Matcher<? super T>> matchers = new LinkedHashMap<String, Matcher<? super T>>();

	public MatcherBuilder() {
		super();
	}

	public MatcherBuilder<T> hasProperty( String propertyName, Object expected ) {
		Preconditions.checkNotNull( propertyName, "propertyName is null" );
		if ( expected == null ) {
			return hasProperty( propertyName, nullValueMatcher() );
		} else if ( allPrimitiveTypes().contains( expected.getClass() ) || allWrapperTypes()
				.contains( expected.getClass() ) ) {
			return hasProperty( propertyName, equalTo( expected ) );
		} else if ( expected instanceof String ) {
			return hasProperty( propertyName, equalTo( expected ) );
		} else {
			return hasProperty( propertyName, samePropertyValuesAs( expected ).build() );
		}
	}

	@SuppressWarnings("unchecked")
	private Matcher<T> nullValueMatcher() {
		return (Matcher<T>) Matchers.nullValue();
	}
{'fraction_non_alphanumeric': 0.10347092959597065, 'fraction_numerical': 0.0015329026606810468, 'mean_word_length': 4.923476005188068, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6650452', 'n_tokens_mistral': 2830, 'n_tokens_neox': 2539, 'n_words': 639}