Skip to content

Commit

Permalink
Provide a method in VariantContextWriterBuilder to set an option depe…
Browse files Browse the repository at this point in the history
…nding on a Boolean.

This will allow chaining of the set/unset methods with conditionals.
  • Loading branch information
nh13 committed Apr 27, 2016
1 parent f6a7e09 commit 4ca1364
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,16 @@ public VariantContextWriterBuilder unsetOption(final Options option) {
return this;
}

/**
* Set or unset option depending on the boolean given
* @param option the option to modify
* @param setIt true to set the option, false to unset it.
* @return this <code>VariantContextWriterBuilder</code>
*/
public VariantContextWriterBuilder modifyOption(final Options option, final boolean setIt) {
return (setIt) ? this.setOption(option) : this.unsetOption(option);
}

/**
* Add one option to the set of default <code>Options</code> that will be used as the initial set of options
* for all VariantContextWriterBuilders created after this call.
Expand Down Expand Up @@ -369,6 +379,15 @@ public VariantContextWriterBuilder clearOptions() {
return this;
}

/**
* Used for testing; tests if the option is set
* @param option the option to test
* @return true if the option is set, false otherwise.
*/
boolean isOptionSet(final Options option) {
return this.options.contains(option);
}

/**
* Validate and build the <code>VariantContextWriter</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import htsjdk.tribble.Tribble;
import htsjdk.tribble.util.TabixUtils;
import htsjdk.variant.VariantBaseTest;
import htsjdk.variant.variantcontext.writer.Options;
import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -359,4 +360,18 @@ public void testClearOptions() {
final VariantContextWriterBuilder builder = new VariantContextWriterBuilder().clearOptions();
Assert.assertTrue(builder.options.isEmpty());
}

@Test
public void testModifyOption() {
final VariantContextWriterBuilder builder = new VariantContextWriterBuilder().clearOptions();
for (final Options option : Options.values()) {
Assert.assertFalse(builder.isOptionSet(option)); // shouldn't be set
builder.modifyOption(option, false);
Assert.assertFalse(builder.isOptionSet(option)); // still shouldn't be set
builder.modifyOption(option, true);
Assert.assertTrue(builder.isOptionSet(option)); // now is set
builder.modifyOption(option, false);
Assert.assertFalse(builder.isOptionSet(option)); // has been unset
}
}
}

0 comments on commit 4ca1364

Please sign in to comment.