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

org.gradle.api.internal.provider.MapCollectors Maven / Gradle / Ivy

There is a newer version: 8.11.1
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.provider;

import com.google.common.base.Objects;
import org.gradle.api.internal.tasks.TaskDependencyResolveContext;

import java.util.Collection;
import java.util.Map;

public class MapCollectors {

    public static class EmptyMap implements MapCollector {

        @Override
        public boolean present() {
            return true;
        }

        @Override
        public void collectInto(MapEntryCollector collector, Map dest) {
        }

        @Override
        public boolean maybeCollectInto(MapEntryCollector collector, Map dest) {
            return true;
        }

        @Override
        public void collectKeysInto(ValueCollector collector, Collection dest) {
        }

        @Override
        public boolean maybeCollectKeysInto(ValueCollector collector, Collection dest) {
            return true;
        }

        @Override
        public boolean maybeVisitBuildDependencies(TaskDependencyResolveContext context) {
            return true;
        }
    }

    public static class SingleEntry implements MapCollector {

        private final K key;
        private final V value;

        public SingleEntry(K key, V value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public boolean present() {
            return true;
        }

        @Override
        public void collectInto(MapEntryCollector collector, Map dest) {
            collector.add(key, value, dest);
        }

        @Override
        public boolean maybeCollectInto(MapEntryCollector collector, Map dest) {
            collectInto(collector, dest);
            return true;
        }

        @Override
        public void collectKeysInto(ValueCollector collector, Collection dest) {
            collector.add(key, dest);
        }

        @Override
        public boolean maybeCollectKeysInto(ValueCollector collector, Collection dest) {
            collectKeysInto(collector, dest);
            return true;
        }

        @Override
        public boolean maybeVisitBuildDependencies(TaskDependencyResolveContext context) {
            return false;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            SingleEntry that = (SingleEntry) o;
            return Objects.equal(key, that.key) && Objects.equal(value, that.value);
        }

        @Override
        public int hashCode() {
            return Objects.hashCode(key, value);
        }
    }

    public static class EntryWithValueFromProvider implements MapCollector {

        private final K key;
        private final ProviderInternal providerOfValue;

        public EntryWithValueFromProvider(K key, ProviderInternal providerOfValue) {
            this.key = key;
            this.providerOfValue = providerOfValue;
        }

        @Override
        public boolean present() {
            return providerOfValue.isPresent();
        }

        @Override
        public void collectInto(MapEntryCollector collector, Map dest) {
            collector.add(key, providerOfValue.get(), dest);
        }

        @Override
        public boolean maybeCollectInto(MapEntryCollector collector, Map dest) {
            V value = providerOfValue.getOrNull();
            if (value != null) {
                collector.add(key, value, dest);
                return true;
            } else {
                return false;
            }
        }

        @Override
        public void collectKeysInto(ValueCollector collector, Collection dest) {
            if (providerOfValue.isPresent()) {
                collector.add(key, dest);
            } else {
                throw new IllegalStateException(Providers.NULL_VALUE);
            }
        }

        @Override
        public boolean maybeCollectKeysInto(ValueCollector collector, Collection dest) {
            if (providerOfValue.isPresent()) {
                collector.add(key, dest);
                return true;
            } else {
                return false;
            }
        }

        @Override
        public boolean maybeVisitBuildDependencies(TaskDependencyResolveContext context) {
            return providerOfValue.maybeVisitBuildDependencies(context);
        }
    }

    public static class EntriesFromMap implements MapCollector {

        private final Map entries;

        public EntriesFromMap(Map entries) {
            this.entries = entries;
        }

        @Override
        public boolean present() {
            return true;
        }

        @Override
        public void collectInto(MapEntryCollector collector, Map dest) {
            collector.addAll(entries.entrySet(), dest);
        }

        @Override
        public boolean maybeCollectInto(MapEntryCollector collector, Map dest) {
            collectInto(collector, dest);
            return true;
        }

        @Override
        public void collectKeysInto(ValueCollector collector, Collection dest) {
            collector.addAll(entries.keySet(), dest);
        }

        @Override
        public boolean maybeCollectKeysInto(ValueCollector collector, Collection dest) {
            collectKeysInto(collector, dest);
            return true;
        }

        @Override
        public boolean maybeVisitBuildDependencies(TaskDependencyResolveContext context) {
            return false;
        }
    }

    public static class EntriesFromMapProvider implements MapCollector {

        private final ProviderInternal> providerOfEntries;

        public EntriesFromMapProvider(ProviderInternal> providerOfEntries) {
            this.providerOfEntries = providerOfEntries;
        }

        @Override
        public boolean present() {
            return providerOfEntries.isPresent();
        }

        @Override
        public void collectInto(MapEntryCollector collector, Map dest) {
            collector.addAll(providerOfEntries.get().entrySet(), dest);
        }

        @Override
        public boolean maybeCollectInto(MapEntryCollector collector, Map dest) {
            Map entries = providerOfEntries.getOrNull();
            if (entries != null) {
                collector.addAll(entries.entrySet(), dest);
                return true;
            } else {
                return false;
            }
        }

        @Override
        public void collectKeysInto(ValueCollector collector, Collection dest) {
            collector.addAll(providerOfEntries.get().keySet(), dest);
        }

        @Override
        public boolean maybeCollectKeysInto(ValueCollector collector, Collection dest) {
            Map entries = providerOfEntries.getOrNull();
            if (entries != null) {
                collector.addAll(entries.keySet(), dest);
                return true;
            } else {
                return false;
            }
        }

        @Override
        public boolean maybeVisitBuildDependencies(TaskDependencyResolveContext context) {
            return providerOfEntries.maybeVisitBuildDependencies(context);
        }
    }

    public static class NoValue implements MapCollector {

        @Override
        public boolean present() {
            return false;
        }

        @Override
        public void collectInto(MapEntryCollector collector, Map dest) {
            throw new IllegalStateException(Providers.NULL_VALUE);
        }

        @Override
        public boolean maybeCollectInto(MapEntryCollector collector, Map dest) {
            return false;
        }

        @Override
        public void collectKeysInto(ValueCollector collector, Collection dest) {
            throw new IllegalStateException(Providers.NULL_VALUE);
        }

        @Override
        public boolean maybeCollectKeysInto(ValueCollector collector, Collection dest) {
            return false;
        }

        @Override
        public boolean maybeVisitBuildDependencies(TaskDependencyResolveContext context) {
            return true;
        }
    }
}