Class ArbitraryInstances
- java.lang.Object
-
- com.google.common.testing.ArbitraryInstances
-
public final class ArbitraryInstances extends java.lang.Object
Supplies an arbitrary "default" instance for a wide range of types, often useful in testing utilities.Covers arrays, enums and common types defined in
java.lang
,java.lang.reflect
,java.io
,java.nio
,java.math
,java.util
,java.util.concurrent
,java.util.regex
,com.google.common.base
,com.google.common.collect
andcom.google.common.primitives
. In addition, if the type exposes at least one public static final constant of the same type, one of the constants will be used; or if the class exposes a public parameter-less constructor then it will be "new"d and returned.All default instances returned by
get(java.lang.Class<T>)
are generics-safe. Clients won't get type errors for usingget(Comparator.class)
as aComparator<Foo>
, for example. Immutable empty instances are returned for collection types;""
for string;0
for number types; reasonable default instance for other stateless types. For mutable types, a fresh instance is created each timeget()
is called.- Since:
- 12.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ArbitraryInstances.AlwaysEqual
private static class
ArbitraryInstances.ByToString
private static class
ArbitraryInstances.Dummies
private static class
ArbitraryInstances.NullByteSink
-
Field Summary
Fields Modifier and Type Field Description private static Ordering<java.lang.reflect.Field>
BY_FIELD_NAME
private static ClassToInstanceMap<java.lang.Object>
DEFAULTS
private static java.util.concurrent.ConcurrentMap<java.lang.Class<?>,java.lang.Class<?>>
implementations
type → implementation.private static java.util.logging.Logger
logger
-
Constructor Summary
Constructors Modifier Constructor Description private
ArbitraryInstances()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static <T> T
arbitraryConstantInstanceOrNull(java.lang.Class<T> type)
private static <T> T
createEmptyArray(java.lang.Class<T> arrayType)
static <T> T
get(java.lang.Class<T> type)
Returns an arbitrary instance fortype
, ornull
if no arbitrary instance can be determined.private static <T> java.lang.Class<? extends T>
getImplementation(java.lang.Class<T> type)
private static java.util.regex.MatchResult
newMatchResult()
Returns a newMatchResult
that corresponds to a successful match.private static <T> void
setImplementation(java.lang.Class<T> type, java.lang.Class<? extends T> implementation)
-
-
-
Field Detail
-
BY_FIELD_NAME
private static final Ordering<java.lang.reflect.Field> BY_FIELD_NAME
-
DEFAULTS
private static final ClassToInstanceMap<java.lang.Object> DEFAULTS
-
implementations
private static final java.util.concurrent.ConcurrentMap<java.lang.Class<?>,java.lang.Class<?>> implementations
type → implementation. Inherently mutable interfaces and abstract classes are mapped to their default implementations and are "new"d upon get().
-
logger
private static final java.util.logging.Logger logger
-
-
Method Detail
-
newMatchResult
private static java.util.regex.MatchResult newMatchResult()
Returns a newMatchResult
that corresponds to a successful match. Apache Harmony (used in Android) requires a successful match in order to generate aMatchResult
: http://goo.gl/5VQFmC
-
setImplementation
private static <T> void setImplementation(java.lang.Class<T> type, java.lang.Class<? extends T> implementation)
-
getImplementation
private static <T> java.lang.Class<? extends T> getImplementation(java.lang.Class<T> type)
-
get
public static <T> T get(java.lang.Class<T> type)
Returns an arbitrary instance fortype
, ornull
if no arbitrary instance can be determined.
-
arbitraryConstantInstanceOrNull
private static <T> T arbitraryConstantInstanceOrNull(java.lang.Class<T> type)
-
createEmptyArray
private static <T> T createEmptyArray(java.lang.Class<T> arrayType)
-
-