@Override public <T> T coerce(Object input, Class<T> type) { return TypeCoercions.coerce(input, type); } @Override public <T> Maybe<T> tryCoerce(Object input, Class<T> type) {
/** performs the language extensions required for this project */ @SuppressWarnings("rawtypes") public static void init() { if (initialized.compareAndSet(false, true)) { TypeCoercions.registerAdapter(Integer.class, PortRange.class, new Function<Integer,PortRange>() { @Override public PortRange apply(Integer x) { return fromInteger(x); } }); TypeCoercions.registerAdapter(String.class, PortRange.class, new Function<String,PortRange>() { @Override public PortRange apply(String x) { return fromString(x); } }); TypeCoercions.registerAdapter(Iterable.class, PortRange.class, new Function<Iterable,PortRange>() { @Override public PortRange apply(Iterable x) { return fromIterable(x); } }); } }
@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) {
latitude = TypeCoercions.castPrimitive(latitude, Double.class); longitude = TypeCoercions.castPrimitive(longitude, Double.class); } catch (Exception e) { Exceptions.propagateIfFatal(e);
@SuppressWarnings("unchecked") protected <T> T coerce(Object value, Class<T> type) { if (type==null || value==null) return (T) value; return TypeCoercions.coerce(value, type); }
@Override public <T> Maybe<T> tryCoerce(Object input, TypeToken<T> type) { return TypeCoercions.tryCoerce(input, type); } };
@SuppressWarnings("rawtypes") public static void registerBrooklynAdapters() { registerAdapter(String.class, AttributeSensor.class, new Function<String,AttributeSensor>() { @Override public AttributeSensor apply(final String input) { Entity entity = BrooklynTaskTags.getContextEntity(Tasks.current()); if (entity!=null) { Sensor<?> result = entity.getEntityType().getSensor(input); if (result instanceof AttributeSensor) return (AttributeSensor) result; } return Sensors.newSensor(Object.class, input); } }); registerAdapter(String.class, Sensor.class, new Function<String,Sensor>() { @Override public AttributeSensor apply(final String input) { Entity entity = BrooklynTaskTags.getContextEntity(Tasks.current()); if (entity!=null) { Sensor<?> result = entity.getEntityType().getSensor(input); if (result != null) return (AttributeSensor) result; } return Sensors.newSensor(Object.class, input); } }); }
protected static <N extends Number> N cast(Number n, TypeToken<? extends N> numberType) { return (N) TypeCoercions.coerce(n, numberType); } }
@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(); } };
/** Setup custom type coercions. */ @SuppressWarnings("rawtypes") public static void init() { if (initialized.getAndSet(true)) return; TypeCoercions.registerAdapter(String.class, DockerAwarePlacementStrategy.class, new Function<String, DockerAwarePlacementStrategy>() { @Override public DockerAwarePlacementStrategy apply(final String input) { ClassLoader classLoader = DockerAwarePlacementStrategy.class.getClassLoader(); Optional<DockerAwarePlacementStrategy> strategy = Reflections.<DockerAwarePlacementStrategy>invokeConstructorWithArgs(classLoader, input); if (strategy.isPresent()) { return strategy.get(); } else { throw new IllegalStateException("Failed to create DockerAwarePlacementStrategy "+input); } } }); }
public PortAttributeSensorAndConfigKey(PortAttributeSensorAndConfigKey orig, Object defaultValue) { super(orig, TypeCoercions.coerce(defaultValue, PortRange.class)); } public PortAttributeSensorAndConfigKey(BasicConfigKey.Builder<PortRange,?> builder) {
@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; } };
@SuppressWarnings("rawtypes") public static void registerGroovyAdapters() { registerAdapter(Closure.class, Predicate.class, new Function<Closure,Predicate>() { @Override public Predicate<?> apply(final Closure closure) { registerAdapter(Closure.class, Function.class, new Function<Closure,Function>() { @Override public Function apply(final Closure closure) { registerAdapter(Object.class, TimeDuration.class, new Function<Object,TimeDuration>() { @Override public TimeDuration apply(final Object input) { registerAdapter(TimeDuration.class, Long.class, new Function<TimeDuration,Long>() { @Override public Long apply(final TimeDuration input) {
@Override public boolean apply(final Object input) { return (input != null) && Objects.equal(TypeCoercions.coerce(value, input.getClass()), input); } };
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; }
private static class LocationMetadataImpl implements UsageListener.LocationMetadata { private final Location loc; private String locationId; private Map<String, String> metadata; LocationMetadataImpl(Location loc) { this.loc = checkNotNull(loc, "loc"); locationId = loc.getId(); metadata = ((LocationInternal)loc).toMetadataRecord(); } @Override public Location getLocation() { return loc; } @Override public String getLocationId() { return locationId; } @Override public Map<String, String> getMetadata() { return metadata; } }
private boolean isHaEnabled(Map<String, Map> configuration) { String HDFS_SITE = "hdfs-site"; String HA_ENABLE = "dfs.ha.automatic-failover.enabled"; if (configuration.get(HDFS_SITE).get(HA_ENABLE) != null) { return Boolean.TRUE.equals(TypeCoercions.coerce(configuration.get(HDFS_SITE).get(HA_ENABLE), Boolean.class)); } return false; } }
@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; }
public static class Builder { private String id; private String name; private AttributeSensor<? extends Number> metric; private Entity entityWithMetric; private Number metricUpperBound; private Number metricLowerBound; private int minPoolSize = 1; private int maxPoolSize = Integer.MAX_VALUE; private Integer resizeDownIterationIncrement; private Integer resizeDownIterationMax; private Integer resizeUpIterationIncrement; private Integer resizeUpIterationMax; private Duration minPeriodBetweenExecs; private Duration resizeUpStabilizationDelay; private Duration resizeDownStabilizationDelay; private ResizeOperator resizeOperator; private Function<Entity,Integer> currentSizeOperator; private BasicNotificationSensor<?> poolHotSensor; private BasicNotificationSensor<?> poolColdSensor; private BasicNotificationSensor<?> poolOkSensor; private BasicNotificationSensor<? super MaxPoolSizeReachedEvent> maxSizeReachedSensor; private Duration maxReachedNotificationDelay; public Builder id(String val) { this.id = val; return this; } public Builder name(String val) { this.name = val; return this; }
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; }