
fr.ird.observe.dto.ToolkitId Maven / Gradle / Ivy
package fr.ird.observe.dto;
/*-
* #%L
* ObServe Toolkit :: Dto
* %%
* Copyright (C) 2017 - 2021 Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import com.google.common.collect.Maps;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Unified id for dto and persistence.
*
* It is based of course on persistence id :(
*
* Created on 13/04/2021.
*
* @author Tony Chemit - [email protected]
* @since 5.0.17
*/
public interface ToolkitId extends ObserveDto{
String PROPERTY_TOOLKIT_TOPIA_ID = "topiaId";
String PROPERTY_TOOLKIT_ID = "id";
String PROPERTY_TOOLKIT_VERSION = "topiaVersion";
String PROPERTY_TOOLKIT_CREATE_DATE = "topiaCreateDate";
String PROPERTY_TOOLKIT_LAST_UPDATE_DATE = "lastUpdateDate";
String PROPERTY_PERSISTED = "persisted";
String PROPERTY_NOT_PERSISTED = "notPersisted";
static Set ids(Stream extends ToolkitId> stream) {
return stream.map(ToolkitId::getTopiaId).collect(Collectors.toCollection(LinkedHashSet::new));
}
static Stream contains(Stream stream, Collection ids) {
return stream.filter(id -> ids.contains(id.getTopiaId()));
}
static Stream notContains(Stream stream, Collection ids) {
return stream.filter(id -> !ids.contains(id.getTopiaId()));
}
static Map uniqueIndex(Collection data) {
return Maps.uniqueIndex(data, ToolkitId::getTopiaId);
}
static R find(Collection source, String id) {
return source.stream().filter(r -> Objects.equals(id, r.getTopiaId())).findFirst().orElse(null);
}
static List filterContains(Collection references, String... containsIds) {
return filterContains(references, Arrays.asList(containsIds));
}
static List filterContains(Collection references, Collection containsIds) {
return contains(references.stream(), containsIds).collect(Collectors.toList());
}
static List filterNotContains(Collection references, Collection containsIds) {
return notContains(references.stream(), containsIds).collect(Collectors.toList());
}
String getId();
String getTopiaId();
long getTopiaVersion();
Date getTopiaCreateDate();
Date getLastUpdateDate();
boolean isPersisted();
default boolean isNotPersisted() {
return !isPersisted();
}
default Optional getOptionalId() {
return Optional.ofNullable(getId());
}
default Optional getOptionalLastUpdateDate() {
return Optional.ofNullable(getLastUpdateDate());
}
}