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

com.kenshoo.pl.entity.Identifier 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 com.kenshoo.jooq.FieldAndValues;
import org.jooq.Record;
import org.jooq.TableField;
import org.jooq.lambda.Seq;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

public interface Identifier>  extends FieldsValueMap {

    IdentifierType getUniqueKey();

    default Stream getValues() {
        return Seq.of(getUniqueKey().getFields()).map(this::get);
    }

    default boolean isEmpty() {
        return !getValues().findAny().isPresent();
    }

    default int size() {
        return getUniqueKey().getFields().length;
    }

    default boolean contains(Identifier otherIdentifier) {
        return Arrays.asList(getUniqueKey().getFields()).containsAll(Arrays.asList(otherIdentifier.getUniqueKey().getFields()));
    }

    static > List> groupValuesByFields(Collection> ids) {
        final IdentifierType uniqueKey = ids.iterator().next().getUniqueKey();
        return Stream.of(uniqueKey.getFields())
                .map(field -> collectValuesOf(field, ids))
                .collect(toList());

    }

    static , T> FieldAndValues collectValuesOf(EntityField field, Collection> ids) {
        EntityFieldDbAdapter dbAdapter = field.getDbAdapter();
        List fieldValues = ids.stream().flatMap(id -> dbAdapter.getDbValues(id.get(field)).sequential()).collect(toList());
        return new FieldAndValues<>((TableField) dbAdapter.getFirstTableField(), fieldValues);
    }


}