@Override public <T> Maybe<T> tryCoerce(Object input, Class<T> type) { return TypeCoercions.tryCoerce(input, type); } @Override public <T> Maybe<T> tryCoerce(Object input, TypeToken<T> type) {
@Override public <T> Maybe<T> tryCoerce(Object input, TypeToken<T> type) { return TypeCoercions.tryCoerce(input, type); } };
@Override public boolean apply(@Nullable Method input) { if (input == null) return false; if (!input.getName().equals(methodName)) return false; Type[] parameterTypes = input.getGenericParameterTypes(); return parameterTypes.length == 1 && TypeCoercions.tryCoerce(argument, TypeToken.of(parameterTypes[0])).isPresentAndNonNull(); } };
@Override public boolean apply(@Nullable Method input) { if (input == null) return false; int numOptionParams = arguments.size(); Type[] parameterTypes = input.getGenericParameterTypes(); if (parameterTypes.length != numOptionParams) return false; for (int paramCount = 0; paramCount < numOptionParams; paramCount++) { if (!TypeCoercions.tryCoerce(((List<?>) arguments).get(paramCount), TypeToken.of(parameterTypes[paramCount])).isPresent()) return false; } return true; } };
private AttributeSensor<Object> getSensor() { final Object configVal = Preconditions.checkNotNull(getConfig(SENSOR), SENSOR); final AttributeSensor<Object> sensor; if (configVal == null) { throw new NullPointerException("Value for " + SENSOR.getName() + " is null"); } else if (configVal instanceof String) { sensor = Sensors.newSensor(Object.class, (String) configVal); } else if (configVal instanceof AttributeSensor) { sensor = (AttributeSensor<Object>) configVal; } else { sensor = TypeCoercions.tryCoerce(configVal, new TypeToken<AttributeSensor<Object>>() {}).get(); } return sensor; }
protected static List<Map<String, Object>> getAsListOfMaps(Entity entity, ConfigKey<Object> key) { Object config = entity.getConfig(key); Maybe<Map<String, Object>> maybeMap = TypeCoercions.tryCoerce(config, new TypeToken<Map<String, Object>>() {}); if (maybeMap.isPresent()) { return Collections.singletonList(maybeMap.get()); } Maybe<List<Map<String, Object>>> maybeList = TypeCoercions.tryCoerce(config, new TypeToken<List<Map<String, Object>>>() {}); if (maybeList.isPresent()) { return maybeList.get(); } throw new FatalConfigurationRuntimeException(key.getDescription() + " is not a map or list of maps"); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public Object applyValueToMap(Object value, Map target) { if (value == null) return null; if (value instanceof StructuredModification) return ((StructuredModification)value).applyToKeyInMap(this, target); if (value instanceof Map.Entry) return applyEntryValueToMap((Map.Entry)value, target); if (!(value instanceof Map)) { Maybe<Map> coercedValue = TypeCoercions.tryCoerce(value, Map.class); if (coercedValue.isPresent()) { log.trace("Coerced value for {} from type {} to map", this, value.getClass().getName()); value = coercedValue.get(); } else { throw new IllegalArgumentException("Cannot set non-map entries on "+this+", given type "+value.getClass().getName()+", value "+value); } } Map result = new MutableMap(); for (Object entry: ((Map)value).entrySet()) { Map.Entry entryT = (Map.Entry)entry; result.put(entryT.getKey(), applyEntryValueToMap(entryT, target)); } if (((Map)value).isEmpty() && !isSet(target)) target.put(this, MutableMap.of()); return result; }
|| (portsPattern.matcher(k.getName()).matches())) { Object value = entity.config().get(k); Maybe<PortRange> maybePortRange = TypeCoercions.tryCoerce(value, new TypeToken<PortRange>() { }); if (maybePortRange.isPresentAndNonNull()) {
return TypeCoercions.tryCoerce(v, TypeToken.of(type));
/** * For resolving a "simple" config key - i.e. where there's not custom logic inside a * {@link StructuredConfigKey} such as a {@link MapConfigKey}. For those, we'd need to do the * same as is in {@link #get(ConfigKey)}, but non-blocking! * See {@link #getNonBlockingResolvingStructuredKey(ConfigKey)}. */ protected <T> Maybe<T> getNonBlockingResolvingSimple(ConfigKey<T> key) { // TODO See AbstractConfigMapImpl.getConfigImpl, for how it looks up the "container" of the // key, so that it gets the right context entity etc. // getRaw returns Maybe(val) if the key was explicitly set (where val can be null) // or Absent if the config key was unset. Object unresolved = getRaw(key).or(key.getDefaultValue()); Maybe<Object> resolved = Tasks.resolving(unresolved) .as(Object.class) .immediately(true) .deep(true) .context(getContext()) .getMaybe(); if (resolved.isAbsent()) return Maybe.Absent.<T>castAbsent(resolved); // likely we don't need this coercion if we set as(key.getType()) above, // but that needs confirmation and quite a lot of testing return TypeCoercions.tryCoerce(resolved.get(), key.getTypeToken()); }