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

org.gradle.api.internal.collections.IterationOrderRetainingSetElementSource 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.base.Objects;
import com.google.common.collect.Iterators;
import org.gradle.api.internal.provider.CollectionProviderInternal;
import org.gradle.api.internal.provider.ProviderInternal;
import org.gradle.api.specs.Spec;

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

public class IterationOrderRetainingSetElementSource extends AbstractIterationOrderRetainingElementSource {
    private final Spec> noDuplicates = new Spec>() {
        @Override
        public boolean isSatisfiedBy(ValuePointer pointer) {
            return !pointer.getElement().isDuplicate(pointer.getIndex());
        }
    };

    @Override
    public Iterator iterator() {
        realizePending();
        return new RealizedElementCollectionIterator(getInserted(), noDuplicates);
    }

    @Override
    public Iterator iteratorNoFlush() {
        return new RealizedElementCollectionIterator(getInserted(), noDuplicates);
    }

    @Override
    public boolean add(T element) {
        modCount++;
        if (!Iterators.contains(iteratorNoFlush(), element)) {
            getInserted().add(new Element(element));
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean addRealized(T value) {
        markDuplicates(value);
        return true;
    }

    @Override
    protected void clearCachedElement(Element element) {
        boolean wasRealized = element.isRealized();
        super.clearCachedElement(element);
        if (wasRealized) {
            for (T value : element.getValues()) {
                markDuplicates(value);
            }
        }
    }

    private void markDuplicates(T value) {
        boolean seen = false;
        for (Element element : getInserted()) {
            if (element.isRealized()) {
                List collected = element.getValues();
                for (int index = 0; index < collected.size(); index++) {
                    if (Objects.equal(collected.get(index), value)) {
                        if (seen) {
                            element.setDuplicate(index);
                        } else {
                            seen = true;
                        }
                    }
                }
            }
        }
    }

    @Override
    public boolean addPending(ProviderInternal provider) {
        modCount++;
        Element element = cachingElement(provider);
        if (!getInserted().contains(element)) {
            getInserted().add(element);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean addPendingCollection(CollectionProviderInternal> provider) {
        modCount++;
        Element element = cachingElement(provider);
        if (!getInserted().contains(element)) {
            getInserted().add(element);
            return true;
        } else {
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy