|
| 1 | +/* |
| 2 | + * Copyright 2011-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.neo4j.core; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Optional; |
| 23 | + |
| 24 | +import org.apiguardian.api.API; |
| 25 | +import org.neo4j.cypherdsl.core.Statement; |
| 26 | +import org.springframework.lang.Nullable; |
| 27 | + |
| 28 | +/** |
| 29 | + * {@link FluentFindOperation} allows creation and execution of Neo4j find operations in a fluent API style. |
| 30 | + * <br /> |
| 31 | + * The starting {@literal domainType} is used for mapping the query provided via {@code by} into the |
| 32 | + * Neo4j specific representation. By default, the originating {@literal domainType} is also used for mapping back the |
| 33 | + * result. However, it is possible to define an different {@literal returnType} via |
| 34 | + * {@code as} to mapping the result.<br /> |
| 35 | + * |
| 36 | + * @author Michael Simons |
| 37 | + * @since 6.1 |
| 38 | + */ |
| 39 | +@API(status = API.Status.STABLE, since = "6.1") |
| 40 | +public interface FluentFindOperation { |
| 41 | + |
| 42 | + /** |
| 43 | + * Start creating a find operation for the given {@literal domainType}. |
| 44 | + * |
| 45 | + * @param domainType must not be {@literal null}. |
| 46 | + * @return new instance of {@link ExecutableFind}. |
| 47 | + * @throws IllegalArgumentException if domainType is {@literal null}. |
| 48 | + */ |
| 49 | + <T> ExecutableFind<T> find(Class<T> domainType); |
| 50 | + |
| 51 | + /** |
| 52 | + * Trigger find execution by calling one of the terminating methods from a state where no query is yet defined. |
| 53 | + * |
| 54 | + * @param <T> returned type |
| 55 | + */ |
| 56 | + interface TerminatingFindWithoutQuery<T> { |
| 57 | + |
| 58 | + /** |
| 59 | + * Get all matching elements. |
| 60 | + * |
| 61 | + * @return never {@literal null}. |
| 62 | + */ |
| 63 | + List<T> all(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Triggers find execution by calling one of the terminating methods. |
| 68 | + * |
| 69 | + * @param <T> returned type |
| 70 | + */ |
| 71 | + interface TerminatingFind<T> extends TerminatingFindWithoutQuery<T> { |
| 72 | + |
| 73 | + /** |
| 74 | + * Get exactly zero or one result. |
| 75 | + * |
| 76 | + * @return {@link Optional#empty()} if no match found. |
| 77 | + * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found. |
| 78 | + */ |
| 79 | + default Optional<T> one() { |
| 80 | + return Optional.ofNullable(oneValue()); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get exactly zero or one result. |
| 85 | + * |
| 86 | + * @return {@literal null} if no match found. |
| 87 | + * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found. |
| 88 | + */ |
| 89 | + @Nullable |
| 90 | + T oneValue(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Terminating operations invoking the actual query execution. |
| 95 | + * |
| 96 | + * @param <T> returned type |
| 97 | + */ |
| 98 | + interface FindWithQuery<T> extends TerminatingFindWithoutQuery<T> { |
| 99 | + |
| 100 | + /** |
| 101 | + * Set the filter query to be used. |
| 102 | + * |
| 103 | + * @param query must not be {@literal null}. |
| 104 | + * @return new instance of {@link TerminatingFind}. |
| 105 | + * @throws IllegalArgumentException if query is {@literal null}. |
| 106 | + */ |
| 107 | + TerminatingFind<T> matching(String query, Map<String, Object> parameter); |
| 108 | + |
| 109 | + /** |
| 110 | + * Set the filter query to be used. |
| 111 | + * |
| 112 | + * @param query must not be {@literal null}. |
| 113 | + * @return new instance of {@link TerminatingFind}. |
| 114 | + * @throws IllegalArgumentException if query is {@literal null}. |
| 115 | + */ |
| 116 | + default TerminatingFind<T> matching(String query) { |
| 117 | + return matching(query, Collections.emptyMap()); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Set the filter {@link Statement statement} to be used. |
| 122 | + * |
| 123 | + * @param statement must not be {@literal null}. |
| 124 | + * @param parameter Will be merged with parameters in the statement. Parameters in {@code parameter} have precedence. |
| 125 | + * @return new instance of {@link TerminatingFind}. |
| 126 | + * @throws IllegalArgumentException if statement is {@literal null}. |
| 127 | + */ |
| 128 | + default TerminatingFind<T> matching(Statement statement, Map<String, Object> parameter) { |
| 129 | + Map<String, Object> mergedParameters = new HashMap<>(); |
| 130 | + mergedParameters.putAll(statement.getParameters()); |
| 131 | + mergedParameters.putAll(parameter); |
| 132 | + return matching(statement.getCypher(), mergedParameters); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Set the filter {@link Statement statement} to be used. |
| 137 | + * |
| 138 | + * @param statement must not be {@literal null}. |
| 139 | + * @return new instance of {@link TerminatingFind}. |
| 140 | + * @throws IllegalArgumentException if criteria is {@literal null}. |
| 141 | + */ |
| 142 | + default TerminatingFind<T> matching(Statement statement) { |
| 143 | + return matching(statement, Collections.emptyMap()); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Result type override (Optional). |
| 149 | + * |
| 150 | + * @param <T> returned type |
| 151 | + */ |
| 152 | + interface FindWithProjection<T> extends FindWithQuery<T> { |
| 153 | + |
| 154 | + /** |
| 155 | + * Define the target type fields should be mapped to. <br /> |
| 156 | + * Skip this step if you are anyway only interested in the original domain type. |
| 157 | + * |
| 158 | + * @param resultType must not be {@literal null}. |
| 159 | + * @param <R> result type. |
| 160 | + * @return new instance of {@link FindWithProjection}. |
| 161 | + * @throws IllegalArgumentException if resultType is {@literal null}. |
| 162 | + */ |
| 163 | + <R> FindWithQuery<R> as(Class<R> resultType); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Entry point for creating executable find operations. |
| 168 | + * |
| 169 | + * @param <T> returned type |
| 170 | + */ |
| 171 | + interface ExecutableFind<T> extends FindWithProjection<T> { |
| 172 | + } |
| 173 | +} |
0 commit comments