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

com.kenshoo.pl.entity.UniqueKey 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.google.common.collect.ImmutableList;
import org.jooq.Record;
import org.jooq.TableField;
import org.jooq.lambda.Seq;

import java.util.Arrays;
import java.util.List;

import static org.jooq.lambda.Seq.seq;


/**
 * Specifies UniqueKey of entry.
 * Since the key is used in maps, it's highly recommended to reuse the value of this class.
 * 

* The instances of this class are immutable. */ public class UniqueKey> implements IdentifierType { private final EntityField[] fields; private final TableField[] tableFields; private int hashCode; public UniqueKey(EntityField[] fields) { this.fields = fields; //noinspection unchecked this.tableFields = Arrays.asList(fields).stream() .flatMap(field -> field.getDbAdapter().getTableFields()) .toArray(TableField[]::new); } public UniqueKey(Iterable> fields) { this(seq(fields).toArray(EntityField[]::new)); } @Override public EntityField[] getFields() { return fields; } @Override public List> getTableFields() { //noinspection unchecked return ImmutableList.copyOf(tableFields); } @Override public Identifier createIdentifier(FieldsValueMap fieldsValueMap) { Object[] values = new Object[fields.length]; for (int i = 0; i < fields.length; i++) { values[i] = fieldsValueMap.get(fields[i]); } return new UniqueKeyValue<>(this, values); } @Override public Identifier createIdentifier(Entity entity) { final Object[] values = Seq.of(fields) .map(entity::get) .toArray(); return new UniqueKeyValue<>(this, values); } @Override public E getEntityType() { if(this.fields.length == 0) { throw new IllegalStateException("unique key does not contain any fields."); } return (E) this.fields[0].getEntityType(); } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof UniqueKey)) return false; UniqueKey uniqueKey = (UniqueKey) o; return Arrays.deepEquals(fields, uniqueKey.fields); } @Override public int hashCode() { if (hashCode == 0) { hashCode = Arrays.deepHashCode(fields); } return hashCode; } @Override public String toString() { return "UniqueKey{" + "fields=" + Arrays.toString(fields) + '}'; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy