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

org.gradle.api.internal.collections.DefaultPendingSource Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.api.internal.collections;

import com.google.common.collect.Lists;
import org.gradle.api.Action;
import org.gradle.api.internal.provider.CollectionProviderInternal;
import org.gradle.api.internal.provider.Collectors.*;
import org.gradle.api.internal.provider.ProviderInternal;

import java.util.Iterator;
import java.util.List;

public class DefaultPendingSource implements PendingSource {
    private final List> pending = Lists.newArrayList();
    private Action flushAction;

    @Override
    public void realizePending() {
        if (!pending.isEmpty()) {
            List> copied = Lists.newArrayList(pending);
            realize(copied);
        }
    }

    @Override
    public void realizePending(Class type) {
        if (!pending.isEmpty()) {
            List> copied = Lists.newArrayList();
            for (TypedCollector collector : pending) {
                if (collector.getType() == null || type.isAssignableFrom(collector.getType())) {
                    copied.add(collector);
                }
            }
            realize(copied);
        }
    }

    private void realize(Iterable> collectors) {
        for (TypedCollector collector : collectors) {
            if (flushAction != null) {
                pending.remove(collector);
                List realized = Lists.newArrayList();
                collector.collectInto(realized);
                for (T element : realized) {
                    flushAction.execute(element);
                }
            } else {
                throw new IllegalStateException("Cannot realize pending elements when realize action is not set");
            }
        }
    }

    @Override
    public boolean addPending(ProviderInternal provider) {
        return pending.add(new TypedCollector(provider.getType(), new ElementFromProvider(provider)));
    }

    @Override
    public boolean removePending(ProviderInternal provider) {
        return removeByProvider(provider);
    }

    private boolean removeByProvider(ProviderInternal provider) {
        Iterator> iterator = pending.iterator();
        while (iterator.hasNext()) {
            TypedCollector collector = iterator.next();
            if (collector.isProvidedBy(provider)) {
                iterator.remove();
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean addPendingCollection(CollectionProviderInternal> provider) {
        return pending.add(new TypedCollector(provider.getElementType(), new ElementsFromCollectionProvider(provider)));
    }

    @Override
    public boolean removePendingCollection(CollectionProviderInternal> provider) {
        return removeByProvider(provider);
    }

    @Override
    public void onRealize(Action action) {
        this.flushAction = action;
    }

    @Override
    public void realizeExternal(ProviderInternal provider) {
        removePending(provider);
    }

    @Override
    public boolean isEmpty() {
        return pending.isEmpty();
    }

    @Override
    public int size() {
        int count = 0;
        for (TypedCollector collector : pending) {
            count += collector.size();
        }
        return count;
    }

    @Override
    public void clear() {
        pending.clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy