MCPcopy Create free account
hub / github.com/antlr/codebuff / Defaults

Class Defaults

corpus/java/training/guava/base/Defaults.java:33–69  ·  view source on GitHub ↗

This class provides default values for all Java types, as defined by the JLS. @author Ben Yu @since 1.0

Source from the content-addressed store, hash-verified

31 * @since 1.0
32 */
33@GwtIncompatible
34public final class Defaults {
35 private Defaults() {}
36
37 private static final Map<Class<?>, Object> DEFAULTS;
38
39 static {
40 // Only add to this map via put(Map, Class<T>, T)
41 Map<Class<?>, Object> map = new HashMap<Class<?>, Object>();
42 put(map, boolean.class, false);
43 put(map, char.class, '\0');
44 put(map, byte.class, (byte) 0);
45 put(map, short.class, (short) 0);
46 put(map, int.class, 0);
47 put(map, long.class, 0L);
48 put(map, float.class, 0f);
49 put(map, double.class, 0d);
50 DEFAULTS = Collections.unmodifiableMap(map);
51 }
52
53 private static <T> void put(Map<Class<?>, Object> map, Class<T> type, T value) {
54 map.put(type, value);
55 }
56
57 /**
58 * Returns the default value of {@code type} as defined by JLS --- {@code 0} for numbers, {@code
59 * false} for {@code boolean} and {@code '\0'} for {@code char}. For non-primitive types and
60 * {@code void}, {@code null} is returned.
61 */
62 @Nullable
63 public static <T> T defaultValue(Class<T> type) {
64 // Primitives.wrap(type).cast(...) would avoid the warning, but we can't use that from here
65 @SuppressWarnings("unchecked") // the put method enforces this key-value relationship
66 T t = (T) DEFAULTS.get(checkNotNull(type));
67 return t;
68 }
69}

Callers

nothing calls this directly

Calls 2

putMethod · 0.95
unmodifiableMapMethod · 0.45

Tested by

no test coverage detected