Skip to content

Commit

Permalink
Persist the sequence number if it changes to the document
Browse files Browse the repository at this point in the history
Currently the targetdefinition is maintaining a sequence number that
should roughly reflect the number of times a relevant property of the
target file changes. Currently this number is never written out to the
XML making it only effective at runtime but not e.g. when committed to a
VCS if not maintained manually.

This now makes sure that the number is correctly persisted in the XML.
  • Loading branch information
laeubi committed Dec 31, 2024
1 parent 6e21922 commit 5dbc553
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class TargetDefinition implements ITargetDefinition {
private TargetFeature[] fFeatures;
private TargetBundle[] fOtherBundles;

private int fSequenceNumber = -1;
private int fSequenceNumber;

/**
* Constructs a target definition based on the given handle.
Expand Down Expand Up @@ -1123,7 +1123,8 @@ public int getSequenceNumber() {
* @return the current sequence number after it has been increased
*/
public int incrementSequenceNumber() {
return ++fSequenceNumber;
setSequenceNumber(getSequenceNumber() + 1);
return getSequenceNumber();
}

/**
Expand All @@ -1132,7 +1133,16 @@ public int incrementSequenceNumber() {
* @param value value to set the sequence number to
*/
void setSequenceNumber(int value) {
fSequenceNumber = value;
if (value != fSequenceNumber) {
fSequenceNumber = value;
if (fRoot != null) {
if (value > 0) {
fRoot.setAttribute(TargetDefinitionPersistenceHelper.ATTR_SEQUENCE_NUMBER, Integer.toString(value));
} else {
fRoot.removeAttribute(TargetDefinitionPersistenceHelper.ATTR_SEQUENCE_NUMBER);
}
}
}
}

private void removeElement(String... childNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,15 @@ public static void initFromDoc(ITargetDefinition definition, Element root) throw
}
}
}

// Set the sequence number at the very end
String sequenceNumber = root.getAttribute(TargetDefinitionPersistenceHelper.ATTR_SEQUENCE_NUMBER);
try {
((TargetDefinition) definition).setSequenceNumber(Integer.parseInt(sequenceNumber));
} catch (NumberFormatException e) {
((TargetDefinition) definition).setSequenceNumber(0);
if (definition instanceof TargetDefinition impl) {
// Set the sequence number at the very end
String sequenceNumber = root.getAttribute(TargetDefinitionPersistenceHelper.ATTR_SEQUENCE_NUMBER);
try {
if (!sequenceNumber.isBlank()) {
impl.setSequenceNumber(Integer.parseInt(sequenceNumber));
}
} catch (NumberFormatException e) {
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,15 @@ public static void initFromDoc(ITargetDefinition definition, Element root) {
}
}
}

// Set the sequence number at the very end
String sequenceNumber = root.getAttribute(TargetDefinitionPersistenceHelper.ATTR_SEQUENCE_NUMBER);
try {
((TargetDefinition) definition).setSequenceNumber(Integer.parseInt(sequenceNumber));
} catch (NumberFormatException e) {
((TargetDefinition) definition).setSequenceNumber(0);
if (definition instanceof TargetDefinition impl) {
// Set the sequence number at the very end
String sequenceNumber = root.getAttribute(TargetDefinitionPersistenceHelper.ATTR_SEQUENCE_NUMBER);
try {
if (!sequenceNumber.isBlank()) {
impl.setSequenceNumber(Integer.parseInt(sequenceNumber));
}
} catch (NumberFormatException e) {
}
}
}

Expand Down

0 comments on commit 5dbc553

Please sign in to comment.