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

nl.vpro.domain.validation.NoDuplicateOwnerValidator Maven / Gradle / Ivy

Go to download

Several domains like 'media', pages' and 'subtitles' in the POMS system share some common properties which are collected here

There is a newer version: 8.3.1
Show newest version
package nl.vpro.domain.validation;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import nl.vpro.domain.media.support.Ownable;
import nl.vpro.domain.media.support.OwnerType;

/**
 * @author Michiel Meeuwissen
 * @since 5.12
 */
public class NoDuplicateOwnerValidator implements ConstraintValidator> {


    @Override
    public boolean isValid(Collection value, ConstraintValidatorContext context) {
        return isValid(value);
    }


    protected  boolean isValid(Collection value) {
        if (value == null || value.isEmpty()) {
            return true;
        }
        Map counts = new HashMap<>();
        for (Ownable o : value) {
            if (counts.computeIfAbsent(o.getOwner(), (a) -> new AtomicInteger(0)).incrementAndGet() > 1) {
                return false;
            }

        }
        return true;

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy