-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract deep copy logic to util class
- Loading branch information
1 parent
a04e1b0
commit 270836b
Showing
4 changed files
with
56 additions
and
44 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
...rc/main/java/org/eclipse/digitaltwin/basyx/core/exceptions/FailedToDeepCopyException.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,12 @@ | ||
package org.eclipse.digitaltwin.basyx.core.exceptions; | ||
|
||
public class FailedToDeepCopyException extends RuntimeException { | ||
|
||
public FailedToDeepCopyException(String objId, Throwable e) { | ||
super(getMessage(objId), e); | ||
} | ||
|
||
private static String getMessage(String objId) { | ||
return "Failed to deep copy object with id " + objId; | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
...-core/src/main/java/org/eclipse/digitaltwin/basyx/serialization/SubmodelDeepCopyUtil.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,40 @@ | ||
package org.eclipse.digitaltwin.basyx.serialization; | ||
|
||
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.DeserializationException; | ||
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.SerializationException; | ||
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonDeserializer; | ||
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; | ||
import org.eclipse.digitaltwin.basyx.core.exceptions.FailedToDeepCopyException; | ||
|
||
public final class SubmodelDeepCopyUtil { | ||
|
||
private SubmodelDeepCopyUtil() { | ||
} | ||
|
||
/** | ||
* Deep copy a submodel. | ||
* | ||
* @param submodel | ||
* The submodel that should be copied. | ||
* @return A deep copy of the submodel. | ||
* | ||
* @throws FailedToDeepCopyException | ||
* If the submodel can not be copied. | ||
*/ | ||
public static Submodel deepCopy(Submodel submodel) { | ||
try { | ||
String submodelAsJSON = new JsonSerializer().write(submodel); | ||
|
||
Submodel submodelDeepCopy = new JsonDeserializer().read(submodelAsJSON, Submodel.class); | ||
|
||
submodelDeepCopy.setSubmodelElements(null); | ||
|
||
return submodelDeepCopy; | ||
|
||
} catch (DeserializationException | SerializationException e) { | ||
throw new FailedToDeepCopyException(submodel.getId(), e); | ||
} | ||
} | ||
|
||
} |
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