Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2011 The Guava 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 com.facebook.presto.jdbc.internal.guava.cache;
import static com.facebook.presto.jdbc.internal.guava.base.Preconditions.checkArgument;
import com.facebook.presto.jdbc.internal.guava.annotations.GwtIncompatible;
import com.facebook.presto.jdbc.internal.guava.annotations.VisibleForTesting;
import com.facebook.presto.jdbc.internal.guava.base.MoreObjects;
import com.facebook.presto.jdbc.internal.guava.base.Objects;
import com.facebook.presto.jdbc.internal.guava.base.Splitter;
import com.facebook.presto.jdbc.internal.guava.cache.LocalCache.Strength;
import com.facebook.presto.jdbc.internal.guava.collect.ImmutableList;
import com.facebook.presto.jdbc.internal.guava.collect.ImmutableMap;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A specification of a {@link CacheBuilder} configuration.
*
*
{@code CacheBuilderSpec} supports parsing configuration off of a string, which makes it
* especially useful for command-line configuration of a {@code CacheBuilder}.
*
*
The string syntax is a series of comma-separated keys or key-value pairs, each corresponding
* to a {@code CacheBuilder} method.
*
*
The set of supported keys will grow as {@code CacheBuilder} evolves, but existing keys will
* never be removed.
*
*
Durations are represented by an integer, followed by one of "d", "h", "m", or "s",
* representing days, hours, minutes, or seconds respectively. (There is currently no syntax to
* request expiration in milliseconds, microseconds, or nanoseconds.)
*
*
Whitespace before and after commas and equal signs is ignored. Keys may not be repeated; it is
* also illegal to use the following pairs of keys in a single value:
*
*
*
{@code maximumSize} and {@code maximumWeight}
*
{@code softValues} and {@code weakValues}
*
*
*
{@code CacheBuilderSpec} does not support configuring {@code CacheBuilder} methods with
* non-value parameters. These must be configured in code.
*
*
A new {@code CacheBuilder} can be instantiated from a {@code CacheBuilderSpec} using {@link
* CacheBuilder#from(CacheBuilderSpec)} or {@link CacheBuilder#from(String)}.
*
* @author Adam Winer
* @since 12.0
*/
@GwtIncompatible
public final class CacheBuilderSpec {
/** Parses a single value. */
private interface ValueParser {
void parse(CacheBuilderSpec spec, String key, @Nullable String value);
}
/** Splits each key-value pair. */
private static final Splitter KEYS_SPLITTER = Splitter.on(',').trimResults();
/** Splits the key from the value. */
private static final Splitter KEY_VALUE_SPLITTER = Splitter.on('=').trimResults();
/** Map of names to ValueParser. */
private static final ImmutableMap VALUE_PARSERS =
ImmutableMap.builder()
.put("initialCapacity", new InitialCapacityParser())
.put("maximumSize", new MaximumSizeParser())
.put("maximumWeight", new MaximumWeightParser())
.put("concurrencyLevel", new ConcurrencyLevelParser())
.put("weakKeys", new KeyStrengthParser(Strength.WEAK))
.put("softValues", new ValueStrengthParser(Strength.SOFT))
.put("weakValues", new ValueStrengthParser(Strength.WEAK))
.put("recordStats", new RecordStatsParser())
.put("expireAfterAccess", new AccessDurationParser())
.put("expireAfterWrite", new WriteDurationParser())
.put("refreshAfterWrite", new RefreshDurationParser())
.put("refreshInterval", new RefreshDurationParser())
.build();
@MonotonicNonNull @VisibleForTesting Integer initialCapacity;
@MonotonicNonNull @VisibleForTesting Long maximumSize;
@MonotonicNonNull @VisibleForTesting Long maximumWeight;
@MonotonicNonNull @VisibleForTesting Integer concurrencyLevel;
@MonotonicNonNull @VisibleForTesting Strength keyStrength;
@MonotonicNonNull @VisibleForTesting Strength valueStrength;
@MonotonicNonNull @VisibleForTesting Boolean recordStats;
@VisibleForTesting long writeExpirationDuration;
@MonotonicNonNull @VisibleForTesting TimeUnit writeExpirationTimeUnit;
@VisibleForTesting long accessExpirationDuration;
@MonotonicNonNull @VisibleForTesting TimeUnit accessExpirationTimeUnit;
@VisibleForTesting long refreshDuration;
@MonotonicNonNull @VisibleForTesting TimeUnit refreshTimeUnit;
/** Specification; used for toParseableString(). */
private final String specification;
private CacheBuilderSpec(String specification) {
this.specification = specification;
}
/**
* Creates a CacheBuilderSpec from a string.
*
* @param cacheBuilderSpecification the string form
*/
public static CacheBuilderSpec parse(String cacheBuilderSpecification) {
CacheBuilderSpec spec = new CacheBuilderSpec(cacheBuilderSpecification);
if (!cacheBuilderSpecification.isEmpty()) {
for (String keyValuePair : KEYS_SPLITTER.split(cacheBuilderSpecification)) {
List keyAndValue = ImmutableList.copyOf(KEY_VALUE_SPLITTER.split(keyValuePair));
checkArgument(!keyAndValue.isEmpty(), "blank key-value pair");
checkArgument(
keyAndValue.size() <= 2,
"key-value pair %s with more than one equals sign",
keyValuePair);
// Find the ValueParser for the current key.
String key = keyAndValue.get(0);
ValueParser valueParser = VALUE_PARSERS.get(key);
checkArgument(valueParser != null, "unknown key %s", key);
String value = keyAndValue.size() == 1 ? null : keyAndValue.get(1);
valueParser.parse(spec, key, value);
}
}
return spec;
}
/** Returns a CacheBuilderSpec that will prevent caching. */
public static CacheBuilderSpec disableCaching() {
// Maximum size of zero is one way to block caching
return CacheBuilderSpec.parse("maximumSize=0");
}
/** Returns a CacheBuilder configured according to this instance's specification. */
CacheBuilder