Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] move command strategies to separate package
Signed-off-by: Dominik Guggemos <dominik.guggemos@bosch-si.com>
- Loading branch information
Showing
9 changed files
with
232 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...pse/ditto/services/things/persistence/actors/strategies/AbstractThingCommandStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) 2017 Bosch Software Innovations GmbH. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/org/documents/epl-2.0/index.php | ||
* Contributors: | ||
* Bosch Software Innovations GmbH - initial contribution | ||
* | ||
*/ | ||
package org.eclipse.ditto.services.things.persistence.actors.strategies; | ||
|
||
import java.text.MessageFormat; | ||
import java.time.Instant; | ||
|
||
import javax.annotation.concurrent.NotThreadSafe; | ||
|
||
import org.eclipse.ditto.services.things.persistence.actors.ThingPersistenceActor; | ||
import org.eclipse.ditto.signals.commands.base.Command; | ||
import org.eclipse.ditto.signals.commands.things.ThingCommand; | ||
|
||
import akka.event.DiagnosticLoggingAdapter; | ||
import akka.japi.pf.FI; | ||
|
||
/** | ||
* This extension of {@link AbstractReceiveStrategy} is for handling {@link ThingCommand}. | ||
* | ||
* @param <T> type of the class this strategy matches against. | ||
*/ | ||
@NotThreadSafe | ||
public abstract class AbstractThingCommandStrategy<T extends Command> extends AbstractReceiveStrategy<T> { | ||
|
||
/** | ||
* Constructs a new {@code AbstractThingCommandStrategy} object. | ||
* | ||
* @param theMatchingClass the class of the message this strategy reacts to. | ||
* @param theLogger the logger to use for logging. | ||
* @throws NullPointerException if {@code theMatchingClass} is {@code null}. | ||
*/ | ||
AbstractThingCommandStrategy(final Class<T> theMatchingClass, final DiagnosticLoggingAdapter theLogger) { | ||
super(theMatchingClass, theLogger); | ||
} | ||
|
||
@Override | ||
public FI.TypedPredicate<T> getPredicate() { | ||
return command -> null != thing() && thing().getId() | ||
.filter(command.getId()::equals) | ||
.isPresent(); | ||
} | ||
|
||
@Override | ||
public FI.UnitApply<T> getUnhandledFunction() { | ||
return command -> { | ||
throw new IllegalArgumentException( | ||
MessageFormat.format(ThingPersistenceActor.UNHANDLED_MESSAGE_TEMPLATE, command.getId())); | ||
}; | ||
} | ||
|
||
protected static Instant eventTimestamp() { | ||
return Instant.now(); | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...java/org/eclipse/ditto/services/things/persistence/actors/strategies/ImmutableResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2017 Bosch Software Innovations GmbH. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/org/documents/epl-2.0/index.php | ||
* Contributors: | ||
* Bosch Software Innovations GmbH - initial contribution | ||
* | ||
*/ | ||
|
||
package org.eclipse.ditto.services.things.persistence.actors.strategies; | ||
|
||
import java.util.Optional; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException; | ||
import org.eclipse.ditto.signals.commands.things.ThingCommandResponse; | ||
import org.eclipse.ditto.signals.events.things.ThingEvent; | ||
|
||
class ImmutableResult implements ReceiveStrategy.Result { | ||
|
||
@Nullable private final ThingEvent eventToPersist; | ||
@Nullable private final ThingCommandResponse response; | ||
@Nullable private final DittoRuntimeException exception; | ||
|
||
private ImmutableResult(@Nullable final ThingEvent eventToPersist, | ||
@Nullable final ThingCommandResponse response, | ||
@Nullable final DittoRuntimeException exception) { | ||
this.eventToPersist = eventToPersist; | ||
this.response = response; | ||
this.exception = exception; | ||
} | ||
|
||
static ReceiveStrategy.Result of(final ThingEvent eventToPersist, final ThingCommandResponse response) { | ||
return new ImmutableResult(eventToPersist, response, null); | ||
} | ||
|
||
@Override | ||
public Optional<ThingEvent> getEventToPersist() { | ||
return Optional.ofNullable(eventToPersist); | ||
} | ||
|
||
@Override | ||
public Optional<ThingCommandResponse> getResponse() { | ||
return Optional.ofNullable(response); | ||
} | ||
|
||
@Override | ||
public Optional<DittoRuntimeException> getException() { | ||
return Optional.ofNullable(exception); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...eclipse/ditto/services/things/persistence/actors/strategies/ModifyAttributesStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) 2017 Bosch Software Innovations GmbH. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/org/documents/epl-2.0/index.php | ||
* Contributors: | ||
* Bosch Software Innovations GmbH - initial contribution | ||
* | ||
*/ | ||
package org.eclipse.ditto.services.things.persistence.actors.strategies; | ||
|
||
import javax.annotation.concurrent.NotThreadSafe; | ||
|
||
import org.eclipse.ditto.model.base.headers.DittoHeaders; | ||
import org.eclipse.ditto.model.things.Thing; | ||
import org.eclipse.ditto.signals.commands.things.modify.ModifyAttributes; | ||
import org.eclipse.ditto.signals.commands.things.modify.ModifyAttributesResponse; | ||
import org.eclipse.ditto.signals.commands.things.modify.ThingModifyCommandResponse; | ||
import org.eclipse.ditto.signals.events.things.AttributesCreated; | ||
import org.eclipse.ditto.signals.events.things.AttributesModified; | ||
import org.eclipse.ditto.signals.events.things.ThingModifiedEvent; | ||
|
||
/** | ||
* This strategy handles the {@link ModifyAttributes} command. | ||
*/ | ||
@NotThreadSafe | ||
public final class ModifyAttributesStrategy extends AbstractThingCommandStrategy<ModifyAttributes> { | ||
|
||
/** | ||
* Constructs a new {@code ModifyAttributesStrategy} object. | ||
*/ | ||
public ModifyAttributesStrategy() { | ||
super(ModifyAttributes.class, null); | ||
} | ||
|
||
@Override | ||
protected Result doApply(final Context context, final ModifyAttributes command) { | ||
final DittoHeaders dittoHeaders = command.getDittoHeaders(); | ||
final ThingModifiedEvent eventToPersist; | ||
final ThingModifyCommandResponse response; | ||
|
||
final String thingId = context.getThingId(); | ||
final Thing thing = context.getThing(); | ||
final long nextRevision = context.nextRevision(); | ||
|
||
if (thing.getAttributes().isPresent()) { | ||
eventToPersist = AttributesModified.of(thingId, command.getAttributes(), nextRevision, | ||
eventTimestamp(), dittoHeaders); | ||
response = ModifyAttributesResponse.modified(thingId, dittoHeaders); | ||
} else { | ||
eventToPersist = AttributesCreated.of(thingId, command.getAttributes(), nextRevision, | ||
eventTimestamp(), dittoHeaders); | ||
response = ModifyAttributesResponse.created(thingId, command.getAttributes(), dittoHeaders); | ||
} | ||
|
||
return ImmutableResult.of(eventToPersist, response); | ||
} | ||
|
||
} |
Oops, something went wrong.