/** * Create a new {@link PathPropertyBoxAdapter}. * @param propertyBox The {@link PropertyBox} set to use (not null) * @return A new {@link PathPropertyBoxAdapter} */ static PathPropertyBoxAdapter create(PropertyBox propertyBox) { return builder(propertyBox).build(); }
public void pathPropertyBoxAdapter() { // tag::ppba[] final StringProperty STR = StringProperty.create("str"); final NumericProperty<Integer> ITG = NumericProperty.integerType("itg"); final PropertySet<?> SET = PropertySet.of(STR, ITG); final Path<String> PATH = Path.of("str", String.class); PropertyBox box = PropertyBox.builder(SET).set(STR, "test1").set(ITG, 1).build(); PathPropertyBoxAdapter adapter = PathPropertyBoxAdapter.create(box); // <1> boolean contains = adapter.containsValue(PATH); // <2> Optional<String> value = adapter.getValue(PATH); // <3> adapter.setValue(PATH, "value"); // <4> // end::ppba[] }
/** * Get given {@link PropertyBox} as a map of {@link Path} and {@link TypedExpression} values. * @param value The property box value (not null) * @param includeNullValues Whether to set <code>null</code> property box values to the <code>null</code> value * @return Values map */ @SuppressWarnings({ "unchecked", "rawtypes" }) protected Map<Path<?>, TypedExpression<?>> asPathValues(PropertyBox value, boolean includeNullValues) { ObjectUtils.argumentNotNull(value, "PropertyBox must be not null"); final Map<Path<?>, TypedExpression<?>> values = new HashMap<>(value.size()); final PathPropertyBoxAdapter propertyBoxAdapter = PathPropertyBoxAdapter.create(value); propertyBoxAdapter.paths().forEach(path -> { propertyBoxAdapter.getValueOrElse(path, pathWithNoValue -> { if (includeNullValues) { values.put(path, NullExpression.create(path)); } }).ifPresent(val -> { values.put(path, (val instanceof TypedExpression) ? (TypedExpression<?>) val : ConstantConverterExpression.create((Path) path, val)); }); }); return values; }
return adapter.getProperty(nestedPath).filter(p -> PropertyBox.class.isAssignableFrom(p.getType())) .map(p -> { final PropertySet<?> propertySet = p.getConfiguration() + PropertySet.PROPERTY_CONFIGURATION_ATTRIBUTE.getKey() + "]")); PropertyBox pb = decodePropertyBox(context.documentContext(propertySet), parent, nested); adapter.setValue((Path) nestedPath, pb); return p; }); if (firstElement != null && Map.class.isAssignableFrom(firstElement.getClass())) { CollectionProperty<PropertyBox, Collection<PropertyBox>> collectionProperty = adapter.getProperty(path) .filter(p -> CollectionProperty.class.isAssignableFrom(p.getClass())) .map(p -> (CollectionProperty<?, ?>) p) boolean isList = adapter.getProperty(path).filter(p -> List.class.isAssignableFrom(p.getType())) .orElse(null) != null; Collection<PropertyBox> propertyValues = isList ? new ArrayList<>(size) : new HashSet<>(size); return adapter.getProperty(path).map(p -> { Object resolvedValue = context.resolveOrFail( FieldValue.create(value, p, p.getConfiguration().getParameter(EnumCodecStrategy.CONFIG_PROPERTY).orElse(null)), Value.class).getValue(); adapter.setValue((Path) path, resolvedValue); return p; });
/** * Decode a document into a {@link PropertyBox} value using given property set. * @param context Resolution context * @param parent Optional parent field name expression * @param document Document to decode * @return Decoded PropertyBox instance * @throws InvalidExpressionException If an error occurred */ private static PropertyBox decodePropertyBox(final MongoDocumentContext<?> context, final String parent, Map<String, Object> document) throws InvalidExpressionException { // PropertyBox builder final PropertyBox propertyBox = PropertyBox.builder(context.getPropertySet()).invalidAllowed(true).build(); // Adapter final PathPropertyBoxAdapter adapter = PathPropertyBoxAdapter.builder(propertyBox) .pathMatcher(DocumentPathMatcher.INSTANCE).build(); // decode decodeDocument(context, parent, document, propertyBox, adapter); return propertyBox; }