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) 2012 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.testing;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Equivalence;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Ticker;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.ImmutableSortedMultiset;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.LinkedHashMultiset;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.Ordering;
import com.google.common.collect.Range;
import com.google.common.collect.RowSortedTable;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.google.common.collect.SortedMultiset;
import com.google.common.collect.Table;
import com.google.common.collect.TreeBasedTable;
import com.google.common.collect.TreeMultiset;
import com.google.common.primitives.Primitives;
import com.google.common.primitives.UnsignedInteger;
import com.google.common.primitives.UnsignedLong;
import com.google.common.reflect.AbstractInvocationHandler;
import com.google.common.reflect.Invokable;
import com.google.common.reflect.Parameter;
import com.google.common.reflect.Reflection;
import com.google.common.reflect.TypeToken;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Currency;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Generates fresh instances of types that are different from each other (if possible).
*
* @author Ben Yu
*/
@GwtIncompatible
@J2ktIncompatible
class FreshValueGenerator {
private static final ImmutableMap, Method> GENERATORS;
static {
ImmutableMap.Builder, Method> builder = ImmutableMap.builder();
for (Method method : FreshValueGenerator.class.getDeclaredMethods()) {
if (method.isAnnotationPresent(Generates.class)) {
builder.put(method.getReturnType(), method);
}
}
GENERATORS = builder.buildOrThrow();
}
private static final ImmutableMap, Method> EMPTY_GENERATORS;
static {
ImmutableMap.Builder, Method> builder = ImmutableMap.builder();
for (Method method : FreshValueGenerator.class.getDeclaredMethods()) {
if (method.isAnnotationPresent(Empty.class)) {
builder.put(method.getReturnType(), method);
}
}
EMPTY_GENERATORS = builder.buildOrThrow();
}
private final AtomicInteger freshness = new AtomicInteger(1);
private final ListMultimap, Object> sampleInstances = ArrayListMultimap.create();
/**
* The freshness level at which the {@link Empty @Empty} annotated method was invoked to generate
* instance.
*/
private final Map emptyInstanceGenerated = new HashMap<>();
final void addSampleInstances(Class type, Iterable extends T> instances) {
sampleInstances.putAll(checkNotNull(type), checkNotNull(instances));
}
/**
* Returns a fresh instance for {@code type} if possible. The returned instance could be:
*
*
*
exactly of the given type, including generic type parameters, such as {@code
* ImmutableList};
*
of the raw type;
*
null if no value can be generated.
*
*/
final @Nullable Object generateFresh(TypeToken> type) {
Object generated = generate(type);
if (generated != null) {
freshness.incrementAndGet();
}
return generated;
}
final @Nullable T generateFresh(Class type) {
return Primitives.wrap(type).cast(generateFresh(TypeToken.of(type)));
}
final T newFreshProxy(final Class interfaceType) {
T proxy = newProxy(interfaceType);
freshness.incrementAndGet();
return proxy;
}
/**
* Generates an instance for {@code type} using the current {@link #freshness}. The generated
* instance may or may not be unique across different calls.
*/
private @Nullable Object generate(TypeToken> type) {
Class> rawType = type.getRawType();
List