/*
* Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
* Copyright (C) $$Year-Rel$$ The Jalview Authors
*
* This file is part of Jalview.
*
* Jalview is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Jalview is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jalview. If not, see .
* The Jalview Authors are detailed in the 'AUTHORS' file.
*/
package jalview.ws.params.simple;
import jalview.ws.params.OptionI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNullElse;
public class Option implements OptionI
{
/**
* A builder class which avoids multiple telescoping parameters nightmare.
*
* @author mmwarowny
*
*/
public static class Builder
{
protected String name = null;
protected String label = null;
protected String description = "";
protected boolean required = false;
protected String defaultValue = null;
protected String value = null;
protected List possibleValues = null;
protected List displayValues = null;
protected URL detailsUrl = null;
public void setName(String name)
{
this.name = name;
}
public void setLabel(String label)
{
this.label = label;
}
public void setDescription(String description)
{
this.description = description;
}
public void setRequired(boolean required)
{
this.required = required;
}
public void setDefaultValue(String defaultValue)
{
this.defaultValue = defaultValue;
}
public void setValue(String value)
{
this.value = value;
}
public void setPossibleValues(List possibleValues)
{
this.possibleValues = possibleValues;
}
public void setDisplayValues(List displayValues)
{
this.displayValues = displayValues;
}
public void setDetailsUrl(URL detailsUrl)
{
this.detailsUrl = detailsUrl;
}
public Option build()
{
return new Option(this);
}
}
public static Builder newBuilder()
{
return new Builder();
}
String name;
String label;
/*
* current value in string format, or "null" if undefined
*/
String value;
/*
* default value in string format, or "null" if undefined
*/
String defvalue;
String description;
List possibleVals;
/*
* optional display names corresponding to possibleVals
*/
List displayVals;
boolean required;
URL fdetails;
protected Option(Builder builder)
{
requireNonNull(builder.name);
name = builder.name;
label = requireNonNullElse(builder.label, name);
description = builder.description;
required = builder.required;
defvalue = builder.defaultValue;
value = builder.value;
if (builder.possibleValues != null)
possibleVals = new ArrayList<>(builder.possibleValues);
if (builder.displayValues != null)
displayVals = new ArrayList<>(builder.displayValues);
else
displayVals = possibleVals;
if (possibleVals == null && displayVals != null)
throw new IllegalArgumentException(
"cannot use displayValues if possibleValues is null");
if (possibleVals != null && possibleVals.size() != displayVals.size())
throw new IllegalArgumentException(
"displayValues size does not match possibleValues");
fdetails = builder.detailsUrl;
}
/**
* Copy constructor
*
* @param opt
*/
public Option(Option opt)
{
name = opt.name;
label = opt.label;
value = opt.value;
defvalue = opt.defvalue;
description = opt.description;
if (opt.possibleVals != null)
{
possibleVals = new ArrayList<>(opt.possibleVals);
}
required = opt.required;
// URLs are singletons - so we copy by reference. nasty but true.
fdetails = opt.fdetails;
}
public Option()
{
}
public Option(String name, String description, String label, boolean isrequired,
String defValue, String val, List possibleVals, URL fdetails)
{
this(name, description, isrequired, defValue, val, possibleVals, fdetails);
this.label = label;
}
/**
* Constructor including display names for possible values
*
* @param name2
* @param description2
* @param isrequired
* @param defValue
* @param val
* @param possibleVals
* @param fdetails
*/
public Option(String name2, String description2, boolean isrequired,
String defValue, String val, List possibleVals,
List displayNames, URL fdetails)
{
name = name2;
description = description2;
this.value = val;
this.required = isrequired;
this.defvalue = defValue;
if (possibleVals != null)
{
this.possibleVals = new ArrayList<>(possibleVals);
}
if (displayNames != null)
{
this.displayVals = new ArrayList<>(displayNames);
}
this.fdetails = fdetails;
}
/**
* Constructor
*
* @param name2
* @param description2
* @param isrequired
* @param defValue
* @param val
* @param possibleVals
* @param fdetails
*/
public Option(String name2, String description2, boolean isrequired,
String defValue, String val, List possibleVals,
URL fdetails)
{
this(name2, description2, isrequired, defValue, val, possibleVals, null,
fdetails);
}
@Override
public OptionI copy()
{
Option opt = new Option(this);
return opt;
}
/**
* toString method to help identify options in the debugger only
*/
@Override
public String toString()
{
return this.getClass().getName() + ":" + name;
}
@Override
public String getName()
{
return name;
}
@Override
public String getLabel()
{
return label != null ? label : name;
}
@Override
public String getValue()
{
return value == null ? defvalue : value;
}
@Override
public void setValue(String selectedItem)
{
value = selectedItem;
}
@Override
public URL getFurtherDetails()
{
return fdetails;
}
@Override
public boolean isRequired()
{
return required;
}
@Override
public String getDescription()
{
return description;
}
@Override
public List getPossibleValues()
{
return possibleVals;
}
@Override
public List getDisplayNames()
{
return displayVals;
}
}