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

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

The newest version!
/*
 * Copyright 2017 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 org.gradle.api.Transformer;
import org.gradle.api.provider.Provider;
import org.gradle.internal.Cast;
import org.gradle.internal.DisplayName;

import javax.annotation.Nullable;

public class Providers {
    private static final NoValueProvider NULL_PROVIDER = new NoValueProvider<>(ValueSupplier.Value.MISSING);

    public static final Provider TRUE = of(true);
    public static final Provider FALSE = of(false);

    public static  ProviderInternal fixedValue(DisplayName owner, T value, Class targetType, ValueSanitizer sanitizer) {
        value = sanitizer.sanitize(value);
        if (!targetType.isInstance(value)) {
            throw new IllegalArgumentException(String.format("Cannot set the value of %s of type %s using an instance of type %s.", owner.getDisplayName(), targetType.getName(), value.getClass().getName()));
        }
        return new FixedValueProvider<>(value);
    }

    public static  ProviderInternal nullableValue(ValueSupplier.Value value) {
        if (value.isMissing()) {
            if (value.getPathToOrigin().isEmpty()) {
                return notDefined();
            } else {
                return new NoValueProvider<>(value);
            }
        } else {
            return of(value.get());
        }
    }

    public static  ProviderInternal notDefined() {
        return Cast.uncheckedCast(NULL_PROVIDER);
    }

    public static  ProviderInternal of(T value) {
        if (value == null) {
            throw new IllegalArgumentException();
        }
        return new FixedValueProvider<>(value);
    }

    public static  ProviderInternal internal(final Provider value) {
        return Cast.uncheckedCast(value);
    }

    public static  ProviderInternal ofNullable(@Nullable T value) {
        if (value == null) {
            return notDefined();
        } else {
            return of(value);
        }
    }

    public static class FixedValueProvider extends AbstractProviderWithValue {
        private final T value;

        FixedValueProvider(T value) {
            this.value = value;
        }

        @Nullable
        @Override
        public Class getType() {
            return Cast.uncheckedCast(value.getClass());
        }

        @Override
        protected Value calculateOwnValue(ValueConsumer consumer) {
            return Value.of(value);
        }

        @Override
        public ProviderInternal withFinalValue(ValueConsumer consumer) {
            return this;
        }

        @Override
        public ExecutionTimeValue calculateExecutionTimeValue() {
            return ExecutionTimeValue.fixedValue(value);
        }

        @Override
        public String toString() {
            return String.format("fixed(%s, %s)", getType(), value);
        }
    }

    public static class FixedValueWithChangingContentProvider extends FixedValueProvider {
        public FixedValueWithChangingContentProvider(T value) {
            super(value);
        }

        @Override
        public ExecutionTimeValue calculateExecutionTimeValue() {
            return super.calculateExecutionTimeValue().withChangingContent();
        }
    }

    private static class NoValueProvider extends AbstractMinimalProvider {
        private final Value value;

        public NoValueProvider(Value value) {
            assert value.isMissing();
            this.value = value;
        }

        @Override
        public Value calculateValue(ValueConsumer consumer) {
            return value;
        }

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

        @Nullable
        @Override
        public Class getType() {
            return null;
        }

        @Override
        protected Value calculateOwnValue(ValueConsumer consumer) {
            return Value.missing();
        }

        @Override
        public ExecutionTimeValue calculateExecutionTimeValue() {
            return ExecutionTimeValue.missing();
        }

        @Override
        public  ProviderInternal map(Transformer transformer) {
            return Cast.uncheckedCast(this);
        }

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

        @Override
        public boolean calculatePresence(ValueConsumer consumer) {
            return false;
        }

        @Override
        public ProviderInternal asSupplier(DisplayName owner, Class targetType, ValueSanitizer sanitizer) {
            return this;
        }

        @Override
        public ProviderInternal withFinalValue(ValueConsumer consumer) {
            return this;
        }

        @Override
        public Provider orElse(T value) {
            return Providers.of(value);
        }

        @Override
        public Provider orElse(Provider provider) {
            return Cast.uncheckedCast(provider);
        }

        @Override
        public String toString() {
            return "undefined";
        }
    }
}