001/*
002 * Copyright (C) 2015-2021 KeepSafe Software
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package com.getkeepsafe.dexcount.plugin;
017
018import com.android.repository.Revision;
019import com.android.repository.Revision.PreviewComparison;
020
021import java.util.Arrays;
022import java.util.List;
023import java.util.Optional;
024
025/**
026 * Utility for obtaining an appropriate task applicator factory for a given
027 * Android Gradle Plugin revision.
028 */
029public final class TaskApplicators {
030    private TaskApplicators() {
031        // no instances
032    }
033
034    public static Optional<TaskApplicator.Factory> getFactory(Revision revision) {
035        List<TaskApplicator.Factory> factories = Arrays.asList(
036            new SevenOhApplicator.Factory(),
037            new FourTwoApplicator.Factory(),
038            new FourOneApplicator.Factory(),
039            new ThreeSixApplicator.Factory(),
040            new ThreeFourApplicator.Factory(),
041            new JavaOnlyApplicator.Factory()
042        );
043
044        return factories.stream()
045            .filter(it -> revision.compareTo(it.getMinimumRevision(), PreviewComparison.IGNORE) >= 0)
046            .findFirst();
047    }
048}