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

com.kenshoo.pl.entity.internal.audit.AuditedFieldSet 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.internal.audit;

import com.google.common.collect.ImmutableSet;
import com.kenshoo.pl.entity.EntityField;
import com.kenshoo.pl.entity.EntityType;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.jooq.lambda.Seq;

import java.util.Set;
import java.util.stream.Stream;

import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.Objects.requireNonNull;

public class AuditedFieldSet> {

    private final EntityField idField;
    // Fields included always in the audit record with their current values (not necessarily from current entityType)
    private final Set> alwaysFields;
    // Fields included in the audit record only when changed, with their old and new values (current entityType only)
    private final Set> onChangeFields;

    private AuditedFieldSet(final EntityField idField,
                            final Set> alwaysFields,
                            final Set> onChangeFields) {
        this.idField = idField;
        this.alwaysFields = alwaysFields;
        this.onChangeFields = onChangeFields;
    }

    public EntityField getIdField() {
        return idField;
    }

    public Set> getAlwaysFields() {
        return alwaysFields;
    }

    public Set> getOnChangeFields() {
        return onChangeFields;
    }

    public Stream> getAllFields() {
        return Stream.of(singleton(idField),
                         alwaysFields,
                         onChangeFields)
                     .flatMap(Set::stream);
    }

    public AuditedFieldSet intersectWith(final Stream> fields) {
        return builder(idField)
            .withAlwaysFields(alwaysFields)
            .withOnChangeFields(Seq.seq(fields).filter(onChangeFields::contains))
            .build();
    }

    public static > Builder builder(final EntityField idField) {
        return new Builder<>(idField);
    }

    public static class Builder> {
        private final EntityField idField;
        private Set> alwaysFields = emptySet();
        private Set> onChangeFields = emptySet();

        public Builder(EntityField idField) {
            this.idField = requireNonNull(idField, "idField is required");
        }

        public Builder withAlwaysFields(final Iterable> alwaysFields) {
            this.alwaysFields = alwaysFields == null ? emptySet() : ImmutableSet.copyOf(alwaysFields);
            return this;
        }

        public Builder withOnChangeFields(final Iterable> onChangeFields) {
            this.onChangeFields = onChangeFields == null ? emptySet() : ImmutableSet.copyOf(onChangeFields);
            return this;
        }

        public AuditedFieldSet build() {
            return new AuditedFieldSet<>(idField, alwaysFields, onChangeFields);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        final AuditedFieldSet that = (AuditedFieldSet) o;

        return new EqualsBuilder()
            .append(idField, that.idField)
            .append(alwaysFields, that.alwaysFields)
            .append(onChangeFields, that.onChangeFields)
            .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
            .append(idField)
            .append(alwaysFields)
            .append(onChangeFields)
            .toHashCode();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
            .append("idField", idField)
            .append("alwaysFields", alwaysFields)
            .append("onChangeFields", onChangeFields)
            .toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy