/** * Construct a new PropertyBox using given <code>properties</code> as property set. * @param <P> Actual property type * @param properties Property set */ public <P extends Property> DefaultPropertyBox(Iterable<P> properties) { this((properties instanceof PropertySet) ? (PropertySet<P>) properties : PropertySet.of(properties)); }
public void propertySet2() { // tag::propertyset2[] final PathProperty<String> NAME = PathProperty.create("name", String.class); final PathProperty<String> SURNAME = PathProperty.create("surname", String.class); final PropertySet<Property<?>> SET = PropertySet.of(NAME, SURNAME); // <1> boolean contains = SET.contains(NAME); // <2> SET.forEach(p -> p.toString()); // <3> String captions = SET.stream().map(p -> p.getMessage()).collect(Collectors.joining()); // <4> List<Property<?>> list = SET.asList(); // <5> // end::propertyset2[] }
public void propertySet3() { // tag::propertyset3[] final NumericProperty<Long> ID = NumericProperty.longType("id"); final StringProperty NAME = StringProperty.create("name"); PropertySet<Property<?>> SET = PropertySet.builder().add(ID).add(NAME).identifier(ID).build(); // <1> SET = PropertySet.builderOf(ID, NAME).identifier(ID).build(); // <2> Set<Property<?>> ids = SET.getIdentifiers(); // <3> Optional<Property<?>> id = SET.getFirstIdentifier(); // <4> SET.identifiers().forEach(p -> p.toString()); // <5> // end::propertyset3[] }
@SuppressWarnings("rawtypes") @Override public <P extends Property> PropertyBox fromJson(JsonReader reader, Iterable<P> propertySet) { ObjectUtils.argumentNotNull(reader, "JsonReader must be not null"); ObjectUtils.argumentNotNull(propertySet, "PropertySet must be not null"); final PropertySet<?> ps = (PropertySet.class.isAssignableFrom(propertySet.getClass())) ? (PropertySet<?>) propertySet : PropertySet.of(propertySet); return ps.execute(() -> fromJson(reader, PropertyBox.class)); }
public void propertySet() { // tag::propertyset[] final StringProperty NAME = StringProperty.create("name"); final StringProperty SURNAME = StringProperty.create("surname"); final NumericProperty<Integer> SEQUENCE = NumericProperty.integerType("surname"); PropertySet<Property<?>> set = PropertySet.of(NAME, SURNAME); // <1> set = PropertySet.builder().add(NAME).add(SURNAME).build(); // <2> PropertySet<Property<?>> set2 = PropertySet.builder().add(set).add(SEQUENCE).build(); // <3> // end::propertyset[] }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static Optional<Path<?>> getPathByName(PropertySet set, String propertyName) { if (set != null && propertyName != null) { return set.stream().filter(p -> Path.class.isAssignableFrom(p.getClass())) .filter(p -> propertyName.equals(((Path) p).getName())).findFirst(); } return Optional.empty(); }
/** * Lookup for a property which can act as document identifier. * @param <S> PropertySet property type * @param <P> Path and Property type * @param propertySet Property set (not null) * @return The document identifier property, if available * @throws InvalidDocumentIdentifierException If more than one suitable property is found or if an identifier * property has invalid type */ @SuppressWarnings({ "rawtypes", "unchecked" }) private static <S extends Property, P extends Path & Property> P findDocumentIdProperty(PropertySet<S> propertySet) throws InvalidDocumentIdentifierException { // check identifiers for the default id path name P identifier = findDefaultDocumentIdPathProperty(propertySet, propertySet.identifiers()); if (identifier != null) { return identifier; } // check a suitable (unique) identifier property with a different path name if (propertySet.getIdentifiers().size() == 1) { S id = propertySet.getIdentifiers().iterator().next(); if (id != null && Path.class.isAssignableFrom(id.getClass())) { if (isValidDocumentIdPropertyType(id)) { return (P) id; } } } // check all properties for the default id path name return findDefaultDocumentIdPathProperty(propertySet, propertySet.stream()); }
static <P extends Property> PropertySet<P> of(PropertySet<? extends P> propertySet, P... properties) { ObjectUtils.argumentNotNull(propertySet, "Source property set must be not null"); final Builder builder = builder(); propertySet.forEach(p -> builder.add(p)); propertySet.identifiers().forEach(i -> builder.identifier(i)); if (propertySet.getConfiguration().hasParameters()) { propertySet.getConfiguration().forEachParameter((n, v) -> builder.configuration(n, v));
public void jaxrs() { Client client = ClientBuilder.newClient(); // <2> PropertyBox box1 = PropertyBox.builder(PROPERTYSET).set(CODE, 1).set(NAME, "Test").build(); client.target("https://host/test/serialize").request().put(Entity.entity(box1, MediaType.APPLICATION_JSON)); // <3> PropertyBox box2 = PROPERTYSET .execute(() -> client.target("https://host/test/deserialize").request().get(PropertyBox.class)); // <4> } // end::jaxrs[]
/** * Convert given {@link Path} set to a {@link PropertySet}, including only {@link Property} type paths. * @param paths Path collection * @return A {@link PropertySet} which includes the {@link Property} type paths, in the given order */ @SuppressWarnings({ "rawtypes", "unchecked" }) private static PropertySet<?> asPropertySet(Iterable<Path<?>> paths) { final PropertySet.Builder builder = PropertySet.builder(); for (Path<?> path : paths) { if (path != null && Property.class.isAssignableFrom(path.getClass())) { builder.add((Property) path); } } return builder.build(); }
/** * Join given {@link PropertySet}s and return a new PropertySet containing all the properties of given sets. * @param <P> Actual property type * @param propertySets PropertySet to join (not null and not empty) * @return New PropertySet containing all the properties of given sets * @deprecated Using this method causes the loss of any property set configuration and/or identifier property * declaration. Use the default PropertySet builder to compose a new PropertySet from different property * sources. */ @SuppressWarnings("unchecked") @Deprecated @SafeVarargs static <P extends Property> PropertySet<P> join(PropertySet<? extends P>... propertySets) { if (propertySets == null || propertySets.length == 0) { throw new IllegalArgumentException("No PropertySet to join"); } Builder builder = builder(); for (PropertySet<? extends P> ps : propertySets) { ps.forEach(p -> builder.add(p)); } return builder.build(); }
@SuppressWarnings("unchecked") @Override public B dataSource(ItemDataProvider<PropertyBox> dataProvider, final Property... identifierProperties) { ObjectUtils.argumentNotNull(identifierProperties, "Identifier properties must be not null"); if (identifierProperties.length == 0) { throw new IllegalArgumentException("Identifier properties must be not empty"); } final PropertySet<?> propertySet = PropertySet.builderOf(identifierProperties) .identifiers(Arrays.asList(identifierProperties)).build(); return dataSource(dataProvider, item -> { PropertyBox.Builder builder = PropertyBox.builder(propertySet); for (Property p : identifierProperties) { item.getValueIfPresent(p).ifPresent(v -> builder.set(p, v)); } return builder.build(); }); }
public void dataMappable2() { final StringProperty P1 = StringProperty.create("p1"); final StringProperty P2 = StringProperty.create("p2"); // tag::datamappable2[] PropertySet<?> PROPERTIES = PropertySet.builderOf(P1, P2) // .withConfiguration(DataMappable.PATH, "mapping-name") // <1> .build(); Optional<String> mapping = PROPERTIES.getConfiguration().getParameter(DataMappable.PATH); // <2> // end::datamappable2[] }
@SuppressWarnings("unchecked") @Override public Optional<BsonProjection> resolve(PropertySetProjection expression, MongoResolutionContext<?> context) throws InvalidExpressionException { // validate expression.validate(); // document context final MongoDocumentContext<?> documentContext = context.documentContext(expression.getPropertySet()); final BsonProjection.Builder builder = BsonProjection.builder(PropertyBox.class); documentContext.getPropertySet().forEach(property -> { documentContext.resolve(property, BsonProjection.class).ifPresent(projection -> { final BsonProjection<?> p = projection; // add field projections p.getFields().forEach((name, bson) -> { builder.field(name, bson); }); }); }); // converter builder.converter(DocumentConverter.propertyBox(expression.getPropertySet())); return Optional.of(builder.build()); }
@SuppressWarnings("rawtypes") @Override public <P extends Property> List<PropertyBox> fromJsonArray(JsonReader reader, Iterable<P> propertySet) { ObjectUtils.argumentNotNull(reader, "JsonReader must be not null"); ObjectUtils.argumentNotNull(propertySet, "PropertySet must be not null"); final PropertySet<?> ps = (PropertySet.class.isAssignableFrom(propertySet.getClass())) ? (PropertySet<?>) propertySet : PropertySet.of(propertySet); return ps.execute(() -> fromJsonArray(reader, PropertyBox.class)); }
@Override public Stream<Property> stream() { return getAndCheckPropertySet().stream(); }
public void jaxrs() { Client client = ClientBuilder.newClient(); // <1> PropertyBox box1 = PropertyBox.builder(PROPERTYSET).set(CODE, 1).set(NAME, "Test").build(); client.target("https://host/test/serialize").request().put(Entity.entity(box1, MediaType.APPLICATION_JSON)); // <2> PropertyBox box2 = PROPERTYSET .execute(() -> client.target("https://host/test/deserialize").request().get(PropertyBox.class)); // <3> } // end::jaxrs[]
/** * Obtain a builder to create and populate a {@link PropertySet}, and add given <code>properties</code> to the * property set to build. * @param <P> Property type * @param properties Properties to initially add to the property set (not null) * @return A new {@link PropertySet} builder * @since 5.1.0 */ @SafeVarargs static <P extends Property> Builder<Property<?>> builderOf(P... properties) { ObjectUtils.argumentNotNull(properties, "Properties must be not null"); Builder<Property<?>> builder = builder(); for (P property : properties) { builder.add(property); } return builder; }
public void identifiers() { // tag::identifiers[] final NumericProperty<Long> ID = NumericProperty.longType("id"); final StringProperty DESCRIPTION = StringProperty.create("description"); final PropertySet<?> PROPERTIES = PropertySet.builderOf(ID, DESCRIPTION).identifier(ID).build(); // <1> PropertyListing listing = Components.listing.properties(PROPERTIES).build(); // end::identifiers[] }
/** * Clone this PropertyBox using given <code>propertySet</code>, i.e. create a new PropertyBox and copy all given set * property values from this box to the newly created box. * @param <P> Actual property type * @param propertySet Property set of the cloned PropertyBox (not null) * @return A new, cloned, PropertyBox instance with given property set */ @SuppressWarnings("unchecked") default <P extends Property> PropertyBox cloneBox(PropertySet<P> propertySet) { ObjectUtils.argumentNotNull(propertySet, "Property set must be not null"); Builder builder = builder(propertySet).invalidAllowed(true); propertySet.forEach(p -> { if (!p.isReadOnly()) { this.getValueIfPresent(p).ifPresent(v -> builder.set(p, v)); } }); return builder.invalidAllowed(this.isInvalidAllowed()).build(); }