org.graylog.plugins.views.Requirements Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog.plugins.views;
import com.google.common.collect.ForwardingMap;
import com.google.inject.assistedinject.Assisted;
import org.graylog.plugins.views.search.views.PluginMetadataSummary;
import javax.inject.Inject;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Requirements extends ForwardingMap {
private final Map requirements;
private final O dto;
@Inject
public Requirements(Set extends Requirement> requirements, @Assisted O dto) {
this.dto = dto;
this.requirements = requirements.stream()
.map(requirement -> requirement.test(dto))
.flatMap(s -> s.entrySet().stream())
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (entry1, entry2) -> entry1));
}
@Override
protected Map delegate() {
return this.requirements;
}
public O rebuildRequirements(Function> getter,
BiFunction, O> setter) {
final Map requirements = Stream.concat(
getter.apply(dto).entrySet().stream(),
this.entrySet().stream()
)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (entry1, entry2) -> entry1));
return setter.apply(dto, requirements);
}
public interface Factory {
Requirements create(O dto);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy