1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/*
* 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.google.common.cache;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.cache.LocalCache.Strength;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.CheckForNull;
/**
* Helper class for creating {@link CacheBuilder} instances with all combinations of several sets of
* parameters.
*
* @author mike nonemacher
*/
class CacheBuilderFactory {
// Default values contain only 'null', which means don't call the CacheBuilder method (just give
// the CacheBuilder default).
private Set<Integer> concurrencyLevels = Sets.newHashSet((Integer) null);
private Set<Integer> initialCapacities = Sets.newHashSet((Integer) null);
private Set<Integer> maximumSizes = Sets.newHashSet((Integer) null);
private Set<DurationSpec> expireAfterWrites = Sets.newHashSet((DurationSpec) null);
private Set<DurationSpec> expireAfterAccesses = Sets.newHashSet((DurationSpec) null);
private Set<DurationSpec> refreshes = Sets.newHashSet((DurationSpec) null);
private Set<Strength> keyStrengths = Sets.newHashSet((Strength) null);
private Set<Strength> valueStrengths = Sets.newHashSet((Strength) null);
CacheBuilderFactory withConcurrencyLevels(Set<Integer> concurrencyLevels) {
this.concurrencyLevels = Sets.newLinkedHashSet(concurrencyLevels);
return this;
}
CacheBuilderFactory withInitialCapacities(Set<Integer> initialCapacities) {
this.initialCapacities = Sets.newLinkedHashSet(initialCapacities);
return this;
}
CacheBuilderFactory withMaximumSizes(Set<Integer> maximumSizes) {
this.maximumSizes = Sets.newLinkedHashSet(maximumSizes);
return this;
}
CacheBuilderFactory withExpireAfterWrites(Set<DurationSpec> durations) {
this.expireAfterWrites = Sets.newLinkedHashSet(durations);
return this;
}
CacheBuilderFactory withExpireAfterAccesses(Set<DurationSpec> durations) {
this.expireAfterAccesses = Sets.newLinkedHashSet(durations);
return this;
}
CacheBuilderFactory withRefreshes(Set<DurationSpec> durations) {
this.refreshes = Sets.newLinkedHashSet(durations);
return this;
}
CacheBuilderFactory withKeyStrengths(Set<Strength> keyStrengths) {
this.keyStrengths = Sets.newLinkedHashSet(keyStrengths);
Preconditions.checkArgument(!this.keyStrengths.contains(Strength.SOFT));
return this;
}
CacheBuilderFactory withValueStrengths(Set<Strength> valueStrengths) {
this.valueStrengths = Sets.newLinkedHashSet(valueStrengths);
return this;
}
Iterable<CacheBuilder<Object, Object>> buildAllPermutations() {
@SuppressWarnings("unchecked")
Iterable<List<Object>> combinations =
buildCartesianProduct(
concurrencyLevels,
initialCapacities,
maximumSizes,
expireAfterWrites,
expireAfterAccesses,
refreshes,
keyStrengths,
valueStrengths);
return Iterables.transform(
combinations,
new Function<List<Object>, CacheBuilder<Object, Object>>() {
@Override
public CacheBuilder<Object, Object> apply(List<Object> combination) {
return createCacheBuilder(
(Integer) combination.get(0),
(Integer) combination.get(1),
(Integer) combination.get(2),
(DurationSpec) combination.get(3),
(DurationSpec) combination.get(4),
(DurationSpec) combination.get(5),
(Strength) combination.get(6),
(Strength) combination.get(7));
}
});
}
private static final Function<Object, Optional<?>> NULLABLE_TO_OPTIONAL =
new Function<Object, Optional<?>>() {
@Override
public Optional<?> apply(@CheckForNull Object obj) {
return Optional.fromNullable(obj);
}
};
private static final Function<Optional<?>, Object> OPTIONAL_TO_NULLABLE =
new Function<Optional<?>, Object>() {
@Override
public Object apply(Optional<?> optional) {
return optional.orNull();
}
};
/**
* Sets.cartesianProduct doesn't allow sets that contain null, but we want null to mean "don't
* call the associated CacheBuilder method" - that is, get the default CacheBuilder behavior. This
* method wraps the elements in the input sets (which may contain null) as Optionals, calls
* Sets.cartesianProduct with those, then transforms the result to unwrap the Optionals.
*/
private Iterable<List<Object>> buildCartesianProduct(Set<?>... sets) {
List<Set<Optional<?>>> optionalSets = Lists.newArrayListWithExpectedSize(sets.length);
for (Set<?> set : sets) {
Set<Optional<?>> optionalSet =
Sets.newLinkedHashSet(Iterables.transform(set, NULLABLE_TO_OPTIONAL));
optionalSets.add(optionalSet);
}
Set<List<Optional<?>>> cartesianProduct = Sets.cartesianProduct(optionalSets);
return Iterables.transform(
cartesianProduct,
new Function<List<Optional<?>>, List<Object>>() {
@Override
public List<Object> apply(List<Optional<?>> objs) {
return Lists.transform(objs, OPTIONAL_TO_NULLABLE);
}
});
}
private CacheBuilder<Object, Object> createCacheBuilder(
Integer concurrencyLevel,
Integer initialCapacity,
Integer maximumSize,
DurationSpec expireAfterWrite,
DurationSpec expireAfterAccess,
DurationSpec refresh,
Strength keyStrength,
Strength valueStrength) {
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
if (concurrencyLevel != null) {
builder.concurrencyLevel(concurrencyLevel);
}
if (initialCapacity != null) {
builder.initialCapacity(initialCapacity);
}
if (maximumSize != null) {
builder.maximumSize(maximumSize);
}
if (expireAfterWrite != null) {
builder.expireAfterWrite(expireAfterWrite.duration, expireAfterWrite.unit);
}
if (expireAfterAccess != null) {
builder.expireAfterAccess(expireAfterAccess.duration, expireAfterAccess.unit);
}
if (refresh != null) {
builder.refreshAfterWrite(refresh.duration, refresh.unit);
}
if (keyStrength != null) {
builder.setKeyStrength(keyStrength);
}
if (valueStrength != null) {
builder.setValueStrength(valueStrength);
}
return builder;
}
static class DurationSpec {
private final long duration;
private final TimeUnit unit;
private DurationSpec(long duration, TimeUnit unit) {
this.duration = duration;
this.unit = unit;
}
public static DurationSpec of(long duration, TimeUnit unit) {
return new DurationSpec(duration, unit);
}
@Override
public int hashCode() {
return Objects.hashCode(duration, unit);
}
@Override
public boolean equals(Object o) {
if (o instanceof DurationSpec) {
DurationSpec that = (DurationSpec) o;
return unit.toNanos(duration) == that.unit.toNanos(that.duration);
}
return false;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("duration", duration)
.add("unit", unit)
.toString();
}
}
}
|