A specification of a CacheBuilder configuration. CacheBuilderSpec supports parsing configuration off of a string, which makes it especially useful for command-line configuration of a CacheBuilder. The string syntax is a series of comma-separated keys or key-value pair
| 77 | * @since 12.0 |
| 78 | */ |
| 79 | @GwtIncompatible |
| 80 | public final class CacheBuilderSpec { |
| 81 | /** Parses a single value. */ |
| 82 | private interface ValueParser { |
| 83 | void parse(CacheBuilderSpec spec, String key, @Nullable String value); |
| 84 | } |
| 85 | |
| 86 | /** Splits each key-value pair. */ |
| 87 | private static final Splitter KEYS_SPLITTER = Splitter.on(',').trimResults(); |
| 88 | |
| 89 | /** Splits the key from the value. */ |
| 90 | private static final Splitter KEY_VALUE_SPLITTER = Splitter.on('=').trimResults(); |
| 91 | |
| 92 | /** Map of names to ValueParser. */ |
| 93 | private static final ImmutableMap<String, ValueParser> VALUE_PARSERS = |
| 94 | ImmutableMap.<String, ValueParser>builder() |
| 95 | .put("initialCapacity", new InitialCapacityParser()) |
| 96 | .put("maximumSize", new MaximumSizeParser()) |
| 97 | .put("maximumWeight", new MaximumWeightParser()) |
| 98 | .put("concurrencyLevel", new ConcurrencyLevelParser()) |
| 99 | .put("weakKeys", new KeyStrengthParser(Strength.WEAK)) |
| 100 | .put("softValues", new ValueStrengthParser(Strength.SOFT)) |
| 101 | .put("weakValues", new ValueStrengthParser(Strength.WEAK)) |
| 102 | .put("recordStats", new RecordStatsParser()) |
| 103 | .put("expireAfterAccess", new AccessDurationParser()) |
| 104 | .put("expireAfterWrite", new WriteDurationParser()) |
| 105 | .put("refreshAfterWrite", new RefreshDurationParser()) |
| 106 | .put("refreshInterval", new RefreshDurationParser()) |
| 107 | .build(); |
| 108 | |
| 109 | @VisibleForTesting Integer initialCapacity; |
| 110 | @VisibleForTesting Long maximumSize; |
| 111 | @VisibleForTesting Long maximumWeight; |
| 112 | @VisibleForTesting Integer concurrencyLevel; |
| 113 | @VisibleForTesting Strength keyStrength; |
| 114 | @VisibleForTesting Strength valueStrength; |
| 115 | @VisibleForTesting Boolean recordStats; |
| 116 | @VisibleForTesting long writeExpirationDuration; |
| 117 | @VisibleForTesting TimeUnit writeExpirationTimeUnit; |
| 118 | @VisibleForTesting long accessExpirationDuration; |
| 119 | @VisibleForTesting TimeUnit accessExpirationTimeUnit; |
| 120 | @VisibleForTesting long refreshDuration; |
| 121 | @VisibleForTesting TimeUnit refreshTimeUnit; |
| 122 | /** Specification; used for toParseableString(). */ |
| 123 | private final String specification; |
| 124 | |
| 125 | private CacheBuilderSpec(String specification) { |
| 126 | this.specification = specification; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Creates a CacheBuilderSpec from a string. |
| 131 | * |
| 132 | * @param cacheBuilderSpecification the string form |
| 133 | */ |
| 134 | public static CacheBuilderSpec parse(String cacheBuilderSpecification) { |
| 135 | CacheBuilderSpec spec = new CacheBuilderSpec(cacheBuilderSpecification); |
| 136 | if (!cacheBuilderSpecification.isEmpty()) { |
nothing calls this directly
no test coverage detected