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

fr.ird.observe.dto.reference.DtoReferenceCollection 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.ImmutableList;
import com.google.common.collect.ImmutableSet;
import fr.ird.observe.dto.ObserveDto;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public abstract class DtoReferenceCollection implements ObserveDto, Serializable {

    public static final String PROPERTY_TYPE = "type";

    public static final String PROPERTY_REFERENCES = "references";

    private static final long serialVersionUID = 1L;

    final Class type;

    private final List references;

    DtoReferenceCollection(Class type, List references) {
        this.type = type;
        this.references = references;
    }

    public static  List filterEnabled(Collection references) {

        return references.stream().filter(ReferentialDtoReference::isEnabled).collect(Collectors.toList());

    }

    public static  List filterContains(Collection references, Set containsIds) {

        return references.stream().filter(r -> containsIds.contains(r.getId())).collect(Collectors.toList());

    }

    public static  List filterNotContains(Collection references, Set containsIds) {

        return references.stream().filter(r -> !containsIds.contains(r.getId())).collect(Collectors.toList());

    }

    public Class getType() {
        return type;
    }

    public Optional tryGetReferenceById(String id) {
        return references.stream().filter(DtoReference.newIdPredicate(id)).findFirst();
    }

    public R getReferenceByPosition(int index) {
        return references.get(index);
    }

    public int size() {
        return references.size();
    }

    public Set toSet() {
        return ImmutableSet.copyOf(references);
    }

    public List toList() {
        return ImmutableList.copyOf(references);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy