<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * 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 io.michaelrocks.grip

import io.michaelrocks.grip.mirrors.*
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type

fun <T> not(matcher: (T) -> Boolean) =
    { value: T -> !matcher(value) }
inline infix fun <reified R, reified T1 : R, reified T2 : R> ((T1) -> Boolean).and(crossinline other: (T2) -> Boolean) =
    { value: R -> this(value as T1) && other(value as T2) }
inline infix fun <reified R, reified T1 : R, reified T2 : R> ((T1) -> Boolean).or(crossinline other: (T2) -> Boolean) =
    { value: R -> this(value as T1) || other(value as T2) }
inline infix fun <reified R, reified T1 : R, reified T2 : R> ((T1) -> Boolean).xor(crossinline other: (T2) -> Boolean) =
    { value: R -> this(value as T1) != other(value as T2) }

inline fun typeMatcher(crossinline predicate: (Type) -> Boolean) =
    { type: Type -> predicate(type) }
fun equalsTo(type: Type) = typeMatcher { it == type }
fun sortEqualsTo(sort: Int) = typeMatcher { it.sort == sort }
fun isPrimitive() = typeMatcher { when (it.sort) {
  Type.OBJECT, Type.ARRAY, Type.METHOD -> false
  else -> true
}}
fun isArray() = sortEqualsTo(Type.ARRAY)
fun isObject() = sortEqualsTo(Type.OBJECT)
fun isVoid() = sortEqualsTo(Type.VOID)

inline fun returns(crossinline predicate: (Type) -> Boolean) =
    typeMatcher { predicate(it.returnType) }
fun returns(type: Type) = returns { it == type }

inline fun stringMatcher(crossinline predicate: (String) -> Boolean) =
    { string: String -> predicate(string) }
fun equalsTo(string: String) = stringMatcher { it == string }
fun matches(regex: Regex) = stringMatcher { regex.matches(it) }
fun startsWith(prefix: String) = stringMatcher { it.startsWith(prefix) }
fun endsWith(suffix: String) = stringMatcher { it.endsWith(suffix) }
fun contains(string: String) = stringMatcher { it.contains(string) }

inline fun type(crossinline predicate: (Type) -> Boolean) =
    { mirror: Typed -> predicate(mirror.type) }

inline fun access(crossinline predicate: (Int) -> Boolean) =
    { mirror: Element -> predicate(mirror.access) }
fun access(access: Int) = access { it == access }
fun accessHasAllOf(mask: Int) = access { it and mask == mask }
fun accessHasAnyOf(mask: Int) = access { it and mask != 0 }
fun accessHasNoneOf(mask: Int) = access { it and mask == 0 }
fun isPublic() = accessHasAllOf(Opcodes.ACC_PUBLIC)
fun isProtected() = accessHasAllOf(Opcodes.ACC_PROTECTED)
fun isPrivate() = accessHasAllOf(Opcodes.ACC_PRIVATE)
fun isPackagePrivate() = accessHasNoneOf(Opcodes.ACC_PUBLIC or Opcodes.ACC_PROTECTED or Opcodes.ACC_PRIVATE)
fun isStatic() = accessHasAllOf(Opcodes.ACC_STATIC)
fun isFinal() = accessHasAllOf(Opcodes.ACC_FINAL)
fun isInterface() = accessHasAllOf(Opcodes.ACC_INTERFACE)
fun isAbstract() = accessHasAllOf(Opcodes.ACC_ABSTRACT)
fun isAnnotation() = accessHasAllOf(Opcodes.ACC_ANNOTATION)
fun isEnum() = accessHasAllOf(Opcodes.ACC_ENUM)

inline fun name(crossinline predicate: (String) -> Boolean) =
    { mirror: Element -> predicate(mirror.name) }

inline fun annotatedWith(crossinline predicate: (AnnotationMirror) -> Boolean) =
    { mirror: Annotated -> mirror.annotations.any { predicate(it) } }
inline fun annotatedWith(annotationType: Type, crossinline predicate: (AnnotationMirror) -> Boolean) =
    annotatedWith { it.type == annotationType && predicate(it) }
fun annotatedWith(annotationType: Type) =
    annotatedWith { it.type == annotationType }

inline fun version(crossinline predicate: (Int) -> Boolean) =
    { mirror: ClassMirror -> predicate(mirror.version) }
fun version(version: Int) = version { it == version }
fun versionIsGreater(version: Int) = version { it > version }
fun versionIsGreaterOrEqual(version: Int) = version { it >= version }
fun versionIsLower(version: Int) = version { it < version }
fun versionIsLowerOrEqual(version: Int) = version { it <= version }

inline fun superName(crossinline predicate: (String) -> Boolean) =
    { mirror: ClassMirror -> mirror.superName?.let { predicate(it) } ?: false }
fun hasSuperName() =
    { mirror: ClassMirror -> !mirror.superName.isNullOrEmpty() }

inline fun interfaces(crossinline predicate: (List<Type>) -> Boolean) =
    { mirror: ClassMirror -> predicate(mirror.interfaces) }
