|
| 1 | +/* |
| 2 | + * Copyright 2016, TeamDev Ltd. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and/or binary forms, with or without |
| 5 | + * modification, must retain the above copyright notice and the following |
| 6 | + * disclaimer. |
| 7 | + * |
| 8 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 9 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 10 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 11 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 12 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 13 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 14 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 15 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 16 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 17 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 18 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 19 | + */ |
| 20 | + |
| 21 | +package org.spine3.base; |
| 22 | + |
| 23 | +import com.google.protobuf.Any; |
| 24 | +import com.google.protobuf.Message; |
| 25 | + |
| 26 | +import javax.annotation.Nullable; |
| 27 | + |
| 28 | +import static org.spine3.protobuf.Values.pack; |
| 29 | +import static org.spine3.protobuf.Messages.toAny; |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * Factories for constructing {@link ValueMismatch} instances for different types of attributes. |
| 34 | + */ |
| 35 | +public class Mismatch { |
| 36 | + |
| 37 | + private Mismatch() { |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a {@link ValueMismatch} instance for a string attribute. |
| 42 | + * |
| 43 | + * @param expected the value expected by a command, or {@code null} if the command expects not populated field |
| 44 | + * @param actual the value found in an entity, or {@code null} if the value is not set |
| 45 | + * @param requested the value requested as new one in the original command |
| 46 | + * @param version the current version of the entity |
| 47 | + * @return info on the mismatch |
| 48 | + */ |
| 49 | + public static ValueMismatch of(@Nullable String expected, @Nullable String actual, String requested, int version) { |
| 50 | + final ValueMismatch.Builder builder = ValueMismatch.newBuilder(); |
| 51 | + if (expected != null) { |
| 52 | + builder.setExpected(pack(expected)); |
| 53 | + } |
| 54 | + |
| 55 | + if (actual != null) { |
| 56 | + builder.setActual(pack(actual)); |
| 57 | + } |
| 58 | + |
| 59 | + builder.setRequested(pack(requested)); |
| 60 | + builder.setVersion(version); |
| 61 | + |
| 62 | + return builder.build(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a {@link ValueMismatch} instance for a integer attribute. |
| 67 | + * |
| 68 | + * @param expected the value expected by a command |
| 69 | + * @param actual the value actual in an entity |
| 70 | + * @param requested the value requested as new one in the original command |
| 71 | + * @param version the current version of the entity |
| 72 | + * @return info on the mismatch |
| 73 | + */ |
| 74 | + public static ValueMismatch of(int expected, int actual, int requested, int version) { |
| 75 | + final ValueMismatch.Builder builder = ValueMismatch.newBuilder(); |
| 76 | + builder.setExpected(pack(expected)); |
| 77 | + builder.setActual(pack(actual)); |
| 78 | + builder.setRequested(pack(requested)); |
| 79 | + builder.setVersion(version); |
| 80 | + |
| 81 | + return builder.build(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Creates a {@link ValueMismatch} instance for a long integer attribute. |
| 86 | + * |
| 87 | + * @param expected the value expected by a command |
| 88 | + * @param actual the value actual in an entity |
| 89 | + * @param requested the value requested as new one in the original command |
| 90 | + * @param version the current version of the entity |
| 91 | + * @return info on the mismatch |
| 92 | + */ |
| 93 | + public static ValueMismatch of(long expected, long actual, long requested, int version) { |
| 94 | + final ValueMismatch.Builder builder = ValueMismatch.newBuilder(); |
| 95 | + builder.setExpected(pack(expected)); |
| 96 | + builder.setActual(pack(actual)); |
| 97 | + builder.setRequested(pack(requested)); |
| 98 | + builder.setVersion(version); |
| 99 | + |
| 100 | + return builder.build(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Creates a {@link ValueMismatch} instance for a float attribute. |
| 105 | + * |
| 106 | + * @param expected the value expected by a command |
| 107 | + * @param actual the value actual in an entity |
| 108 | + * @param requested the value requested as new one in the original command |
| 109 | + * @param version the current version of the entity |
| 110 | + * @return info on the mismatch |
| 111 | + */ |
| 112 | + public static ValueMismatch of(float expected, float actual, float requested, int version) { |
| 113 | + final ValueMismatch.Builder builder = ValueMismatch.newBuilder(); |
| 114 | + builder.setExpected(pack(expected)); |
| 115 | + builder.setActual(pack(actual)); |
| 116 | + builder.setRequested(pack(requested)); |
| 117 | + builder.setVersion(version); |
| 118 | + |
| 119 | + return builder.build(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Creates a {@link ValueMismatch} instance for a double attribute. |
| 124 | + * |
| 125 | + * @param expected the value expected by a command |
| 126 | + * @param actual the value actual in an entity |
| 127 | + * @param requested the value requested as new one in the original command |
| 128 | + * @param version the current version of the entity |
| 129 | + * @return info on the mismatch |
| 130 | + */ |
| 131 | + public static ValueMismatch of(double expected, double actual, double requested, int version) { |
| 132 | + final ValueMismatch.Builder builder = ValueMismatch.newBuilder(); |
| 133 | + builder.setExpected(pack(expected)); |
| 134 | + builder.setActual(pack(actual)); |
| 135 | + builder.setRequested(pack(requested)); |
| 136 | + builder.setVersion(version); |
| 137 | + |
| 138 | + return builder.build(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Creates a {@link ValueMismatch} instance for a boolean attribute. |
| 143 | + * |
| 144 | + * @param expected the value expected by a command |
| 145 | + * @param actual the value actual in an entity |
| 146 | + * @param requested the value requested as new one in the original command |
| 147 | + * @param version the current version of the entity |
| 148 | + * @return info on the mismatch |
| 149 | + */ |
| 150 | + public static ValueMismatch of(boolean expected, boolean actual, boolean requested, int version) { |
| 151 | + final ValueMismatch.Builder builder = ValueMismatch.newBuilder(); |
| 152 | + builder.setExpected(pack(expected)); |
| 153 | + builder.setActual(pack(actual)); |
| 154 | + builder.setRequested(pack(requested)); |
| 155 | + builder.setVersion(version); |
| 156 | + |
| 157 | + return builder.build(); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Creates a {@link ValueMismatch} instance for a Message attribute. |
| 162 | + * |
| 163 | + * @param expected the value expected by a command, or {@code null} if the command expects not populated field |
| 164 | + * @param actual the value actual in an entity, or {@code null} if the value is not set |
| 165 | + * @param requested the value requested as new one in the original command |
| 166 | + * @param version the current version of the entity |
| 167 | + * @return info on the mismatch |
| 168 | + */ |
| 169 | + public static ValueMismatch of(@Nullable Message expected, @Nullable Message actual, Message requested, int version) { |
| 170 | + final ValueMismatch.Builder builder = ValueMismatch.newBuilder(); |
| 171 | + |
| 172 | + if (expected != null) { |
| 173 | + builder.setExpected(toAny(expected)); |
| 174 | + } |
| 175 | + |
| 176 | + if (actual != null) { |
| 177 | + builder.setActual(toAny(actual)); |
| 178 | + } |
| 179 | + |
| 180 | + final Any requestedAny = toAny(requested); |
| 181 | + |
| 182 | + builder.setRequested(requestedAny); |
| 183 | + builder.setVersion(version); |
| 184 | + |
| 185 | + return builder.build(); |
| 186 | + } |
| 187 | +} |
0 commit comments