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 java.util.Optional;

import static com.kenshoo.pl.entity.internal.Triptionals.firstPresent;
import static java.util.Objects.requireNonNull;

public class EntityIdExtractor {

    public static final EntityIdExtractor INSTANCE = new EntityIdExtractor();

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

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

    private , T> Optional extract(final EntityChange entityChange,
                                                                  final CurrentEntityState currentState,
                                                                  final EntityField idField) {

        return firstPresent(() -> extractFromIdentifier(entityChange, idField),
                            () -> new FinalEntityState(currentState, entityChange).safeGet(idField))
            .mapToOptional(String::valueOf);
    }

    private , T> Triptional extractFromIdentifier(final EntityChange entityChange,
                                                                             final EntityField idField) {
        return Triptional.of(entityChange.getIdentifier())
                         .flatMap(identifier -> identifier.safeGet(idField), Triptional::absent);
    }

    private EntityIdExtractor() {
        // singleton
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy