<commit_before>/* 
 * Copyright (C) 2015 <NAME>
 * All rights reserved.
 *
 * This software may be modified and distributed under the terms
 * of the BSD license.  See the LICENSE file for details.
 */
package api.web.gw2.mapping.core;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Base class for an object marshaller built on top of JSON-P.
 * @author <NAME>
 */
abstract class JsonpAbstractMarshaller {

    /**
     * The logger instance.
     */
    protected final Logger logger = Logger.getLogger(getClass().getName());

    /**
     * Creates a new empty instance.
     */
    public JsonpAbstractMarshaller() {
    }

    public abstract <T> T loadObject(final Class<T> targetClass, final InputStream input) throws NullPointerException, IOException;

    public abstract <T> Collection<T> loadObjectArray(final Class<T> targetClass, final InputStream input) throws IOException;

    public abstract <T> T loadRuntimeObject(final String selector, final String pattern, final InputStream input) throws IOException;

    /**
     * Creates a concrete instance of the desired interface using the {@code ServiceManager}.
     * @param <T> The target type.
     * @param targetClass The target class.
     * @return A {@code T} instance, may be {@code null}.
     */
    protected final <T> T createConcreteEmptyInstance(final Class<T> targetClass) {
        // Get service loader for target class.
        final ServiceLoader serviceLoader = ServiceLoader.load(targetClass);
        // Get concrete instance for target class.
        final Iterator<T> iterator = serviceLoader.iterator();
        T result = null;
        while (iterator.hasNext()) {
            if (result == null) {
                result = (T) iterator.next();
            }
        }
        return result;
    }

    protected final Field lookupField(final Class targetClass, final String fieldName) throws NoSuchFieldException {
        Field field = null;
        for (Class aClass = targetClass; targetClass != null && field == null; aClass = aClass.getSuperclass()) {
            try {
                field = aClass.getDeclaredField(fieldName);
            } catch (NoSuchFieldException ex) {
                logger.log(Level.WARNING, "Could not find field \"{0}\" in class {1}, attenpting to use class hierachy.", new Object[]{fieldName, targetClass});
            }
        }
        if (field == null) {
            throw new NoSuchFieldException(fieldName);
        }
        return field;
    }

    /**
     * Converts a JSON key into a Java field name.
     * @param key The JSON key.
     * @return A {@code String}, never {@code null}.
     * @throws NullPointerException If {@code key} is {@code null}.
     */
    protected final String jsonKeyToJavaFieldName(final String key) throws NullPointerException {
        Objects.requireNonNull(key);
        final String[] tokens = key.split("_"); // NOI18N.
        final StringBuilder buffer = new StringBuilder(key.length());
        buffer.append(tokens[0]);
        Arrays.stream(tokens, 1, tokens.length)
                .forEach(token -> {
                    String head = token.substring(0, 1).toUpperCase();
                    String tail = token.substring(1, token.length());
                    buffer.append(head);
                    buffer.append(tail);<commit_msg>Fixed NPE in loop while searching for field in parent classes.<commit_after>/* 
 * Copyright (C) 2015 <NAME>
 * All rights reserved.
 *
 * This software may be modified and distributed under the terms
 * of the BSD license.  See the LICENSE file for details.
 */
package api.web.gw2.mapping.core;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Base class for an object marshaller built on top of JSON-P.
 * @author <NAME>
 */
abstract class JsonpAbstractMarshaller {

    /**
     * The logger instance.
     */
    protected final Logger logger = Logger.getLogger(getClass().getName());

    /**
     * Creates a new empty instance.
     */
    public JsonpAbstractMarshaller() {
    }

    public abstract <T> T loadObject(final Class<T> targetClass, final InputStream input) throws NullPointerException, IOException;

    public abstract <T> Collection<T> loadObjectArray(final Class<T> targetClass, final InputStream input) throws IOException;

    public abstract <T> T loadRuntimeObject(final String selector, final String pattern, final InputStream input) throws IOException;

    /**
     * Creates a concrete instance of the desired interface using the {@code ServiceManager}.
     * @param <T> The target type.
     * @param targetClass The target class.
     * @return A {@code T} instance, may be {@code null}.
     */
    protected final <T> T createConcreteEmptyInstance(final Class<T> targetClass) {
        // Get service loader for target class.
        final ServiceLoader serviceLoader = ServiceLoader.load(targetClass);
        // Get concrete instance for target class.
        final Iterator<T> iterator = serviceLoader.iterator();
        T result = null;
        while (iterator.hasNext()) {
            if (result == null) {
                result = (T) iterator.next();
            }
        }
        return result;
    }

    protected final Field lookupField(final Class targetClass, final String fieldName) throws NoSuchFieldException {
        Field field = null;
        for (Class aClass = targetClass; aClass != null && field == null; aClass = aClass.getSuperclass()) {
            try {
                field = aClass.getDeclaredField(fieldName);
            } catch (NoSuchFieldException ex) {
                logger.log(Level.FINEST, "Could not find field \"{0}\" in class {1}, attenpting to use class hierachy.", new Object[]{fieldName, targetClass});
            }
        }
        if (field == null) {
            logger.log(Level.WARNING, "Could not find field \"{0}\" in class {1}.", new Object[]{fieldName, targetClass});
        }
        return field;
    }

    /**
     * Converts a JSON key into a Java field name.
     * @param key The JSON key.
     * @return A {@code String}, never {@code null}.
     * @throws NullPointerException If {@code key} is {@code null}.
     */
    protected final String jsonKeyToJavaFieldName(final String key) throws NullPointerException {
        Objects.requireNonNull(key);
        final String[] tokens = key.split("_"); // NOI18N.
        final StringBuilder buffer = new StringBuilder(key.length());
        buffer.append(tokens[0]);
        Arrays.stream(tokens, 1, tokens.length)
                .forEach(token -> {
                    String head = token.substring(0, 1).toUpperCase();
                    String tail = token.substring(1, token.length());
                    buffer.append(head);
                    buffer.append(tail);
{'fraction_non_alphanumeric': 0.10466674789813574, 'fraction_numerical': 0.0036554161082003166, 'mean_word_length': 2.8391019644527598, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, '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': '4330014', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2164, 'n_words': 702}