Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate IBaseDataObjectHelper.clone() that allows partial clones. #1018

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/emissary/core/IBaseDataObjectHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,27 @@ private InternalIdBaseDataObject(final UUID internalId) {

private IBaseDataObjectHelper() {}

/**
* Clones an IBaseDataObject.
*
* @param iBaseDataObject the IBaseDataObject to be cloned.
* @return the clone of the IBaseDataObject passed in.
*/
public static IBaseDataObject clone(final IBaseDataObject iBaseDataObject) {
return clone(iBaseDataObject, true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan having the new overload reference the very method we're deprecating. Maybe we should refactor the existing one by extracting two supporting methods that both overloads can use. Something like the following signatures:

private static copyBasicFields(final IBaseDataObject src, final IBaseDataObject dest) {
    // body would be current lines 66-87 
}
private static copyNonBasicFields(final IBaseDataObject src, final IBaseDataObject dest) {
    // body would be current lines 90-106
}

With that change, this overload would strip down to the following:

 public static IBaseDataObject clone(final IBaseDataObject src) {
    Validate.notNull(src, "Required: src not null");
    IBaseDataObject dst = new InternalIdBaseDataObject(src.getInternalId());
    copyBasicFields(src, dst);
    copyNonBasicFields(src, dst);
    retrun dst;
}

The original now-deprecated overload would become this:

 public static IBaseDataObject clone(final IBaseDataObject src, final boolean fullClone) {
    Validate.notNull(src, "Required: src not null");
    IBaseDataObject dst = fullClone ? new InternalIdBaseDataObject(src.getInternalId()) : DataObjectFactory.getInstance();
    copyBasicFields(src, dst);
    if (fullClone) {
        copyNonBasicFields(src, dst);
    }
    retrun dst;
}

After the deprecated overload is removed, we can collapse those private methods into the remaining clone method

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that having the clone(IBDO) method temporarily call the deprecated clone(IBDO,boolean) method is not ideal; however, when the clone(IBDO,boolean) method is deleted, then the clone(IBDO) method could be what you have above and just call the single copy() method from PR #1009. Additionally, implementing the two private copy methods you have above would run into the same problems encountered in PR #1009.

}

/**
* Clones an IBaseDataObject equivalently to emissary.core.BaseDataObject.clone(), which duplicates some attributes.
*
* A "fullClone" duplicates all attributes.
*
* @deprecated prefer {@link #clone(IBaseDataObject)}
* @param iBaseDataObject the IBaseDataObject to be cloned.
* @param fullClone specifies if all fields should be cloned.
* @return the clone of the IBaseDataObject passed in.
*/
@Deprecated(forRemoval = true)
public static IBaseDataObject clone(final IBaseDataObject iBaseDataObject, final boolean fullClone) {
Validate.notNull(iBaseDataObject, "Required: iBaseDataObject not null");

Expand Down
13 changes: 13 additions & 0 deletions src/test/java/emissary/core/IBaseDataObjectHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ private static void verifyCloneAssertions(final Method method, final Object obj1

}

@Test
void testOneArgClone() {
final IBaseDataObject originalIbdo = new BaseDataObject(new byte[123], "Filename", "form", "filetype");
final IBaseDataObject clonedIbdo = IBaseDataObjectHelper.clone(originalIbdo);
final List<String> differences = new ArrayList<>();
final DiffCheckConfiguration diffCheckConfiguration =
DiffCheckConfiguration.configure().enableData().enableInternalId().enableTimestamp().enableTransformHistory().build();

IBaseDataObjectDiffHelper.diff(originalIbdo, clonedIbdo, differences, diffCheckConfiguration);

assertEquals(0, differences.size());
}

@Test
void testCloneArguments() {
assertNotNull(IBaseDataObjectHelper.clone(new BaseDataObject(), false));
Expand Down
Loading