@Override public Set<String> tokens(Bean bean) { if (bean.propertyNames().size() == 1) { String singlePropertyName = Iterables.getOnlyElement(bean.propertyNames()); Object propertyValue = bean.property(singlePropertyName).get(); Set<String> valueTokens = ValuePathEvaluator.tokens(propertyValue); return ImmutableSet.<String>builder() .add(singlePropertyName) .addAll(valueTokens) .build(); } else { return bean.propertyNames(); } }
public void test_immutableValuesViaBeanGet() { LocalDateDoubleTimeSeries test = LocalDateDoubleTimeSeries.builder().putAll(DATES_2010_12, VALUES_10_12).build(); double[] array = (double[]) ((Bean) test).property("values").get(); array[0] = -1; LocalDateDoublePoint[] points = test.stream().toArray(LocalDateDoublePoint[]::new); assertEquals(points[0], LocalDateDoublePoint.of(DATE_2010_01_01, 10d)); assertEquals(points[1], LocalDateDoublePoint.of(DATE_2011_01_01, 11d)); assertEquals(points[2], LocalDateDoublePoint.of(DATE_2012_01_01, 12d)); }
private Set<String> fieldValues(Object object) { if (!(object instanceof Bean)) { return ImmutableSet.of(); } Bean bean = (Bean) object; return bean.propertyNames().stream() .map(bean::property) .filter(p -> SUPPORTED_FIELD_TYPES.contains(p.metaProperty().propertyType())) .map(Property::get) .filter(v -> v != null) .map(Object::toString) .map(v -> v.toLowerCase(Locale.ENGLISH)) .collect(toImmutableSet()); }
public void test_immutableValuesViaBeanGet() { LocalDateDoubleTimeSeries test = LocalDateDoubleTimeSeries.builder().putAll(DATES_2015_1_WEEK, VALUES_1_WEEK).build(); double[] array = (double[]) ((Bean) test).property("points").get(); array[0] = -1; LocalDateDoublePoint[] points = test.stream().toArray(LocalDateDoublePoint[]::new); assertEquals(points[0], LocalDateDoublePoint.of(DATE_2015_01_05, 10d)); assertEquals(points[1], LocalDateDoublePoint.of(DATE_2015_01_06, 11d)); assertEquals(points[2], LocalDateDoublePoint.of(DATE_2015_01_07, 12d)); }
@Override public EvaluationResult evaluate( Bean bean, CalculationFunctions functions, String firstToken, List<String> remainingTokens) { Optional<String> propertyName = bean.propertyNames().stream() .filter(p -> p.equalsIgnoreCase(firstToken)) .findFirst(); if (propertyName.isPresent()) { Object propertyValue = bean.property(propertyName.get()).get(); return propertyValue != null ? EvaluationResult.success(propertyValue, remainingTokens) : EvaluationResult.failure("No value available for property '{}'", firstToken); } // The bean has a single property which doesn't match the token. // Return the property value without consuming any tokens. // This allows skipping over properties when the bean only has a single property. if (bean.propertyNames().size() == 1) { String singlePropertyName = Iterables.getOnlyElement(bean.propertyNames()); Object propertyValue = bean.property(singlePropertyName).get(); List<String> tokens = ImmutableList.<String>builder().add(firstToken).addAll(remainingTokens).build(); return propertyValue != null ? EvaluationResult.success(propertyValue, tokens) : EvaluationResult.failure("No value available for property '{}'", firstToken); } return invalidTokenFailure(bean, firstToken); }
public void test_immutableDatesViaBeanGet() { LocalDateDoubleTimeSeries test = LocalDateDoubleTimeSeries.builder().putAll(DATES_2010_12, VALUES_10_12).build(); LocalDate[] array = (LocalDate[]) ((Bean) test).property("dates").get(); array[0] = DATE_2012_01_01; LocalDateDoublePoint[] points = test.stream().toArray(LocalDateDoublePoint[]::new); assertEquals(points[0], LocalDateDoublePoint.of(DATE_2010_01_01, 10d)); assertEquals(points[1], LocalDateDoublePoint.of(DATE_2011_01_01, 11d)); assertEquals(points[2], LocalDateDoublePoint.of(DATE_2012_01_01, 12d)); }