|
| 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.repository.query; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | +import java.util.function.Function; |
| 21 | +import java.util.function.LongSupplier; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +import org.apiguardian.api.API; |
| 25 | +import org.springframework.data.domain.Example; |
| 26 | +import org.springframework.data.domain.Page; |
| 27 | +import org.springframework.data.domain.Pageable; |
| 28 | +import org.springframework.data.domain.Sort; |
| 29 | +import org.springframework.data.neo4j.core.FluentFindOperation; |
| 30 | +import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext; |
| 31 | +import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery; |
| 32 | +import org.springframework.data.support.PageableExecutionUtils; |
| 33 | +import org.springframework.lang.Nullable; |
| 34 | + |
| 35 | +/** |
| 36 | + * Immutable implementation of a {@link FetchableFluentQuery}. All |
| 37 | + * methods that return a {@link FetchableFluentQuery} return a new instance, the original instance won't be |
| 38 | + * modified. |
| 39 | + * |
| 40 | + * @author Michael J. Simons |
| 41 | + * @param <S> Source type |
| 42 | + * @param <R> Result type |
| 43 | + * @since 6.2 |
| 44 | + */ |
| 45 | +@API(status = API.Status.INTERNAL, since = "6.2") |
| 46 | +final class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<R> implements FetchableFluentQuery<R> { |
| 47 | + |
| 48 | + private final Neo4jMappingContext mappingContext; |
| 49 | + |
| 50 | + private final Example<S> example; |
| 51 | + |
| 52 | + private final FluentFindOperation findOperation; |
| 53 | + |
| 54 | + private final Function<Example<S>, Long> countOperation; |
| 55 | + |
| 56 | + private final Function<Example<S>, Boolean> existsOperation; |
| 57 | + |
| 58 | + FetchableFluentQueryByExample( |
| 59 | + Example<S> example, |
| 60 | + Class<R> resultType, |
| 61 | + Neo4jMappingContext mappingContext, |
| 62 | + FluentFindOperation findOperation, |
| 63 | + Function<Example<S>, Long> countOperation, |
| 64 | + Function<Example<S>, Boolean> existsOperation |
| 65 | + ) { |
| 66 | + this(example, resultType, mappingContext, findOperation, countOperation, existsOperation, Sort.unsorted(), |
| 67 | + null); |
| 68 | + } |
| 69 | + |
| 70 | + FetchableFluentQueryByExample( |
| 71 | + Example<S> example, |
| 72 | + Class<R> resultType, |
| 73 | + Neo4jMappingContext mappingContext, |
| 74 | + FluentFindOperation findOperation, |
| 75 | + Function<Example<S>, Long> countOperation, |
| 76 | + Function<Example<S>, Boolean> existsOperation, |
| 77 | + Sort sort, |
| 78 | + @Nullable Collection<String> properties |
| 79 | + ) { |
| 80 | + super(resultType, sort, properties); |
| 81 | + this.mappingContext = mappingContext; |
| 82 | + this.example = example; |
| 83 | + this.findOperation = findOperation; |
| 84 | + this.countOperation = countOperation; |
| 85 | + this.existsOperation = existsOperation; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + @SuppressWarnings("HiddenField") |
| 90 | + public FetchableFluentQuery<R> sortBy(Sort sort) { |
| 91 | + |
| 92 | + return new FetchableFluentQueryByExample<>(this.example, this.resultType, this.mappingContext, this.findOperation, |
| 93 | + this.countOperation, this.existsOperation, this.sort.and(sort), this.properties); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + @SuppressWarnings("HiddenField") |
| 98 | + public <NR> FetchableFluentQuery<NR> as(Class<NR> resultType) { |
| 99 | + |
| 100 | + return new FetchableFluentQueryByExample<>(this.example, resultType, this.mappingContext, this.findOperation, |
| 101 | + this.countOperation, this.existsOperation); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + @SuppressWarnings("HiddenField") |
| 106 | + public FetchableFluentQuery<R> project(Collection<String> properties) { |
| 107 | + |
| 108 | + return new FetchableFluentQueryByExample<>(this.example, this.resultType, this.mappingContext, this.findOperation, |
| 109 | + this.countOperation, this.existsOperation, this.sort, mergeProperties(properties)); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public R one() { |
| 114 | + |
| 115 | + return findOperation.find(example.getProbeType()) |
| 116 | + .as(resultType) |
| 117 | + .matching(QueryFragmentsAndParameters.forExample(mappingContext, example, sort, |
| 118 | + createIncludedFieldsPredicate())) |
| 119 | + .oneValue(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public R first() { |
| 124 | + |
| 125 | + List<R> all = all(); |
| 126 | + return all.isEmpty() ? null : all.get(0); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public List<R> all() { |
| 131 | + |
| 132 | + return findOperation.find(example.getProbeType()) |
| 133 | + .as(resultType) |
| 134 | + .matching(QueryFragmentsAndParameters.forExample(mappingContext, example, sort, |
| 135 | + createIncludedFieldsPredicate())) |
| 136 | + .all(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public Page<R> page(Pageable pageable) { |
| 141 | + |
| 142 | + List<R> page = findOperation.find(example.getProbeType()) |
| 143 | + .as(resultType) |
| 144 | + .matching(QueryFragmentsAndParameters.forExample(mappingContext, example, pageable, |
| 145 | + createIncludedFieldsPredicate())) |
| 146 | + .all(); |
| 147 | + |
| 148 | + LongSupplier totalCountSupplier = this::count; |
| 149 | + return PageableExecutionUtils.getPage(page, pageable, totalCountSupplier); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public Stream<R> stream() { |
| 154 | + return all().stream(); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public long count() { |
| 159 | + return countOperation.apply(example); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public boolean exists() { |
| 164 | + return existsOperation.apply(example); |
| 165 | + } |
| 166 | +} |
0 commit comments