<commit_before>/*
 * Copyright 2017 Crown Copyright
 *
 * 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 uk.gov.gchq.gaffer.rest.service.v2.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import uk.gov.gchq.gaffer.data.element.Edge;
import uk.gov.gchq.gaffer.data.element.Element;
import uk.gov.gchq.gaffer.data.element.Entity;
import uk.gov.gchq.gaffer.data.element.IdentifierType;
import uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator;
import uk.gov.gchq.gaffer.data.element.id.EdgeId;
import uk.gov.gchq.gaffer.data.element.id.ElementId;
import uk.gov.gchq.gaffer.data.element.id.EntityId;
import uk.gov.gchq.gaffer.operation.Operation;
import uk.gov.gchq.gaffer.operation.OperationChain;
import uk.gov.gchq.gaffer.operation.data.EdgeSeed;
import uk.gov.gchq.gaffer.operation.data.EntitySeed;
import uk.gov.gchq.gaffer.operation.impl.Limit;
import uk.gov.gchq.gaffer.operation.impl.add.AddElements;
import uk.gov.gchq.gaffer.operation.impl.compare.Sort;
import uk.gov.gchq.gaffer.operation.impl.generate.GenerateElements;
import uk.gov.gchq.gaffer.operation.impl.generate.GenerateObjects;
import uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds;
import uk.gov.gchq.gaffer.operation.impl.get.GetAllElements;
import uk.gov.gchq.gaffer.operation.impl.get.GetElements;
import uk.gov.gchq.gaffer.operation.io.Output;
import uk.gov.gchq.gaffer.rest.example.ExampleDomainObject;
import uk.gov.gchq.gaffer.rest.example.ExampleDomainObjectGenerator;
import uk.gov.gchq.gaffer.rest.example.ExampleElementGenerator;
import uk.gov.gchq.gaffer.rest.factory.GraphFactory;
import uk.gov.gchq.gaffer.store.schema.Schema;
import uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition;
import uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition;
import uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition;

import javax.inject.Inject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.concurrent.ThreadLocalRandom;

import static java.lang.reflect.Modifier.isStatic;

/**
 * Default implementation of the {@link uk.gov.gchq.gaffer.rest.service.v2.example.ExamplesFactory}
 * interface. Required to be registered with HK2 to allow the correct {@link uk.gov.gchq.gaffer.rest.factory.GraphFactory}
 * object to be injected.
 */
