|
| 1 | +/* |
| 2 | + * Copyright 2019, TeamDev. 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 io.spine.server.storage.datastore; |
| 22 | + |
| 23 | +import com.google.cloud.datastore.BooleanValue; |
| 24 | +import com.google.cloud.datastore.DoubleValue; |
| 25 | +import com.google.cloud.datastore.LongValue; |
| 26 | +import com.google.cloud.datastore.StringValue; |
| 27 | +import com.google.cloud.datastore.TimestampValue; |
| 28 | +import com.google.cloud.datastore.Value; |
| 29 | +import com.google.common.collect.ImmutableSortedMap; |
| 30 | +import com.google.protobuf.Message; |
| 31 | +import com.google.protobuf.Timestamp; |
| 32 | +import io.spine.core.Version; |
| 33 | +import io.spine.string.Stringifiers; |
| 34 | + |
| 35 | +import java.io.Serializable; |
| 36 | +import java.util.Comparator; |
| 37 | + |
| 38 | +import static com.google.cloud.Timestamp.ofTimeSecondsAndNanos; |
| 39 | +import static io.spine.util.Exceptions.newIllegalArgumentException; |
| 40 | + |
| 41 | +final class DefaultColumnTypeRegistry implements ColumnTypeRegistry { |
| 42 | + |
| 43 | + private static final ImmutableSortedMap<Class<?>, PersistenceStrategy<?>> defaultStrategies = |
| 44 | + defaultPolicies(); |
| 45 | + |
| 46 | + @SuppressWarnings("unchecked") // Ensured by `defaultPolicies` declaration. |
| 47 | + @Override |
| 48 | + public <T> PersistenceStrategy<T> persistenceStrategyOf(Class<T> clazz) { |
| 49 | + PersistenceStrategy<?> strategy = defaultStrategies.get(clazz); |
| 50 | + if (strategy == null) { |
| 51 | + strategy = searchForSuperclassStrategy(clazz); |
| 52 | + } |
| 53 | + PersistenceStrategy<T> result = (PersistenceStrategy<T>) strategy; |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + private static <T> PersistenceStrategy<?> searchForSuperclassStrategy(Class<T> clazz) { |
| 58 | + PersistenceStrategy<?> result = |
| 59 | + defaultPolicies().keySet() |
| 60 | + .stream() |
| 61 | + .filter(cls -> cls.isAssignableFrom(clazz)) |
| 62 | + .map(defaultStrategies::get) |
| 63 | + .findFirst() |
| 64 | + .orElseThrow(() -> classNotFound(clazz)); |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + private static <T> IllegalArgumentException classNotFound(Class<T> clazz) { |
| 69 | + throw newIllegalArgumentException("The class %s is not found among registered types.", |
| 70 | + clazz.getCanonicalName()); |
| 71 | + } |
| 72 | + |
| 73 | + private static ImmutableSortedMap<Class<?>, PersistenceStrategy<?>> defaultPolicies() { |
| 74 | + ImmutableSortedMap.Builder<Class<?>, PersistenceStrategy<?>> policies = |
| 75 | + ImmutableSortedMap.orderedBy(new SimplisticClassComparator()); |
| 76 | + |
| 77 | + policies.put(String.class, new DefaultStringPersistenceStrategy()); |
| 78 | + policies.put(Integer.class, new DefaultIntegerPersistenceStrategy()); |
| 79 | + policies.put(Long.class, new DefaultLongPersistenceStrategy()); |
| 80 | + policies.put(Double.class, new DefaultDoublePersistenceStrategy()); |
| 81 | + policies.put(Float.class, new DefaultFloatPersistenceStrategy()); |
| 82 | + policies.put(Boolean.class, new DefaultBooleanPersistenceStrategy()); |
| 83 | + policies.put(Timestamp.class, new DefaultTimestampPersistenceStrategy()); |
| 84 | + policies.put(Version.class, new DefaultVersionPersistenceStrategy()); |
| 85 | + policies.put(Enum.class, new DefaultEnumPersistenceStrategy()); |
| 86 | + policies.put(Message.class, new DefaultMessagePersistenceStrategy()); |
| 87 | + |
| 88 | + return policies.build(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * A class comparator for the {@linkplain #defaultStrategies} map. |
| 93 | + * |
| 94 | + * <p>Compares classes in such a way so the subclasses go <b>before</b> their superclasses. |
| 95 | + * |
| 96 | + * <p>For the classes without "parent-child" relationship there is no predefined order of |
| 97 | + * storing. |
| 98 | + */ |
| 99 | + private static class SimplisticClassComparator implements Comparator<Class<?>>, Serializable { |
| 100 | + |
| 101 | + private static final long serialVersionUID = 0L; |
| 102 | + |
| 103 | + @Override |
| 104 | + public int compare(Class<?> o1, Class<?> o2) { |
| 105 | + if (o1.equals(o2)) { |
| 106 | + return 0; |
| 107 | + } |
| 108 | + if (o1.isAssignableFrom(o2)) { |
| 109 | + return 1; |
| 110 | + } |
| 111 | + return -1; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static class DefaultStringPersistenceStrategy implements PersistenceStrategy<String> { |
| 116 | + |
| 117 | + @Override |
| 118 | + public Value<?> apply(String s) { |
| 119 | + return StringValue.of(s); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static class DefaultIntegerPersistenceStrategy implements PersistenceStrategy<Integer> { |
| 124 | + |
| 125 | + @Override |
| 126 | + public Value<?> apply(Integer integer) { |
| 127 | + return LongValue.of(integer); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static class DefaultLongPersistenceStrategy implements PersistenceStrategy<Long> { |
| 132 | + |
| 133 | + @Override |
| 134 | + public Value<?> apply(Long aLong) { |
| 135 | + return LongValue.of(aLong); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static class DefaultDoublePersistenceStrategy implements PersistenceStrategy<Double> { |
| 140 | + |
| 141 | + @Override |
| 142 | + public Value<?> apply(Double aDouble) { |
| 143 | + return DoubleValue.of(aDouble); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private static class DefaultFloatPersistenceStrategy implements PersistenceStrategy<Float> { |
| 148 | + |
| 149 | + @Override |
| 150 | + public Value<?> apply(Float aFloat) { |
| 151 | + return DoubleValue.of(aFloat); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private static class DefaultBooleanPersistenceStrategy |
| 156 | + implements PersistenceStrategy<Boolean> { |
| 157 | + |
| 158 | + @Override |
| 159 | + public Value<?> apply(Boolean aBoolean) { |
| 160 | + return BooleanValue.of(aBoolean); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private static class DefaultTimestampPersistenceStrategy |
| 165 | + implements PersistenceStrategy<Timestamp> { |
| 166 | + |
| 167 | + @Override |
| 168 | + public Value<?> apply(Timestamp timestamp) { |
| 169 | + return TimestampValue.of( |
| 170 | + ofTimeSecondsAndNanos(timestamp.getSeconds(), timestamp.getNanos()) |
| 171 | + ); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private static class DefaultVersionPersistenceStrategy |
| 176 | + implements PersistenceStrategy<Version> { |
| 177 | + |
| 178 | + @Override |
| 179 | + public Value<?> apply(Version version) { |
| 180 | + int versionNumber = version.getNumber(); |
| 181 | + return LongValue.of(versionNumber); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private static class DefaultEnumPersistenceStrategy implements PersistenceStrategy<Enum<?>> { |
| 186 | + |
| 187 | + @Override |
| 188 | + public Value<?> apply(Enum<?> anEnum) { |
| 189 | + int ordinal = anEnum.ordinal(); |
| 190 | + return LongValue.of(ordinal); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private static class DefaultMessagePersistenceStrategy |
| 195 | + implements PersistenceStrategy<Message> { |
| 196 | + |
| 197 | + @Override |
| 198 | + public Value<?> apply(Message message) { |
| 199 | + String messageString = Stringifiers.toString(message); |
| 200 | + return StringValue.of(messageString); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments