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

org.gradle.internal.ImmutableActionSet Maven / Gradle / Ivy

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


import com.google.common.collect.ImmutableSet;
import org.gradle.api.Action;

/**
 * An immutable composite {@link Action} implementation which has set semantics. Optimized for high execute to mutate ratio, and for a small number of actions.
 *
 * This set also INTENTIONALLY ignores {@link Actions#doNothing()} actions and empty sets as to avoid growing for something that would never do anything.
 *
 * Actions are executed in order of insertion. Duplicates are ignored. Execution stops on the first failure.
 *
 * @param  the type of the subject of the action
 */
public abstract class ImmutableActionSet implements Action {
    private static final ImmutableActionSet EMPTY = new EmptySet();

    /**
     * Creates an empty action set.
     */
    public static  ImmutableActionSet empty() {
        return Cast.uncheckedCast(EMPTY);
    }

    /**
     * Creates an action set.
     */
    public static  ImmutableActionSet of(Action... actions) {
        if (actions.length == 0) {
            return empty();
        }

        ImmutableSet.Builder> builder = ImmutableSet.builder();
        for (Action action : actions) {
            if (action == Actions.DO_NOTHING || (action instanceof EmptySet)) {
                continue;
            }
            unpackAction(action, builder);
        }
        ImmutableSet> set = builder.build();
        if (set.isEmpty()) {
            return empty();
        }
        if (set.size() == 1) {
            return new SingletonSet(set.iterator().next());
        }
        return new CompositeSet(set);
    }

    private static  void unpackAction(Action action, ImmutableSet.Builder> builder) {
        if (action instanceof SingletonSet) {
            SingletonSet singletonSet = (SingletonSet) action;
            builder.add(singletonSet.singleAction);
        } else if (action instanceof CompositeSet) {
            CompositeSet compositeSet = (CompositeSet) action;
            builder.addAll(compositeSet.multipleActions);
        } else {
            builder.add(action);
        }
    }

    /**
     * Creates a new set that runs the actions of this set plus the given action.
     */
    public ImmutableActionSet add(Action action) {
        if (action == Actions.DO_NOTHING || action instanceof EmptySet || action == this) {
            return this;
        }
        if (action instanceof SingletonSet) {
            SingletonSet singletonSet = (SingletonSet) action;
            return doAdd(singletonSet.singleAction);
        }
        if (action instanceof CompositeSet) {
            CompositeSet compositeSet = (CompositeSet) action;
            return doAddAll(compositeSet);
        }
        return doAdd(action);
    }

    /**
     * Does this set do anything?
     */
    public abstract boolean isEmpty();

    abstract ImmutableActionSet doAddAll(CompositeSet source);

    abstract ImmutableActionSet doAdd(Action action);

    private static class EmptySet extends ImmutableActionSet {
        @Override
        ImmutableActionSet doAdd(Action action) {
            return new SingletonSet(action);
        }

        @Override
        ImmutableActionSet doAddAll(CompositeSet source) {
            return source;
        }

        @Override
        public void execute(Object o) {
        }

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

    private static class SingletonSet extends ImmutableActionSet {
        private final Action singleAction;

        SingletonSet(Action singleAction) {
            this.singleAction = singleAction;
        }

        @Override
        ImmutableActionSet doAdd(Action action) {
            if (action.equals(singleAction)) {
                return this;
            }
            ImmutableSet> of = Cast.uncheckedCast(ImmutableSet.of(singleAction, action));
            return new CompositeSet(of);
        }

        @Override
        ImmutableActionSet doAddAll(CompositeSet source) {
            if (source.multipleActions.contains(singleAction)) {
                return source;
            }
            ImmutableSet.Builder> builder = ImmutableSet.builder();
            builder.add(singleAction);
            builder.addAll(source.multipleActions);
            return new CompositeSet(builder.build());
        }

        @Override
        public void execute(T t) {
            singleAction.execute(t);
        }

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

    private static class CompositeSet extends ImmutableActionSet {
        private final ImmutableSet> multipleActions;

        CompositeSet(ImmutableSet> multipleActions) {
            this.multipleActions = multipleActions;
        }

        @Override
        ImmutableActionSet doAdd(Action action) {
            if (multipleActions.contains(action)) {
                return this;
            }
            ImmutableSet.Builder> builder = ImmutableSet.builder();
            builder.addAll(multipleActions);
            builder.add(action);
            return new CompositeSet(builder.build());
        }

        @Override
        ImmutableActionSet doAddAll(CompositeSet source) {
            if (multipleActions.containsAll(source.multipleActions)) {
                return this;
            }
            ImmutableSet.Builder> builder = ImmutableSet.builder();
            builder.addAll(multipleActions);
            builder.addAll(source.multipleActions);
            return new CompositeSet(builder.build());
        }

        @Override
        public void execute(T t) {
            for (Action action : multipleActions) {
                action.execute(t);
            }
        }

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