JAL-4131 Replace usages of requireNonNullElse
[jalview.git] / src / jalview / ws / params / simple / BooleanOption.java
index b69da25..87e4ad1 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -22,19 +22,80 @@ package jalview.ws.params.simple;
 
 import java.net.URL;
 import java.util.Arrays;
+import java.util.List;
 
-import jalview.ws.params.OptionI;
-
-public class BooleanOption extends Option implements OptionI
+public class BooleanOption extends Option
 {
+  public static class Builder extends Option.Builder
+  {
+    private boolean defaultValue = false;
+
+    private boolean value = false;
+
+    private String reprValue = null;
+
+    public void setDefaultValue(Boolean defaultValue)
+    {
+      this.defaultValue = defaultValue != null ? defaultValue : false;
+    }
+
+    public void setValue(Boolean value)
+    {
+      this.value = value != null ? value : false;
+    }
+
+    public void setReprValue(String reprValue)
+    {
+      this.reprValue = reprValue;
+    }
+
+    @Override
+    public void setPossibleValues(List<String> possibleValues)
+    {
+      throw new UnsupportedOperationException("cannot set possible values for boolean");
+    }
+
+    @Override
+    public BooleanOption build()
+    {
+      return new BooleanOption(this);
+    }
+  }
+
+  public static Builder newBuilder()
+  {
+    return new Builder();
+  }
+
+  protected BooleanOption(Builder builder)
+  {
+    super(builder);
+    String reprValue = builder.reprValue != null ? builder.reprValue : name;
+    defvalue = builder.defaultValue ? reprValue : null;
+    value = builder.value ? reprValue : null;
+    possibleVals = List.of(reprValue);
+    displayVals = List.of(label);
+  }
 
   public BooleanOption(String name, String descr, boolean required,
-          boolean defVal, boolean val, URL link)
+      Boolean defVal, Boolean val, URL link)
   {
+    super(name, descr, required, (defVal != null && defVal ? name : null),
+        (val != null && val ? name : null), Arrays.asList(name), link);
+  }
 
-    super(name, descr, required, (defVal ? name : ""), (val ? name : ""),
-            Arrays.asList(new String[]
-            { name }), link);
+  public BooleanOption(String name, String description, String label,
+      boolean isrequired, Boolean defValue, String reprValue, URL link)
+  {
+    super(name, description, label, isrequired,
+        defValue != null && defValue ? reprValue : null,
+        defValue != null && defValue ? reprValue : null,
+        Arrays.asList(reprValue), link);
   }
 
+  public BooleanOption(String name, String description, String label,
+      boolean isrequired, Boolean defValue, URL link)
+  {
+    this(name, description, label, isrequired, defValue, String.valueOf(true), link);
+  }
 }