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

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

import com.kenshoo.pl.entity.*;
import org.jooq.lambda.Seq;

import java.util.Optional;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

public class EntityIdExtractor {

    public static final EntityIdExtractor INSTANCE = new EntityIdExtractor();

    public > Optional extract(final EntityChange entityChange,
                                                              final Entity entity) {
        requireNonNull(entityChange, "entityChange is required");
        requireNonNull(entity, "entity is required");

        return entityChange.getEntityType()
                           .getIdField()
                           .flatMap(idField -> extract(entityChange, entity, idField));
    }

    private > Optional extract(final EntityChange entityChange,
                                                               final Entity entity,
                                                               final EntityField idField) {

        return Seq.>>of(() -> extractFromEntityChange(entityChange, idField),
                                             () -> extractFromIdentifier(entityChange, idField),
                                             () -> extractFromEntity(entity, idField))
            .map(Supplier::get)
            .findFirst(Optional::isPresent)
            .flatMap(optionalId -> optionalId.map(String::valueOf));
    }

    private > Optional extractFromEntityChange(final EntityChange entityChange,
                                                                          final EntityField idField) {
        if (entityChange.containsField(idField)) {
            return Optional.ofNullable(entityChange.get(idField));
        }
        return Optional.empty();
    }

    private > Optional extractFromIdentifier(EntityChange entityChange, EntityField idField) {
        final Identifier identifier = entityChange.getIdentifier();
        if (identifier != null && identifier.containsField(idField)) {
            return Optional.ofNullable(identifier.get(idField));
        }
        return Optional.empty();
    }

    private > Optional extractFromEntity(Entity entity, EntityField idField) {
        if (entity.containsField(idField)) {
            return Optional.ofNullable(entity.get(idField));
        }
        return Optional.empty();
    }

    private EntityIdExtractor() {
        // singleton
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy