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

fr.ird.observe.dto.reference.DtoReference Maven / Gradle / Ivy

There is a newer version: 4.34
Show newest version
package fr.ird.observe.dto.reference;

/*-
 * #%L
 * ObServe Toolkit :: Common Dto
 * %%
 * Copyright (C) 2017 - 2020 IRD, 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.ImmutableSet;
import com.google.common.collect.Maps;
import fr.ird.observe.dto.IdDto;
import fr.ird.observe.dto.ObserveDto;

import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created on 21/11/15.
 *
 * @author Tony Chemit - [email protected]
 */
public abstract class DtoReference> extends IdDto implements ObserveDto, DtoReferenceAware {


    public static final String PROPERTY_TYPE = "type";

    public static final String PROPERTY_CREATE_DATE = "createDate";

    public static final String PROPERTY_VERSION = "version";

    public abstract DtoReferenceDefinition getDefinition();

    protected DtoReference(DtoReferenceAware dto) {
        Objects.requireNonNull(dto);
        this.id = dto.getTopiaId();
        this.version = dto.getTopiaVersion();
        this.createDate = dto.getTopiaCreateDate();
        this.lastUpdateDate = dto.getLastUpdateDate();
    }

//    public static  Predicate newIdPredicate(String id) {
//        return input -> Objects.equals(id, input.getId());
//    }

    public static  Map splitById(Collection dtos) {
        return Maps.uniqueIndex(dtos, DtoReference::getId);
    }

    public static  Collection filterById(Collection source, String id) {
        return source.stream().filter(newIdPredicate(id)).collect(Collectors.toList());
    }

    public static  BeanType find(Collection source, String id) {
        return source.stream().filter(newIdPredicate(id)).findFirst().orElse(null);
    }

    @Override
    public final Date getCreateDate() {
        return createDate;
    }

    @Override
    public final long getVersion() {
        return version;
    }

    @SuppressWarnings("unchecked")
    public  O getPropertyValue(String propertyName) {
        Function propertyFunction = getDefinition().getPropertyFunction(propertyName);
        return propertyFunction.apply((R) this);
    }

    public Class getDtoType() {
        return getDefinition().getDtoType();
    }

    public Class getReferenceType() {
        return getDefinition().getType();
    }

    public ImmutableSet getPropertyNames() {
        return getDefinition().getPropertyNames();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy