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

keycloakjar.com.github.benmanes.caffeine.cache.Weigher Maven / Gradle / Ivy

The newest version!
/*
 * 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 org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.nullness.qual.NonNull;

/**
 * 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)
 */
@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 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 {
  INSTANCE;

  @Override public int weigh(Object key, Object value) {
    return 1;
  }
}

final class BoundedWeigher implements Weigher, Serializable {
  static final long serialVersionUID = 1;
  final Weigher delegate;

  BoundedWeigher(Weigher delegate) {
    this.delegate = requireNonNull(delegate);
  }

  @Override
  public int weigh(K key, V value) {
    int weight = delegate.weigh(key, value);
    Caffeine.requireArgument(weight >= 0);
    return weight;
  }

  Object writeReplace() {
    return delegate;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy