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

com.kenshoo.pl.entity.OverridingContext Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.pl.entity;

import org.jooq.lambda.Seq;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Map;

import static java.util.Optional.ofNullable;

public class OverridingContext implements ChangeContext {

    private final ChangeContext original;
    private final Map overrides = new IdentityHashMap<>();

    public OverridingContext(ChangeContext original) {
        this.original = original;
    }

    @Override
    public boolean isEnabled(Feature feature) {
        return original.isEnabled(feature);
    }

    @Override
    public CurrentEntityState getEntity(EntityChange entityChange) {
        return ofNullable(overrides.get(entityChange))
                .orElseGet(() -> original.getEntity(entityChange));
    }

    @Override
    public void addEntity(EntityChange change, CurrentEntityState currentState) {
        overrides.put(change, new OverridingEntity(currentState, original.getEntity(change)));
    }

    @Override
    public void addValidationError(EntityChange> entityChange, ValidationError error) {
        original.addValidationError(entityChange, error);
    }

    @Override
    public boolean hasValidationErrors() {
        return original.hasValidationErrors();
    }

    @Override
    public Seq getValidationErrors(EntityChange cmd) {
        return original.getValidationErrors(cmd);
    }

    @Override
    public boolean containsError(EntityChange entityChange) {
        return original.containsError(entityChange);
    }

    @Override
    public boolean containsErrorNonRecursive(EntityChange entityChange) {
        return original.containsErrorNonRecursive(entityChange);
    }

    @Override
    public PersistentLayerStats getStats() {
        return original.getStats();
    }

    @Override
    public Collection getFetchRequests() {
        return original.getFetchRequests();
    }

    @Override
    public Hierarchy getHierarchy() {
        return original.getHierarchy();
    }

    private static class OverridingEntity implements CurrentEntityState {

        private final CurrentEntityState overriding;
        private final CurrentEntityState original;


        private OverridingEntity(CurrentEntityState overriding, CurrentEntityState original) {
            this.overriding = overriding;
            this.original = original;
        }

        @Override
        public boolean containsField(EntityField field) {
            return overriding.containsField(field) || original.containsField(field);
        }

        @Override
        public  T get(EntityField field) {
            return overriding.containsField(field)
                    ? overriding.get(field)
                    : original.get(field);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy