diff --git a/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/v2x/DuplicatableMessage.java b/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/v2x/DuplicatableMessage.java new file mode 100644 index 000000000..df5dd3ca3 --- /dev/null +++ b/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/v2x/DuplicatableMessage.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2024 Fraunhofer FOKUS and others. All rights reserved. + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contact: mosaic@fokus.fraunhofer.de + */ + +package org.eclipse.mosaic.lib.objects.v2x; + +/** + * A message that can duplicate itself. + * @param a class that extends {@link V2xMessage} + */ +public interface DuplicatableMessage { + + /** + * Creates a copy of the V2xMessage. + * The message gets a new ID and routing (to use current vehicle position). + * Only its contents (e.g. payload, type, ...) are copied. + * + * @param routing contains current vehicle position + * @return cloned message + */ + T duplicate(MessageRouting routing); +} diff --git a/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/v2x/GenericV2xMessage.java b/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/v2x/GenericV2xMessage.java index ed03aefd5..c7b6c22d1 100644 --- a/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/v2x/GenericV2xMessage.java +++ b/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/v2x/GenericV2xMessage.java @@ -23,7 +23,7 @@ /** * This {@link V2xMessage} implementation can be used for simple message exchange between entities. */ -public final class GenericV2xMessage extends V2xMessage { +public final class GenericV2xMessage extends V2xMessage implements DuplicatableMessage { private static final long serialVersionUID = 1L; @@ -82,4 +82,9 @@ public String toString() { + ", encodedPayload=" + payload + '}'; } + @Override + public GenericV2xMessage duplicate(MessageRouting routing) { + return new GenericV2xMessage(routing, messageType, payload.getEffectiveLength()); + } + }