|
| 1 | +/* |
| 2 | + * Copyright 2025 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.cassandra.repository.support; |
| 17 | + |
| 18 | +import org.springframework.data.cassandra.core.CassandraOperations; |
| 19 | +import org.springframework.data.cassandra.repository.query.CassandraEntityInformation; |
| 20 | +import org.springframework.data.repository.core.RepositoryMetadata; |
| 21 | +import org.springframework.data.repository.core.support.RepositoryComposition.RepositoryFragments; |
| 22 | +import org.springframework.data.repository.core.support.RepositoryFragmentsContributor; |
| 23 | +import org.springframework.util.Assert; |
| 24 | + |
| 25 | +/** |
| 26 | + * Cassandra-specific {@link RepositoryFragmentsContributor} contributing fragments based on the repository. |
| 27 | + * <p> |
| 28 | + * Implementations must define a no-args constructor. |
| 29 | + * |
| 30 | + * @author Chris Bono |
| 31 | + * @since 5.0 |
| 32 | + */ |
| 33 | +public interface CassandraRepositoryFragmentsContributor extends RepositoryFragmentsContributor { |
| 34 | + |
| 35 | + CassandraRepositoryFragmentsContributor DEFAULT = DefaultCassandraRepositoryFragmentsContributor.INSTANCE; |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns a composed {@code CassandraRepositoryFragmentsContributor} that first applies this contributor to its inputs, |
| 39 | + * and then applies the {@code after} contributor concatenating effectively both results. If evaluation of either |
| 40 | + * contributors throws an exception, it is relayed to the caller of the composed contributor. |
| 41 | + * |
| 42 | + * @param after the contributor to apply after this contributor is applied. |
| 43 | + * @return a composed contributor that first applies this contributor and then applies the {@code after} contributor. |
| 44 | + */ |
| 45 | + default CassandraRepositoryFragmentsContributor andThen(CassandraRepositoryFragmentsContributor after) { |
| 46 | + |
| 47 | + Assert.notNull(after, "CassandraRepositoryFragmentsContributor must not be null"); |
| 48 | + |
| 49 | + return new CassandraRepositoryFragmentsContributor() { |
| 50 | + |
| 51 | + @Override |
| 52 | + public RepositoryFragments contribute(RepositoryMetadata metadata, |
| 53 | + CassandraEntityInformation<?, ?> entityInformation, CassandraOperations operations) { |
| 54 | + return CassandraRepositoryFragmentsContributor.this.contribute(metadata, entityInformation, operations) |
| 55 | + .append(after.contribute(metadata, entityInformation, operations)); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public RepositoryFragments describe(RepositoryMetadata metadata) { |
| 60 | + return CassandraRepositoryFragmentsContributor.this.describe(metadata).append(after.describe(metadata)); |
| 61 | + } |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates {@link RepositoryFragments} based on {@link RepositoryMetadata} to add |
| 67 | + * Cassandra-specific extensions. |
| 68 | + * |
| 69 | + * @param metadata repository metadata. |
| 70 | + * @param entityInformation must not be {@literal null}. |
| 71 | + * @param operations must not be {@literal null}. |
| 72 | + * @return {@link RepositoryFragments} to be added to the repository. |
| 73 | + */ |
| 74 | + RepositoryFragments contribute(RepositoryMetadata metadata, |
| 75 | + CassandraEntityInformation<?, ?> entityInformation, CassandraOperations operations); |
| 76 | + |
| 77 | + /** |
| 78 | + * Implementation of {@link CassandraRepositoryFragmentsContributor} that contributes empty fragments by default. |
| 79 | + * |
| 80 | + * @author Chris Bono |
| 81 | + * @since 5.0 |
| 82 | + */ |
| 83 | + enum DefaultCassandraRepositoryFragmentsContributor implements CassandraRepositoryFragmentsContributor { |
| 84 | + |
| 85 | + INSTANCE; |
| 86 | + |
| 87 | + @Override |
| 88 | + public RepositoryFragments contribute(RepositoryMetadata metadata, |
| 89 | + CassandraEntityInformation<?, ?> entityInformation, CassandraOperations operations) { |
| 90 | + return RepositoryFragments.empty(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public RepositoryFragments describe(RepositoryMetadata metadata) { |
| 95 | + return RepositoryFragments.empty(); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments