/** * Performs the specified {@link Query}. * * @return the {@link Entry} on a successful query. * The specified {@code other} on a failure due to missing entry. * * @see #get(Revision, Query) */ default <T> CompletableFuture<Entry<T>> getOrNull(Revision revision, Query<T> query) { requireNonNull(query, "query"); requireNonNull(revision, "revision"); return getOrNull(revision, query.path()).thenApply(entry -> { if (entry == null) { return null; } final EntryType entryType = entry.type(); if (!query.type().supportedEntryTypes().contains(entryType)) { throw new QueryExecutionException("unsupported entry type: " + entryType); } @SuppressWarnings("unchecked") final T entryContent = (T) entry.content(); try { return Entry.of(entry.revision(), query.path(), entryType, query.apply(entryContent)); } catch (CentralDogmaException e) { throw e; } catch (Exception e) { throw new QueryExecutionException(e); } }); }
/** * Applies the specified {@link Query} to the {@link Entry#content()} of the specified {@link Entry} and * returns the query result. * * @throws IllegalStateException if the specified {@link Entry} is a directory * @throws QuerySyntaxException if the syntax of specified {@link Query} is invalid * @throws QueryExecutionException if an {@link Exception} is raised while applying the specified * {@link Query} to the {@link Entry#content()} */ static <T> Entry<T> applyQuery(Entry<T> entry, Query<T> query) { requireNonNull(query, "query"); entry.content(); // Ensure that content is not null. final EntryType entryType = entry.type(); final QueryType queryType = query.type(); if (!queryType.supportedEntryTypes().contains(entryType)) { throw new QueryExecutionException("Unsupported entry type: " + entryType + " (query: " + query + ')'); } if (queryType == IDENTITY) { return entry; } else if (queryType == JSON_PATH) { return Entry.of(entry.revision(), query.path(), entryType, query.apply(entry.content())); } else { throw new QueryExecutionException("Unsupported entry type: " + entryType + " (query: " + query + ')'); } }
/** * Applies the specified {@link Query} to the {@link Entry#content()} of the specified {@link Entry} and * returns the query result. * * @throws IllegalStateException if the specified {@link Entry} is a directory * @throws QuerySyntaxException if the syntax of specified {@link Query} is invalid * @throws QueryExecutionException if an {@link Exception} is raised while applying the specified * {@link Query} to the {@link Entry#content()} */ static <T> Entry<T> applyQuery(Entry<T> entry, Query<T> query) { requireNonNull(query, "query"); entry.content(); // Ensure that content is not null. final EntryType entryType = entry.type(); final QueryType queryType = query.type(); if (!queryType.supportedEntryTypes().contains(entryType)) { throw new QueryExecutionException("Unsupported entry type: " + entryType + " (query: " + query + ')'); } if (queryType == IDENTITY) { return entry; } else if (queryType == JSON_PATH) { return Entry.of(entry.revision(), query.path(), entryType, query.apply(entry.content())); } else { throw new QueryExecutionException("Unsupported entry type: " + entryType + " (query: " + query + ')'); } }