private <T> Map<Entity, T> coerceValues(Map<Entity, T> values, Class<? super T> type) { Map<Entity, T> typedValues = MutableMap.of(); for (Entry<Entity, T> entry : values.entrySet()) { @SuppressWarnings("unchecked") T typedValue = (T) TypeCoercions.coerce(entry.getValue(), type); typedValues.put(entry.getKey(), typedValue); } return typedValues; }
protected static <T> T getMandatoryVal(Map<String,?> map, ConfigKey<T> keyC) { String key = keyC.getName(); checkArgument(map.containsKey(key), "must contain key '"+keyC+"'"); return TypeCoercions.coerce(map.get(key), keyC.getTypeToken()); }
public AbstractEntityAdjunct(@SuppressWarnings("rawtypes") Map properties) { super(properties); _legacyNoConstructionInit = (properties != null) && Boolean.TRUE.equals(properties.get("noConstructionInit")); if (isLegacyConstruction()) { AbstractEntityAdjunct checkWeGetThis = configure(properties); assert this.equals(checkWeGetThis) : this+" configure method does not return itself; returns "+checkWeGetThis+" instead of "+this; boolean deferConstructionChecks = (properties.containsKey("deferConstructionChecks") && TypeCoercions.coerce(properties.get("deferConstructionChecks"), Boolean.class)); if (!deferConstructionChecks) { FlagUtils.checkRequiredFields(this); } } }
@Test public void testCoerceStringToEnum() { assertEquals(TypeCoercions.coerce("STARTING", Lifecycle.class), Lifecycle.STARTING); assertEquals(TypeCoercions.coerce("Starting", Lifecycle.class), Lifecycle.STARTING); assertEquals(TypeCoercions.coerce("starting", Lifecycle.class), Lifecycle.STARTING); assertEquals(TypeCoercions.coerce("LOWERCASE", PerverseEnum.class), PerverseEnum.lowercase); assertEquals(TypeCoercions.coerce("CAMELCASE", PerverseEnum.class), PerverseEnum.camelCase); assertEquals(TypeCoercions.coerce("upper", PerverseEnum.class), PerverseEnum.UPPER); assertEquals(TypeCoercions.coerce("upper_with_underscore", PerverseEnum.class), PerverseEnum.UPPER_WITH_UNDERSCORE); assertEquals(TypeCoercions.coerce("LOWER_WITH_UNDERSCORE", PerverseEnum.class), PerverseEnum.lower_with_underscore); } public static enum PerverseEnum {
protected static <T> T getOptionalVal(Map<String,?> map, ConfigKey<T> keyC) { if (keyC==null) return null; String key = keyC.getName(); if (map!=null && map.containsKey(key)) { return TypeCoercions.coerce(map.get(key), keyC.getTypeToken()); } else { return keyC.getDefaultValue(); } }
@Test public void testStringToListCoercion() { List<?> s = TypeCoercions.coerce("a,b,c", List.class); Assert.assertEquals(s, ImmutableList.of("a", "b", "c")); }
@Test @SuppressWarnings("serial") public void testCoerceRecursivelyStringToGenericsCollection() { assertEquals(TypeCoercions.coerce("1,2", new TypeToken<List<Integer>>() {}), ImmutableList.of(1, 2)); }
@Test public void testKeyEqualsOrColonValueWithoutBracesStringToMapCoercion() { Map<?,?> s = TypeCoercions.coerce("a=1, b: 2", Map.class); Assert.assertEquals(s, ImmutableMap.of("a", "1", "b", 2)); }
@Test public void testListToSetCoercion() { Set<?> s = TypeCoercions.coerce(ImmutableList.of(1), Set.class); Assert.assertEquals(s, ImmutableSet.of(1)); }
@Test public void testListEntryToCollectionCoercion() { @SuppressWarnings("serial") Collection<?> s = TypeCoercions.coerce(ImmutableList.of("java.lang.Integer", "java.lang.Double"), new TypeToken<Collection<Class<?>>>() { }); Assert.assertEquals(s, ImmutableList.of(Integer.class, Double.class)); }
@Test public void testSetToListCoercion() { List<?> s = TypeCoercions.coerce(ImmutableSet.of(1), List.class); Assert.assertEquals(s, ImmutableList.of(1)); }
@Test public void testListEntryCoercion() { @SuppressWarnings("serial") List<?> s = TypeCoercions.coerce(ImmutableList.of("java.lang.Integer", "java.lang.Double"), new TypeToken<List<Class<?>>>() { }); Assert.assertEquals(s, ImmutableList.of(Integer.class, Double.class)); }
/** Returns the result of the last task queued in this context, coerced to the given type */ protected <V> V last(Class<V> type) { Task<?> last = waitForLast(); if (last==null) throw new IllegalStateException("No last task available (in "+DynamicTasks.getTaskQueuingContext()+")"); if (!Tasks.isQueuedOrSubmitted(last)) throw new IllegalStateException("Last task "+last+" has not been queued or submitted; will not block on its result"); return TypeCoercions.coerce(last.getUnchecked(), type); } }
@Test public void testIterableToArrayCoercion() { String[] s = TypeCoercions.coerce(ImmutableList.of("a", "b"), String[].class); Assert.assertTrue(Arrays.equals(s, new String[] {"a", "b"}), "result="+Arrays.toString(s)); Integer[] i = TypeCoercions.coerce(ImmutableList.of(1, 2), Integer[].class); Assert.assertTrue(Arrays.equals(i, new Integer[] {1, 2}), "result="+Arrays.toString(i)); int[] i2 = TypeCoercions.coerce(ImmutableList.of(1, 2), int[].class); Assert.assertTrue(Arrays.equals(i2, new int[] {1, 2}), "result="+Arrays.toString(i2)); int[] i3 = TypeCoercions.coerce(MutableSet.of("1", 2), int[].class); Assert.assertTrue(Arrays.equals(i3, new int[] {1, 2}), "result="+Arrays.toString(i3)); }
@Test public void testMapOfCreatedObjs() { assertEquals( coerce(ImmutableMap.of("vals", ImmutableMap.of()), MapOfMyClazz.class), MapOfMyClazz.create(ImmutableMap.of())); assertEquals( coerce(ImmutableMap.of("vals", ImmutableMap.of("key1", ImmutableMap.of("arg1", "val1", "arg2", "val2"))), MapOfMyClazz.class), MapOfMyClazz.create(ImmutableMap.of("key1", MyClazz.create("val1", "val2")))); }
@Test public void testFailsIfNoNoargBuildMethod() { try { coerce(ImmutableMap.of("arg1", "val1"), MyClazzWithNoNoargBuildMethod.class); Asserts.shouldHaveFailedPreviously(); } catch (ClassCoercionException e) { Asserts.expectedFailureContains(e, "Builder for", "MyClazzWithNoNoargBuildMethod", "has no build() method"); } }
public static Maybe<ReleaseableLatch> maxConcurrencyImmediately(Object maxThreads) { Maybe<?> resolvedMaxThreads = resolveImmediately(maxThreads); if (resolvedMaxThreads.isAbsent()) return Maybe.absent(); Integer resolvedMaxThreadsInt = TypeCoercions.coerce(resolvedMaxThreads, Integer.class); ReleaseableLatch result = ReleaseableLatch.Factory.newMaxConcurrencyLatch(resolvedMaxThreadsInt); return Maybe.<ReleaseableLatch>of(result); }
@Test public void testCoerce() { HttpsSslConfig config = TypeCoercions.coerce(MutableMap.of("keystoreUrl", "http://foo", "keystorePassword", "b4r", "keyAlias", "baz"), HttpsSslConfig.class); Assert.assertEquals(config.getKeystoreUrl(), "http://foo"); Assert.assertEquals(config.getKeystorePassword(), "b4r"); Assert.assertEquals(config.getKeyAlias(), "baz"); }
@Override public <T> T getConfig(ConfigKey<T> key, T defaultValue) { // TODO does not support MapConfigKey etc where entries use subkey notation; for now, access using submap if (!containsKey(key.getName())) { if (defaultValue!=null) return defaultValue; return key.getDefaultValue(); } Object value = get(key.getName()); if (value==null) return null; // no evaluation / key extraction here return TypeCoercions.coerce(value, key.getTypeToken()); }