public class DefaultExamplesFactory implements ExamplesFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExamplesFactory.class);

    @Inject
    private GraphFactory graphFactory;

    public DefaultExamplesFactory() {
        // public constructor required by HK2
    }

    @Override
    public Operation generateExample(final Class<? extends Operation> opClass) throws IllegalAccessException, InstantiationException {
        final Operation operation = opClass.newInstance();

        // Provide specific implementations for certain operations
        if (operation instanceof GetAllElements) {
            return getAllElements();
        } else if (operation instanceof GetElements) {
            return getElements();
        } else if (operation instanceof GetAdjacentIds) {
            return getAdjacentIds();
        } else if (operation instanceof AddElements) {
            return addElements();
        } else if (operation instanceof GenerateObjects) {
            return generateObjects();
        } else if (operation instanceof GenerateElements) {
            return generateElements();
        } else if (operation instanceof OperationChain) {
            return new OperationChain.Builder()
                    .first(getAllElements())
                    .then(new Limit<>(1))
                    .build();
        } else if (operation instanceof Sort) {
            return sort();
        }

        else {

            final List<Field> fields = Arrays.asList(opClass.getDeclaredFields());

            for (final Field field : fields) {
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                if (!isStatic(field.getModifiers())) {
                    field.set(operation, getExampleValue(field.getType(), ThreadLocalRandom
                            .current().nextInt(0, 11)));
                }
            }

            return operation;
        }
    }

    private Object getExampleValue(final Class clazz, final int uniqueId) {
        Object value;
        if (null == clazz) {<commit_msg>spacing correcion<commit_after>/*
 * Copyright 2017 Crown Copyright
 *
 * 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 uk.gov.gchq.gaffer.rest.service.v2.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import uk.gov.gchq.gaffer.data.element.Edge;
import uk.gov.gchq.gaffer.data.element.Element;
import uk.gov.gchq.gaffer.data.element.Entity;
import uk.gov.gchq.gaffer.data.element.IdentifierType;
import uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator;
import uk.gov.gchq.gaffer.data.element.id.EdgeId;
import uk.gov.gchq.gaffer.data.element.id.ElementId;
import uk.gov.gchq.gaffer.data.element.id.EntityId;
import uk.gov.gchq.gaffer.operation.Operation;
import uk.gov.gchq.gaffer.operation.OperationChain;
import uk.gov.gchq.gaffer.operation.data.EdgeSeed;
import uk.gov.gchq.gaffer.operation.data.EntitySeed;
import uk.gov.gchq.gaffer.operation.impl.Limit;
import uk.gov.gchq.gaffer.operation.impl.add.AddElements;
import uk.gov.gchq.gaffer.operation.impl.compare.Sort;
import uk.gov.gchq.gaffer.operation.impl.generate.GenerateElements;
import uk.gov.gchq.gaffer.operation.impl.generate.GenerateObjects;
import uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds;
import uk.gov.gchq.gaffer.operation.impl.get.GetAllElements;
import uk.gov.gchq.gaffer.operation.impl.get.GetElements;
import uk.gov.gchq.gaffer.operation.io.Output;
import uk.gov.gchq.gaffer.rest.example.ExampleDomainObject;
import uk.gov.gchq.gaffer.rest.example.ExampleDomainObjectGenerator;
import uk.gov.gchq.gaffer.rest.example.ExampleElementGenerator;
import uk.gov.gchq.gaffer.rest.factory.GraphFactory;
import uk.gov.gchq.gaffer.store.schema.Schema;
import uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition;
import uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition;
import uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition;

import javax.inject.Inject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.concurrent.ThreadLocalRandom;

import static java.lang.reflect.Modifier.isStatic;

/**
 * Default implementation of the {@link uk.gov.gchq.gaffer.rest.service.v2.example.ExamplesFactory}
 * interface. Required to be registered with HK2 to allow the correct {@link uk.gov.gchq.gaffer.rest.factory.GraphFactory}
 * object to be injected.
 */
public class DefaultExamplesFactory implements ExamplesFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExamplesFactory.class);

    @Inject
    private GraphFactory graphFactory;

    public DefaultExamplesFactory() {
        // public constructor required by HK2
    }

    @Override
    public Operation generateExample(final Class<? extends Operation> opClass) throws IllegalAccessException, InstantiationException {
        final Operation operation = opClass.newInstance();

        // Provide specific implementations for certain operations
        if (operation instanceof GetAllElements) {
            return getAllElements();
        } else if (operation instanceof GetElements) {
            return getElements();
        } else if (operation instanceof GetAdjacentIds) {
            return getAdjacentIds();
        } else if (operation instanceof AddElements) {
            return addElements();
        } else if (operation instanceof GenerateObjects) {
            return generateObjects();
        } else if (operation instanceof GenerateElements) {
            return generateElements();
        } else if (operation instanceof OperationChain) {
            return new OperationChain.Builder()
                    .first(getAllElements())
                    .then(new Limit<>(1))
                    .build();
        } else if (operation instanceof Sort) {
            return sort();
        } else {

            final List<Field> fields = Arrays.asList(opClass.getDeclaredFields());

            for (final Field field : fields) {
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                if (!isStatic(field.getModifiers())) {
                    field.set(operation, getExampleValue(field.getType(), ThreadLocalRandom
                            .current().nextInt(0, 11)));
                }
            }

            return operation;
        }
    }

    private Object getExampleValue(final Class clazz, final int uniqueId) {
        Object value;
        if (null == clazz) {
{'fraction_non_alphanumeric': 0.1001286237261304, 'fraction_numerical': 0.003561887800534283, 'mean_word_length': 3.248844052122741, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '988853', 'n_tokens_mistral': 2988, 'n_tokens_neox': 2866, 'n_words': 659}