All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.gov.gchq.gaffer.data.generator.OneToOneElementGenerator Maven / Gradle / Ivy

/*
 * Copyright 2016 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.data.generator;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import uk.gov.gchq.gaffer.data.AlwaysValid;
import uk.gov.gchq.gaffer.data.TransformIterable;
import uk.gov.gchq.gaffer.data.Validator;
import uk.gov.gchq.gaffer.data.element.Element;

/**
 * An OneToOneElementGenerator extends {@link uk.gov.gchq.gaffer.data.generator.ElementGenerator} and provides one to
 * one generator methods for directly converting a single {@link uk.gov.gchq.gaffer.data.element.Element} to a domain object and
 * vice versa.
 *
 * @param  the type of domain object
 */
public abstract class OneToOneElementGenerator implements ElementGenerator {

    private Validator elementValidator;
    private Validator objValidator;
    private boolean skipInvalid;

    /**
     * Constructs an OneToOneElementGenerator that doesn't validate the any elements or objects.
     */
    public OneToOneElementGenerator() {
        this(new AlwaysValid(), new AlwaysValid(), false);
    }

    /**
     * Constructs an OneToOneElementGenerator with the provided element and object validators.
     * These validators allow elements and objects to be filtered out before attempting to convert them.
     *
     * @param elementValidator a {@link uk.gov.gchq.gaffer.data.Validator} to validate {@link uk.gov.gchq.gaffer.data.element.Element}s
     * @param objValidator     a {@link uk.gov.gchq.gaffer.data.Validator} to validate domain objects
     * @param skipInvalid      true if invalid elements/objects should be skipped, otherwise an
     *                         {@link java.lang.IllegalArgumentException} will be thrown if a validator rejects a value.
     */
    public OneToOneElementGenerator(final Validator elementValidator, final Validator objValidator,
                                    final boolean skipInvalid) {
        this.elementValidator = elementValidator;
        this.objValidator = objValidator;
        this.skipInvalid = skipInvalid;
    }

    public Validator getElementValidator() {
        return elementValidator;
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class")
    @JsonGetter("elementValidator")
    Validator getElementValidatorJson() {
        return elementValidator instanceof AlwaysValid ? null : elementValidator;
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class")
    public void setElementValidator(final Validator elementValidator) {
        this.elementValidator = elementValidator;
    }

    public Validator getObjValidator() {
        return objValidator;
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class")
    @JsonGetter("objValidator")
    Validator getObjValidatorJson() {
        return objValidator instanceof AlwaysValid ? null : objValidator;
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class")
    public void setObjValidator(final Validator objValidator) {
        this.objValidator = objValidator;
    }

    public boolean isSkipInvalid() {
        return skipInvalid;
    }

    public void setSkipInvalid(final boolean skipInvalid) {
        this.skipInvalid = skipInvalid;
    }

    /**
     * @param domainObjects an {@link java.lang.Iterable} of domain objects to convert
     * @return a {@link uk.gov.gchq.gaffer.data.TransformIterable} to lazy convert each domain object into an
     * {@link uk.gov.gchq.gaffer.data.element.Element}
     * @see uk.gov.gchq.gaffer.data.generator.ElementGenerator#getElements(java.lang.Iterable)
     */
    @Override
    public Iterable getElements(final Iterable domainObjects) {
        return new TransformIterable(domainObjects, objValidator, skipInvalid) {
            @Override
            protected Element transform(final OBJ item) {
                return getElement(item);
            }
        };
    }

    /**
     * @param elements an {@link java.lang.Iterable} of {@link uk.gov.gchq.gaffer.data.element.Element} to convert
     * @return a {@link uk.gov.gchq.gaffer.data.TransformIterable} to lazy convert each {@link uk.gov.gchq.gaffer.data.element.Element} to
     * domain object
     * @see uk.gov.gchq.gaffer.data.generator.ElementGenerator#getObjects(java.lang.Iterable)
     */
    @Override
    public Iterable getObjects(final Iterable elements) {
        return new TransformIterable(elements, elementValidator, skipInvalid) {
            @Override
            protected OBJ transform(final Element item) {
                return getObject(item);
            }
        };
    }

    /**
     * @param domainObject the domain object to convert
     * @return the generated {@link uk.gov.gchq.gaffer.data.element.Element}
     */
    public abstract Element getElement(final OBJ domainObject);

    /**
     * @param element the {@link uk.gov.gchq.gaffer.data.element.Element} to convert
     * @return the generated domain object
     */
    public abstract OBJ getObject(Element element);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy