|
| 1 | +/* |
| 2 | + * Copyright 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.mongodb.core.mapping; |
| 17 | + |
| 18 | +import java.lang.annotation.Documented; |
| 19 | +import java.lang.annotation.ElementType; |
| 20 | +import java.lang.annotation.Retention; |
| 21 | +import java.lang.annotation.RetentionPolicy; |
| 22 | +import java.lang.annotation.Target; |
| 23 | + |
| 24 | +import javax.annotation.meta.When; |
| 25 | + |
| 26 | +import org.springframework.core.annotation.AliasFor; |
| 27 | + |
| 28 | +/** |
| 29 | + * The annotation to configure a value object as embedded (flattened out) in the target document. |
| 30 | + * <p /> |
| 31 | + * Depending on the {@link OnEmpty value} of {@link #onEmpty()} the property is set to {@literal null} or an empty |
| 32 | + * instance in the case all embedded values are {@literal null} when reading from the result set. |
| 33 | + * |
| 34 | + * @author Christoph Strobl |
| 35 | + * @since 3.2 |
| 36 | + */ |
| 37 | +@Documented |
| 38 | +@Retention(value = RetentionPolicy.RUNTIME) |
| 39 | +@Target(value = { ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD }) |
| 40 | +public @interface Embedded { |
| 41 | + |
| 42 | + /** |
| 43 | + * Set the load strategy for the embedded object if all contained fields yield {@literal null} values. |
| 44 | + * <p /> |
| 45 | + * {@link Nullable @Embedded.Nullable} and {@link Empty @Embedded.Empty} offer shortcuts for this. |
| 46 | + * |
| 47 | + * @return never {@link} null. |
| 48 | + */ |
| 49 | + OnEmpty onEmpty(); |
| 50 | + |
| 51 | + /** |
| 52 | + * @return prefix for columns in the embedded value object. An empty {@link String} by default. |
| 53 | + */ |
| 54 | + String prefix() default ""; |
| 55 | + |
| 56 | + /** |
| 57 | + * Load strategy to be used {@link Embedded#onEmpty()}. |
| 58 | + * |
| 59 | + * @author Christoph Strobl |
| 60 | + */ |
| 61 | + enum OnEmpty { |
| 62 | + USE_NULL, USE_EMPTY |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Shortcut for a nullable embedded property. |
| 67 | + * |
| 68 | + * <pre class="code"> |
| 69 | + * @Embedded.Nullable private Address address; |
| 70 | + * </pre> |
| 71 | + * |
| 72 | + * as alternative to the more verbose |
| 73 | + * |
| 74 | + * <pre class="code"> |
| 75 | + * @Embedded(onEmpty = USE_NULL) @javax.annotation.Nonnull(when = When.MAYBE) private Address address; |
| 76 | + * </pre> |
| 77 | + * |
| 78 | + * @author Christoph Strobl |
| 79 | + * @see Embedded#onEmpty() |
| 80 | + */ |
| 81 | + @Embedded(onEmpty = OnEmpty.USE_NULL) |
| 82 | + @Documented |
| 83 | + @Retention(RetentionPolicy.RUNTIME) |
| 84 | + @Target({ ElementType.FIELD, ElementType.METHOD }) |
| 85 | + @javax.annotation.Nonnull(when = When.MAYBE) |
| 86 | + @interface Nullable { |
| 87 | + |
| 88 | + /** |
| 89 | + * @return prefix for columns in the embedded value object. An empty {@link String} by default. |
| 90 | + */ |
| 91 | + @AliasFor(annotation = Embedded.class, attribute = "prefix") |
| 92 | + String prefix() default ""; |
| 93 | + |
| 94 | + /** |
| 95 | + * @return value for columns in the embedded value object. An empty {@link String} by default. |
| 96 | + */ |
| 97 | + @AliasFor(annotation = Embedded.class, attribute = "prefix") |
| 98 | + String value() default ""; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Shortcut for an empty embedded property. |
| 103 | + * |
| 104 | + * <pre class="code"> |
| 105 | + * @Embedded.Empty private Address address; |
| 106 | + * </pre> |
| 107 | + * |
| 108 | + * as alternative to the more verbose |
| 109 | + * |
| 110 | + * <pre class="code"> |
| 111 | + * @Embedded(onEmpty = USE_EMPTY) @javax.annotation.Nonnull(when = When.NEVER) private Address address; |
| 112 | + * </pre> |
| 113 | + * |
| 114 | + * @author Christoph Strobl |
| 115 | + * @see Embedded#onEmpty() |
| 116 | + */ |
| 117 | + @Embedded(onEmpty = OnEmpty.USE_EMPTY) |
| 118 | + @Documented |
| 119 | + @Retention(RetentionPolicy.RUNTIME) |
| 120 | + @Target({ ElementType.FIELD, ElementType.METHOD }) |
| 121 | + @javax.annotation.Nonnull(when = When.NEVER) |
| 122 | + @interface Empty { |
| 123 | + |
| 124 | + /** |
| 125 | + * @return prefix for columns in the embedded value object. An empty {@link String} by default. |
| 126 | + */ |
| 127 | + @AliasFor(annotation = Embedded.class, attribute = "prefix") |
| 128 | + String prefix() default ""; |
| 129 | + |
| 130 | + /** |
| 131 | + * @return value for columns in the embedded value object. An empty {@link String} by default. |
| 132 | + */ |
| 133 | + @AliasFor(annotation = Embedded.class, attribute = "prefix") |
| 134 | + String value() default ""; |
| 135 | + } |
| 136 | +} |
0 commit comments