|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + */ |
| 13 | +package org.eclipse.ditto.client.live.commands; |
| 14 | + |
| 15 | +import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull; |
| 16 | + |
| 17 | +import java.text.MessageFormat; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.Function; |
| 22 | + |
| 23 | +import javax.annotation.Nonnull; |
| 24 | +import javax.annotation.concurrent.Immutable; |
| 25 | + |
| 26 | +import org.eclipse.ditto.client.live.commands.base.LiveCommand; |
| 27 | +import org.eclipse.ditto.client.live.commands.modify.ModifyLiveCommandFactory; |
| 28 | +import org.eclipse.ditto.client.live.commands.query.QueryLiveCommandFactory; |
| 29 | +import org.eclipse.ditto.signals.commands.base.Command; |
| 30 | +import org.eclipse.ditto.signals.commands.things.modify.CreateThing; |
| 31 | +import org.eclipse.ditto.signals.commands.things.modify.DeleteAttribute; |
| 32 | +import org.eclipse.ditto.signals.commands.things.modify.DeleteAttributes; |
| 33 | +import org.eclipse.ditto.signals.commands.things.modify.DeleteFeature; |
| 34 | +import org.eclipse.ditto.signals.commands.things.modify.DeleteFeatureDefinition; |
| 35 | +import org.eclipse.ditto.signals.commands.things.modify.DeleteFeatureProperties; |
| 36 | +import org.eclipse.ditto.signals.commands.things.modify.DeleteFeatureProperty; |
| 37 | +import org.eclipse.ditto.signals.commands.things.modify.DeleteFeatures; |
| 38 | +import org.eclipse.ditto.signals.commands.things.modify.DeleteThing; |
| 39 | +import org.eclipse.ditto.signals.commands.things.modify.MergeThing; |
| 40 | +import org.eclipse.ditto.signals.commands.things.modify.ModifyAttribute; |
| 41 | +import org.eclipse.ditto.signals.commands.things.modify.ModifyAttributes; |
| 42 | +import org.eclipse.ditto.signals.commands.things.modify.ModifyFeature; |
| 43 | +import org.eclipse.ditto.signals.commands.things.modify.ModifyFeatureDefinition; |
| 44 | +import org.eclipse.ditto.signals.commands.things.modify.ModifyFeatureProperties; |
| 45 | +import org.eclipse.ditto.signals.commands.things.modify.ModifyFeatureProperty; |
| 46 | +import org.eclipse.ditto.signals.commands.things.modify.ModifyFeatures; |
| 47 | +import org.eclipse.ditto.signals.commands.things.modify.ModifyThing; |
| 48 | +import org.eclipse.ditto.signals.commands.things.query.RetrieveAttribute; |
| 49 | +import org.eclipse.ditto.signals.commands.things.query.RetrieveAttributes; |
| 50 | +import org.eclipse.ditto.signals.commands.things.query.RetrieveFeature; |
| 51 | +import org.eclipse.ditto.signals.commands.things.query.RetrieveFeatureDefinition; |
| 52 | +import org.eclipse.ditto.signals.commands.things.query.RetrieveFeatureProperties; |
| 53 | +import org.eclipse.ditto.signals.commands.things.query.RetrieveFeatureProperty; |
| 54 | +import org.eclipse.ditto.signals.commands.things.query.RetrieveFeatures; |
| 55 | +import org.eclipse.ditto.signals.commands.things.query.RetrieveThing; |
| 56 | +import org.eclipse.ditto.signals.commands.things.query.RetrieveThings; |
| 57 | + |
| 58 | +/** |
| 59 | + * A factory for creating immutable instances of {@link LiveCommand} based on existing <em>Twin Commands</em>. |
| 60 | + * |
| 61 | + * @since 2.0.0 |
| 62 | + */ |
| 63 | +@Immutable |
| 64 | +public final class LiveCommandFactory { |
| 65 | + |
| 66 | + private static final int STRATEGIES_NUMBER = 26; |
| 67 | + private static final LiveCommandFactory INSTANCE = new LiveCommandFactory(); |
| 68 | + |
| 69 | + private final Map<String, Function<Command, LiveCommand>> mappingStrategies; |
| 70 | + |
| 71 | + private LiveCommandFactory() { |
| 72 | + mappingStrategies = Collections.unmodifiableMap(initMappingStrategies()); |
| 73 | + } |
| 74 | + |
| 75 | + private static Map<String, Function<Command, LiveCommand>> initMappingStrategies() { |
| 76 | + final Map<String, Function<Command, LiveCommand>> result = new HashMap<>(STRATEGIES_NUMBER); |
| 77 | + result.put(CreateThing.TYPE, ModifyLiveCommandFactory::createThing); |
| 78 | + result.put(DeleteAttribute.TYPE, ModifyLiveCommandFactory::deleteAttribute); |
| 79 | + result.put(DeleteAttributes.TYPE, ModifyLiveCommandFactory::deleteAttributes); |
| 80 | + result.put(DeleteFeature.TYPE, ModifyLiveCommandFactory::deleteFeature); |
| 81 | + result.put(DeleteFeatureDefinition.TYPE, ModifyLiveCommandFactory::deleteFeatureDefinition); |
| 82 | + result.put(DeleteFeatureProperties.TYPE, ModifyLiveCommandFactory::deleteFeatureProperties); |
| 83 | + result.put(DeleteFeatureProperty.TYPE, ModifyLiveCommandFactory::deleteFeatureProperty); |
| 84 | + result.put(DeleteFeatures.TYPE, ModifyLiveCommandFactory::deleteFeatures); |
| 85 | + result.put(DeleteThing.TYPE, ModifyLiveCommandFactory::deleteThing); |
| 86 | + result.put(ModifyAttribute.TYPE, ModifyLiveCommandFactory::modifyAttribute); |
| 87 | + result.put(ModifyAttributes.TYPE, ModifyLiveCommandFactory::modifyAttributes); |
| 88 | + result.put(ModifyFeature.TYPE, ModifyLiveCommandFactory::modifyFeature); |
| 89 | + result.put(ModifyFeatureDefinition.TYPE, ModifyLiveCommandFactory::modifyFeatureDefinition); |
| 90 | + result.put(ModifyFeatureProperties.TYPE, ModifyLiveCommandFactory::modifyFeatureProperties); |
| 91 | + result.put(ModifyFeatureProperty.TYPE, ModifyLiveCommandFactory::modifyFeatureProperty); |
| 92 | + result.put(ModifyFeatures.TYPE, ModifyLiveCommandFactory::modifyFeatures); |
| 93 | + result.put(ModifyThing.TYPE, ModifyLiveCommandFactory::modifyThing); |
| 94 | + result.put(MergeThing.TYPE, ModifyLiveCommandFactory::mergeThing); |
| 95 | + |
| 96 | + result.put(RetrieveAttribute.TYPE, QueryLiveCommandFactory::retrieveAttribute); |
| 97 | + result.put(RetrieveAttributes.TYPE, QueryLiveCommandFactory::retrieveAttributes); |
| 98 | + result.put(RetrieveFeature.TYPE, QueryLiveCommandFactory::retrieveFeature); |
| 99 | + result.put(RetrieveFeatureDefinition.TYPE, QueryLiveCommandFactory::retrieveFeatureDefinition); |
| 100 | + result.put(RetrieveFeatureProperties.TYPE, QueryLiveCommandFactory::retrieveFeatureProperties); |
| 101 | + result.put(RetrieveFeatureProperty.TYPE, QueryLiveCommandFactory::retrieveFeatureProperty); |
| 102 | + result.put(RetrieveFeatures.TYPE, QueryLiveCommandFactory::retrieveFeatures); |
| 103 | + result.put(RetrieveThing.TYPE, QueryLiveCommandFactory::retrieveThing); |
| 104 | + result.put(RetrieveThings.TYPE, QueryLiveCommandFactory::retrieveThings); |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns an instance of {@code LiveCommandFactory}. |
| 110 | + * |
| 111 | + * @return the instance. |
| 112 | + */ |
| 113 | + @Nonnull |
| 114 | + public static LiveCommandFactory getInstance() { |
| 115 | + return INSTANCE; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns an immutable {@link LiveCommand} which is associated with the specified Command. |
| 120 | + * |
| 121 | + * @param command the command to get a LiveCommand for. |
| 122 | + * @return the LiveCommand. |
| 123 | + * @throws NullPointerException if {@code command} is {@code null}. |
| 124 | + * @throws IllegalArgumentException if the given command cannot be mapped to a LiveCommand because there is no |
| 125 | + * mapping strategy associated with the command's type. |
| 126 | + */ |
| 127 | + @Nonnull |
| 128 | + public LiveCommand getLiveCommand(@Nonnull final Command<?> command) { |
| 129 | + checkNotNull(command, "command"); |
| 130 | + |
| 131 | + final String commandType = command.getType(); |
| 132 | + final Function<Command, LiveCommand> commandToLiveCommand = mappingStrategies.get(commandType); |
| 133 | + if (null == commandToLiveCommand) { |
| 134 | + final String msgTemplate = "No mapping strategy for command <{0}> available!" |
| 135 | + + " The command type <{1}> is unknown!"; |
| 136 | + throw new IllegalArgumentException(MessageFormat.format(msgTemplate, command, commandType)); |
| 137 | + } |
| 138 | + return commandToLiveCommand.apply(command); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments