Skip to content

Commit

Permalink
Add NUMBER support (#1745)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre601 committed Aug 18, 2021
1 parent 84f2cc3 commit 5b1d26e
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,9 @@ public int hashCode()
public static class Choice
{
private final String name;
private final long intValue;
private final String stringValue;
private long intValue = 0;
private double doubleValue = Double.NaN;
private String stringValue = null;

/**
* Create a Choice tuple
Expand All @@ -361,8 +362,21 @@ public static class Choice
public Choice(@Nonnull String name, long value)
{
this.name = name;
this.intValue = value;
this.stringValue = Long.toString(value);
setIntValue(value);
}

/**
* Create a Choice tuple
*
* @param name
* The display name of this choice
* @param value
* The double value you receive in a command option
*/
public Choice(@Nonnull String name, double value)
{
this.name = name;
setDoubleValue(value);
}

/**
Expand All @@ -376,8 +390,7 @@ public Choice(@Nonnull String name, long value)
public Choice(@Nonnull String name, @Nonnull String value)
{
this.name = name;
this.intValue = 0;
this.stringValue = value;
setStringValue(value);
}

/**
Expand All @@ -397,13 +410,15 @@ public Choice(@Nonnull DataObject json)
this.name = json.getString("name");
if (json.isType("value", DataType.INT))
{
this.intValue = json.getLong("value");
this.stringValue = Long.toString(intValue); // does this make sense?
setIntValue(json.getLong("value"));
}
else if (json.isType("value", DataType.FLOAT))
{
setDoubleValue(json.getDouble("value"));
}
else
{
this.intValue = 0;
this.stringValue = json.getString("value");
setStringValue(json.getString("value"));
}
}

Expand All @@ -419,6 +434,16 @@ public String getName()
return name;
}

/**
* The value of this choice.
*
* @return The double value, or NaN if this is not a numeric choice value
*/
public double getAsDouble()
{
return doubleValue;
}

/**
* The value of this choice.
*
Expand Down Expand Up @@ -460,6 +485,27 @@ public String toString()
{
return "Choice(" + name + "," + stringValue + ")";
}

private void setIntValue(long value)
{
this.doubleValue = value;
this.intValue = value;
this.stringValue = Long.toString(value);
}

private void setDoubleValue(double value)
{
this.doubleValue = value;
this.intValue = (long) value;
this.stringValue = Double.toString(value);
}

private void setStringValue(@Nonnull String value)
{
this.doubleValue = Double.NaN;
this.intValue = 0;
this.stringValue = value;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ public long getAsLong()
}
}

/**
* The double value for this option.
*
* @throws IllegalStateException
* If this option {@link #getType() type} cannot be converted to a double
* @throws NumberFormatException
* If this option is of type {@link OptionType#STRING STRING} and could not be parsed to a valid double value
*
* @return The double value
*/
public double getAsDouble()
{
switch (type)
{
default:
throw new IllegalStateException("Cannot convert option of type " + type + " to double");
case STRING:
case INTEGER:
case NUMBER:
return data.getDouble("value");
}
}

/**
* The resolved {@link IMentionable} instance for this option value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public enum OptionType
CHANNEL(7),
ROLE(8),
MENTIONABLE(9),
NUMBER(10, true),
;

private final int raw;
Expand Down
Loading

0 comments on commit 5b1d26e

Please sign in to comment.