001/*
002 * JDrupes Builder
003 * Copyright (C) 2026 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.builder.core;
020
021import java.util.HashSet;
022import java.util.Set;
023import java.util.function.Consumer;
024import java.util.stream.Stream;
025import org.jdrupes.builder.api.BoundResourceQuery;
026import org.jdrupes.builder.api.Resource;
027import org.jdrupes.builder.api.ResourceProvider;
028import org.jdrupes.builder.api.ResourceQuery;
029
030/// The Class DefaultBoundResourceQuery.
031///
032@Deprecated
033public class DefaultBoundResourceQuery<T extends Resource>
034        implements BoundResourceQuery<T> {
035
036    private final ResourceQuery<T> query;
037    private final Stream<ResourceProvider> providers;
038    private final Set<ResourceProvider> excluded = new HashSet<>();
039    private final Set<Class<? extends ResourceProvider>> excludedTypes
040        = new HashSet<>();
041    private Consumer<ResourceProvider> onBeforeFetch = _ -> {
042    };
043
044    /* default */ DefaultBoundResourceQuery(Stream<ResourceProvider> providers,
045            ResourceQuery<T> query) {
046        this.query = query;
047        this.providers = providers;
048    }
049
050    @Override
051    public BoundResourceQuery<T> without(ResourceProvider provider) {
052        excluded.add(provider);
053        return this;
054    }
055
056    @Override
057    public BoundResourceQuery<T>
058            without(Class<? extends ResourceProvider> providerType) {
059        excludedTypes.add(providerType);
060        return this;
061    }
062
063    @Override
064    public BoundResourceQuery<T>
065            onBeforeFetch(Consumer<ResourceProvider> hook) {
066        this.onBeforeFetch = hook;
067        return this;
068    }
069
070    @Override
071    public Stream<T> fetch() {
072        return providers.filter(p -> !excluded.contains(p))
073            .filter(p -> excludedTypes.stream()
074                .filter(t -> t.isAssignableFrom(p.getClass())).findAny()
075                .isEmpty())
076            .peek(onBeforeFetch::accept)
077            .flatMap(provider -> LauncherSupport.buildContext()
078                .get(provider, query));
079    }
080}