private static Path<?> getPathByName(String propertyName, Iterable<?> properties) { if (propertyName != null && properties != null) { for (Object property : properties) { if (String.class.isAssignableFrom(property.getClass()) && propertyName.equals(property)) { return Path.of(propertyName, Object.class); } if (property instanceof Path && propertyName.equals(((Path<?>) property).getName())) { return (Path<?>) property; } } } return null; }
@Override public Optional<Path> resolve(FieldName expression, MongoResolutionContext context) throws InvalidExpressionException { // validate expression.validate(); // field name final String fieldName = expression.getFieldName(); // check document context or use field name return Optional.of(getPathUsingDocumentContext(context, fieldName).orElse(Path.of(fieldName, Object.class))); }
public void path() { // tag::path[] Path<String> stringPath = Path.of("pathName", String.class); // <1> String name = stringPath.getName(); // <2> boolean root = stringPath.isRootPath(); // <3> Path<String> hierarchicalPath = Path.of("subName", String.class).parent(stringPath); // <4> String fullName = hierarchicalPath.fullName(); // <5> // end::path[] }
private static <T, P extends Path<T> & Property<T>> Map<String, Object> encodePropertyBoxTypeCollectionProperty( MongoDocumentContext<?> context, P property, Collection<PropertyBox> values, String name, String parentPath, boolean forUpdate) throws InvalidExpressionException { // resolve field name String fieldName = context.resolveOrFail(Path.of(name, Object.class), FieldName.class).getFieldName(); // encode collection elements if (values == null || values.isEmpty()) { return Collections.emptyMap(); } List<Document> encoded = new ArrayList<>(values.size()); for (PropertyBox pb : values) { Document doc = encodePropertyBox( context.documentContext(property.getConfiguration() .getParameter(PropertySet.PROPERTY_CONFIGURATION_ATTRIBUTE).orElse(pb), false), pb, parentPath, forUpdate); if (doc != null) { encoded.add(doc); } } return Collections.singletonMap(forUpdate ? composeFieldPath(parentPath, fieldName) : fieldName, encoded); }
sort = QuerySort.of(Path.of(order.getSorted(), Object.class), direction);
private static <T, P extends Path<T> & Property<T>> Map<String, Object> encodePropertyBoxTypeProperty( MongoDocumentContext<?> context, P property, PropertyBox value, String name, String parentPath, boolean forUpdate) throws InvalidExpressionException { // resolve field name String fieldName = context.resolveOrFail(Path.of(name, Object.class), FieldName.class).getFieldName(); // encode nested document Document encoded = encodePropertyBox( context.documentContext(property.getConfiguration() .getParameter(PropertySet.PROPERTY_CONFIGURATION_ATTRIBUTE).orElse(value), false), value, forUpdate ? composeFieldPath(parentPath, fieldName) : parentPath, forUpdate); if (forUpdate) { return encoded; } else { return Collections.singletonMap(fieldName, (encoded != null) ? encoded : NO_VALUE); } }
final Path<?> nestedPath = Path.of(fieldName, Object.class); return adapter.getProperty(nestedPath).filter(p -> PropertyBox.class.isAssignableFrom(p.getType())) .map(p -> {
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[] }
public void pathPropertySetAdapter1() { // tag::ppsa1[] 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); PathPropertySetAdapter adapter = PathPropertySetAdapter.create(SET); // <1> boolean contains = adapter.contains(PATH); // <2> Optional<Property<String>> property = adapter.getProperty(PATH); // <3> Optional<Path<String>> path = adapter.getPath(STR); // <4> Stream<Path<?>> paths = adapter.paths(); // <5> // end::ppsa1[] // tag::ppsa2[] PathPropertySetAdapter pathPropertySetAdapter = PathPropertySetAdapter.builder(SET) // <1> .pathConverter(new MyPathConverter()) // <2> .pathMatcher(new MyPathMatcher()) // <3> .build(); // end::ppsa2[] }