Skip to content

Commit

Permalink
Stop misuse of the API and ensure only one flag is set at once. This …
Browse files Browse the repository at this point in the history
…is less powerful but stops users setting custom flags (e.g. 999 in Asad's ReactionDecoder) which has undefined behaviour.
  • Loading branch information
johnmay committed Nov 9, 2016
1 parent 7897397 commit b5ff6c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion base/data/src/main/java/org/openscience/cdk/ChemObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,17 @@ public void setID(String identifier) {
notifyChanged();
}

private boolean isPowerOfTwo(int num) {
return (num == 1) || (num & (num-1)) == 0;
}

/**
* @inheritDoc
* {@inheritDoc}
*/
@Override
public void setFlag(int mask, boolean value) {
if (mask > Short.MAX_VALUE || !isPowerOfTwo(mask))
throw new IllegalArgumentException("setFlag() must be provided a valid CDKConstant and not used for custom properties");
// set/unset a bit in the flags value
if (value)
flags |= mask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ public interface IChemObject extends ICDKObject {
/**
* Sets the value of some flag. The flag is a mask from a given
* CDKConstant (e.g. {@link org.openscience.cdk.CDKConstants#ISAROMATIC}
* or {@link org.openscience.cdk.CDKConstants#VISITED}).
* or {@link org.openscience.cdk.CDKConstants#VISITED}). The flags are
* intrinsic internal properties and should not be used to store custom
* values, please use {@link #setProperty(Object, Object)}.
*
* <pre>{@code
* // set this chem object to be aromatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,17 @@ public void setID(String identifier) {
this.identifier = identifier;
}

private boolean isPowerOfTwo(int num) {
return (num == 1) || (num & (num-1)) == 0;
}

/**
* @inheritDoc
*/
@Override
public void setFlag(int mask, boolean value) {
if (mask > Short.MAX_VALUE || !isPowerOfTwo(mask))
throw new IllegalArgumentException("setFlag() must be provided a valid CDKConstant and not used for custom properties");
// set/unset a bit in the flags value
if (value)
flags |= mask;
Expand Down

0 comments on commit b5ff6c4

Please sign in to comment.