<commit_before>/* 
 * Copyright 2007 <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 gldapo.schema
import gldapo.schema.attribute.*
import java.lang.reflect.Field
import gldapo.Gldapo
import org.apache.commons.lang.StringUtils

class SchemaInspection 
{
    static excludedFields = ["metaClass"]
    
    def schema
    def gldapo
    
    SchemaInspection(Class schema, Gldapo gldapo) {
        this.schema = schema
        this.gldapo = gldapo
    }

    Map getAttributeMappings()
    {
        def mappings = [:]
        this.schema.declaredFields.each {
            def mapping = getMappingForField(it)
            if (mapping) mappings[it.name] = mapping
        }
        return mappings
    }
    
    AbstractAttributeMapping getMappingForField(Field field)
    {
        if (excludedFields.contains(field.name) || !fieldIsReadableAndWritable(field)) return null
        getAttributeMappingClassForField(field).newInstance(schema, field, gldapo.typemappings)
    }
    
    def getAttributeMappingClassForField(field) {
        (Collection.isAssignableFrom(field.type)) ? MultiValueAttributeMapping : SingleValueAttributeMapping
    }

    def fieldIsReadableAndWritable(Field field)
    {
        def capitalisedFieldName = StringUtils.capitalize(field.name)
        def mc = this.schema.metaClass
        mc.hasMetaMethod("get${capitalisedFieldName}") && mc.hasMetaMethod("set${capitalisedFieldName}", field.type)
    }
}<commit_msg>• Refactored schema inspection to do the inspection during construction.<commit_after>/* 
 * Copyright 2007 <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 gldapo.schema
import gldapo.schema.attribute.*
import java.lang.reflect.Field
import gldapo.Gldapo
import org.apache.commons.lang.StringUtils

class SchemaInspection 
{
    static excludedFields = ["metaClass"]
    
    def schema
    def gldapo
    def attributeMappings = [:]
    
    SchemaInspection(Class schema, Gldapo gldapo) {
        this.schema = schema
        this.gldapo = gldapo
        
        this.schema.declaredFields.each {
            def mapping = getMappingForField(it)
            if (mapping) 
                attributeMappings[it.name] = mapping
        }
    }
    
    private getMappingForField(Field field)
    {
        if (excludedFields.contains(field.name) || !fieldIsReadableAndWritable(field)) return null
        getAttributeMappingClassForField(field).newInstance(schema, field, gldapo.typemappings)
    }
    
    private getAttributeMappingClassForField(field) {
        (Collection.isAssignableFrom(field.type)) ? MultiValueAttributeMapping : SingleValueAttributeMapping
    }

    private fieldIsReadableAndWritable(Field field)
    {
        def capitalisedFieldName = StringUtils.capitalize(field.name)
        def mc = this.schema.metaClass
        mc.hasMetaMethod("get${capitalisedFieldName}") && mc.hasMetaMethod("set${capitalisedFieldName}", field.type)
    }
}
{'fraction_non_alphanumeric': 0.08211586901763224, 'fraction_numerical': 0.004030226700251889, 'mean_word_length': 3.2516059957173447, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5007701', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1076, 'n_words': 307}