Skip to content

Commit 4919741

Browse files
authored
Copy properties when cloning a ModelObject (#110)
* Copy properties when cloning a ModelObject Fixes #107 Signed-off-by: Gary O'Neall <[email protected]>
1 parent aa2d7ca commit 4919741

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/main/java/org/spdx/library/model/ModelObject.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,20 @@ public boolean equals(Object o) {
10021002
* @return
10031003
*/
10041004
public ModelObject clone(IModelStore modelStore) {
1005+
if (Objects.isNull(this.copyManager)) {
1006+
throw new IllegalStateException("A copy manager must be provided to clone");
1007+
}
1008+
if (this.modelStore.equals(modelStore)) {
1009+
throw new IllegalStateException("Can not clone to the same model store");
1010+
}
1011+
Objects.requireNonNull(modelStore, "Model store for clone must not be null");
1012+
if (modelStore.exists(this.documentUri, this.id)) {
1013+
throw new IllegalStateException("Can not clone - "+this.id+" already exists.");
1014+
}
10051015
try {
1006-
return SpdxModelFactory.createModelObject(modelStore, this.documentUri, this.id, this.getType(), this.copyManager);
1016+
ModelObject retval = SpdxModelFactory.createModelObject(modelStore, this.documentUri, this.id, this.getType(), this.copyManager);
1017+
retval.copyFrom(this);
1018+
return retval;
10071019
} catch (InvalidSPDXAnalysisException e) {
10081020
throw new RuntimeException(e);
10091021
}

src/test/java/org/spdx/library/model/ModelObjectTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,8 @@ public void testClone() throws InvalidSPDXAnalysisException {
715715
ModelObject result = gmo.clone(store2);
716716
assertTrue(result instanceof GenericModelObject);
717717
assertEquals(result, gmo);
718+
assertTrue(result.equivalent(gmo));
719+
assertTrue(gmo.equivalent(result));
718720
}
719721

720722
/**

0 commit comments

Comments
 (0)