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

org.gradle.api.internal.attributes.DefaultCompatibilityRuleChain Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2016 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.attributes;

import com.google.common.collect.Lists;
import org.gradle.api.Action;
import org.gradle.api.attributes.CompatibilityCheckDetails;

import java.util.Comparator;
import java.util.List;

public class DefaultCompatibilityRuleChain implements CompatibilityRuleChainInternal {

    private final List>> rules = Lists.newArrayList();

    private boolean assumeCompatibleWhenMissing;

    @Override
    public void ordered(Comparator comparator) {
        Action> rule = AttributeMatchingRules.orderedCompatibility(comparator, false);
        add(rule);
    }

    @Override
    public void reverseOrdered(Comparator comparator) {
        Action> rule = AttributeMatchingRules.orderedCompatibility(comparator, true);
        add(rule);
    }

    @Override
    public void add(Action> rule) {
        rules.add(rule);
    }

    @Override
    public void assumeCompatibleWhenMissing() {
        assumeCompatibleWhenMissing = true;
    }

    @Override
    public boolean isCompatibleWhenMissing() {
        return assumeCompatibleWhenMissing;
    }

    @Override
    public void execute(CompatibilityCheckDetails details) {
        State state = new State(details);
        for (Action> rule : rules) {
            rule.execute(state);
            if (state.determined) {
                return;
            }
        }
        if (!state.determined) {
            AttributeMatchingRules.equalityCompatibility().execute(state);
            if (state.determined) {
                return;
            }
        }
        // Eventually fail, always
        details.incompatible();
    }

    private static class State implements CompatibilityCheckDetails {
        private final CompatibilityCheckDetails delegate;
        private boolean determined;

        private State(CompatibilityCheckDetails delegate) {
            this.delegate = delegate;
        }

        @Override
        public T getConsumerValue() {
            return delegate.getConsumerValue();
        }

        @Override
        public T getProducerValue() {
            return delegate.getProducerValue();
        }

        @Override
        public void compatible() {
            determined = true;
            delegate.compatible();
        }

        @Override
        public void incompatible() {
            determined = true;
            delegate.incompatible();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy