com.github.benmanes.caffeine.cache.Weigher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of caffeine Show documentation
Show all versions of caffeine Show documentation
A high performance caching library
/*
* Copyright 2014 Ben Manes. All Rights Reserved.
*
* 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 com.github.benmanes.caffeine.cache;
import static java.util.Objects.requireNonNull;
import java.io.Serializable;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
/**
* Calculates the weights of cache entries. The total weight threshold is used to determine when an
* eviction is required.
*
* @param the type of keys
* @param the type of values
* @author [email protected] (Ben Manes)
*/
@ThreadSafe
@FunctionalInterface
public interface Weigher {
/**
* Returns the weight of a cache entry. There is no unit for entry weights; rather they are simply
* relative to each other.
*
* @param key the key to weigh
* @param value the value to weigh
* @return the weight of the entry; must be non-negative
*/
@Nonnegative
int weigh(@Nonnull K key, @Nonnull V value);
/**
* Returns a weigher where an entry has a weight of {@code 1}.
*
* @param the type of keys
* @param the type of values
* @return a weigher where an entry has a weight of {@code 1}
*/
@Nonnull
static Weigher singletonWeigher() {
@SuppressWarnings("unchecked")
Weigher self = (Weigher) SingletonWeigher.INSTANCE;
return self;
}
/**
* Returns a weigher that enforces that the weight is non-negative.
*
* @param delegate the weigher to that weighs the entry
* @param the type of keys
* @param the type of values
* @return a weigher that enforces that the weight is non-negative
*/
@Nonnull
static Weigher boundedWeigher(@Nonnull Weigher delegate) {
return new BoundedWeigher<>(delegate);
}
}
enum SingletonWeigher implements Weigher
© 2015 - 2025 Weber Informatics LLC | Privacy Policy