fun interfacesContain(type: Type) = interfaces { it.contains(type) }
fun interfacesAreEmpty() = interfaces { it.isEmpty() }

inline fun withField(crossinline predicate: (FieldMirror) -> Boolean) =
    { mirror: ClassMirror -> mirror.fields.any { predicate(it) } }

inline fun withConstructor(crossinline predicate: (MethodMirror) -> Boolean) =
    { mirror: ClassMirror -> mirror.constructors.any { predicate(it) } }
inline fun withMethod(crossinline predicate: (MethodMirror) -> Boolean) =
    { mirror: ClassMirror -> mirror.methods.any { predicate(it) } }
fun isConstructor() =
    { mirror: MethodMirror -> mirror.isConstructor() }
fun isDefaultConstructor() =
    { mirror: MethodMirror -> mirror.isDefaultConstructor() }
fun isStaticInitializer() =
    { mirror: MethodMirror -> mirror.isStaticInitializer() }

inline fun withParameter(crossinline predicate: (MethodParameterMirror) -> Boolean) =
    { mirror: MethodMirror -> mirror.parameters.any { predicate(it) } }

fun existsInClasses(query: Query<ClassesResult>) =
    { mirror: ClassMirror ->
      query.execute().containsType(mirror.type)
    }
fun existsInFields(query: Query<FieldsResult>) =
    { mirror: FieldMirror ->
      query.execute().any {
        it.fields.any { it.name == mirror.name && it.type == mirror.type }
      }
    }
fun existsInMethods(query: Query<MethodsResult>) =
    { mirror: MethodMirror ->
      query.execute().any {
        it.methods.any { it.name == mirror.name && it.type == mirror.type }
      }
    }<commit_msg>Add Grip argument to matchers<commit_after>/*
 * Copyright 2016 <NAME>
 *
 * 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 io.michaelrocks.grip

import io.michaelrocks.grip.mirrors.*
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type

fun <T> not(matcher: (Grip, T) -> Boolean) =
    { grip: Grip, value: T -> !matcher(grip, value) }
inline infix fun <reified R, reified T1 : R, reified T2 : R> ((Grip, T1) -> Boolean).and(
    crossinline other: (Grip, T2) -> Boolean
) = { grip: Grip, value: R -> this(grip, value as T1) && other(grip, value as T2) }
inline infix fun <reified R, reified T1 : R, reified T2 : R> ((Grip, T1) -> Boolean).or(
    crossinline other: (Grip, T2) -> Boolean
) = { grip: Grip, value: R -> this(grip, value as T1) || other(grip, value as T2) }
inline infix fun <reified R, reified T1 : R, reified T2 : R> ((Grip, T1) -> Boolean).xor(
    crossinline other: (Grip, T2) -> Boolean
) = { grip: Grip, value: R -> this(grip, value as T1) != other(grip, value as T2) }

inline fun typeMatcher(crossinline predicate: (Grip, Type) -> Boolean) =
    { grip: Grip, type: Type -> predicate(grip, type) }
fun equalsTo(otherType: Type) = typeMatcher { grip, type -> type == otherType }
fun sortEqualsTo(sort: Int) = typeMatcher { grip, type -> type.sort == sort }
fun isPrimitive() = typeMatcher { grip, type ->
  when (type.sort) {
    Type.OBJECT, Type.ARRAY, Type.METHOD -> false
    else -> true
  }
}
fun isArray() = sortEqualsTo(Type.ARRAY)
fun isObject() = sortEqualsTo(Type.OBJECT)
fun isVoid() = sortEqualsTo(Type.VOID)

inline fun returns(crossinline predicate: (Grip, Type) -> Boolean) =
    typeMatcher { grip, type -> predicate(grip, type.returnType) }
fun returns(otherType: Type) = returns { grip, type -> type == otherType }

inline fun stringMatcher(crossinline predicate: (Grip, String) -> Boolean) =
    { grip: Grip, string: String -> predicate(grip, string) }
fun equalsTo(otherString: String) = stringMatcher { grip, string -> string == otherString }
fun matches(regex: Regex) = stringMatcher { grip, string -> regex.matches(string) }
fun startsWith(prefix: String) = stringMatcher { grip, string -> string.startsWith(prefix) }
fun endsWith(suffix: String) = stringMatcher { grip, string -> string.endsWith(suffix) }
fun contains(otherString: String) = stringMatcher { grip, string -> string.contains(otherString) }

inline fun type(crossinline predicate: (Grip, Type) -> Boolean) =
    { grip: Grip, mirror: Typed -> predicate(grip, mirror.type) }

inline fun access(crossinline predicate: (Grip, Int) -> Boolean) =
    { grip: Grip, mirror: Element -> predicate(grip, mirror.access) }
fun access(otherAccess: Int) = access { grip, access -> access == otherAccess }
fun accessHasAllOf(mask: Int) = access { grip, access -> access and mask == mask }
fun accessHasAnyOf(mask: Int) = access { grip, access -> access and mask != 0 }
fun accessHasNoneOf(mask: Int) = access { grip, access -> access and mask == 0 }
fun isPublic() = accessHasAllOf(Opcodes.ACC_PUBLIC)
fun isProtected() = accessHasAllOf(Opcodes.ACC_PROTECTED)
fun isPrivate() = accessHasAllOf(Opcodes.ACC_PRIVATE)
fun isPackagePrivate() = accessHasNoneOf(Opcodes.ACC_PUBLIC or Opcodes.ACC_PROTECTED or Opcodes.ACC_PRIVATE)
fun isStatic() = accessHasAllOf(Opcodes.ACC_STATIC)
fun isFinal() = accessHasAllOf(Opcodes.ACC_FINAL)
fun isInterface() = accessHasAllOf(Opcodes.ACC_INTERFACE)
fun isAbstract() = accessHasAllOf(Opcodes.ACC_ABSTRACT)
fun isAnnotation() = accessHasAllOf(Opcodes.ACC_ANNOTATION)
fun isEnum() = accessHasAllOf(Opcodes.ACC_ENUM)

inline fun name(crossinline predicate: (Grip, String) -> Boolean) =
    { grip: Grip, mirror: Element -> predicate(grip, mirror.name) }

inline fun annotatedWith(crossinline predicate: (Grip, AnnotationMirror) -> Boolean) =
    { grip: Grip, mirror: Annotated -> mirror.annotations.any { predicate(grip, it) } }
inline fun annotatedWith(annotationType: Type, crossinline predicate: (Grip, AnnotationMirror) -> Boolean) =
    annotatedWith { grip, annotation -> annotation.type == annotationType && predicate(grip, annotation) }
fun annotatedWith(annotationType: Type) =
    annotatedWith { grip, annotation -> annotation.type == annotationType }

inline fun version(crossinline predicate: (Grip, Int) -> Boolean) =
    { grip: Grip, mirror: ClassMirror -> predicate(grip, mirror.version) }
fun version(otherVersion: Int) = version { grip, version -> version == otherVersion }
fun versionIsGreater(otherVersion: Int) = version { grip, version -> version > otherVersion }
fun versionIsGreaterOrEqual(otherVersion: Int) = version { grip, version -> version >= otherVersion }
fun versionIsLower(otherVersion: Int) = version { grip, version -> version < otherVersion }
fun versionIsLowerOrEqual(otherVersion: Int) = version { grip, version -> version <= otherVersion }

inline fun superName(crossinline predicate: (Grip, String) -> Boolean) =
    { grip: Grip, mirror: ClassMirror -> mirror.superName?.let { predicate(grip, it) } ?: false }
fun hasSuperName() =
    { grip: Grip, mirror: ClassMirror -> !mirror.superName.isNullOrEmpty() }

inline fun interfaces(crossinline predicate: (Grip, List<Type>) -> Boolean) =
    { grip: Grip, mirror: ClassMirror -> predicate(grip, mirror.interfaces) }
fun interfacesContain(type: Type) = interfaces { grip, interfaces -> interfaces.contains(type) }
fun interfacesAreEmpty() = interfaces { grip, interfaces -> interfaces.isEmpty() }

inline fun withField(crossinline predicate: (Grip, FieldMirror) -> Boolean) =
    { grip: Grip, mirror: ClassMirror -> mirror.fields.any { predicate(grip, it) } }

inline fun withConstructor(crossinline predicate: (Grip, MethodMirror) -> Boolean) =
    { grip: Grip, mirror: ClassMirror -> mirror.constructors.any { predicate(grip, it) } }
inline fun withMethod(crossinline predicate: (Grip, MethodMirror) -> Boolean) =
    { grip: Grip, mirror: ClassMirror -> mirror.methods.any { predicate(grip, it) } }
fun isConstructor() =
    { grip: Grip, mirror: MethodMirror -> mirror.isConstructor() }
fun isDefaultConstructor() =
    { grip: Grip, mirror: MethodMirror -> mirror.isDefaultConstructor() }
fun isStaticInitializer() =
    { grip: Grip, mirror: MethodMirror -> mirror.isStaticInitializer() }

inline fun withParameter(crossinline predicate: (Grip, MethodParameterMirror) -> Boolean) =
    { grip: Grip, mirror: MethodMirror -> mirror.parameters.any { predicate(grip, it) } }

fun existsInClasses(query: Query<ClassesResult>) =
    { grip: Grip, mirror: ClassMirror ->
      query.execute().containsType(mirror.type)
    }
fun existsInFields(query: Query<FieldsResult>) =
    { grip: Grip, mirror: FieldMirror ->
      query.execute().any {
        it.fields.any { it.name == mirror.name && it.type == mirror.type }
      }
    }
fun existsInMethods(query: Query<MethodsResult>) =
    { grip: Grip, mirror: MethodMirror ->
      query.execute().any {
        it.methods.any { it.name == mirror.name && it.type == mirror.type }
      }
    }
{'fraction_non_alphanumeric': 0.13094462540716612, 'fraction_numerical': 0.004053564965617083, 'mean_word_length': 4.495624502784407, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 144, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1368361', 'n_tokens_mistral': 4345, 'n_tokens_neox': 4182, 'n_words': 1215}