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

org.gradle.cache.internal.TestCrossBuildInMemoryCacheFactory.groovy Maven / Gradle / Ivy

/*
 * 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.cache.internal


import java.util.concurrent.ConcurrentHashMap
import java.util.function.Function
import java.util.function.Predicate

class TestCrossBuildInMemoryCacheFactory implements CrossBuildInMemoryCacheFactory {
    @Override
     CrossBuildInMemoryCache newCache() {
        return new TestCache()
    }

    @Override
     CrossBuildInMemoryCache newCacheRetainingDataFromPreviousBuild(Predicate retentionFilter) {
        return new TestCache()
    }

    @Override
     CrossBuildInMemoryCache, V> newClassCache() {
        return new TestCache, V>()
    }

    @Override
     CrossBuildInMemoryCache, V> newClassMap() {
        return new TestCache, V>()
    }

    static class TestCache implements CrossBuildInMemoryCache {
        private final Map values = new ConcurrentHashMap<>()

        @Override
        V getIfPresent(K key) {
            return values.get(key)
        }

        @Override
        V get(K key, Function factory) {
            def v = values.get(key)
            if (v == null) {
                v = factory.apply(key)
                values.put(key, v)
            }
            return v
        }

        @Override
        void put(K key, V value) {
            values.put(key, value)
        }

        @Override
        void clear() {
            values.clear()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy