Created
December 18, 2014 23:02
-
-
Save kevinherron/b06151d59a7aad6bfb82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RandomFunction extends AbstractFunction { | |
private static final Random RANDOM = new Random(); | |
@Override | |
public QualifiedValue execute(Expression[] args) throws ExpressionException { | |
QualifiedValue qv = args[0].execute(); | |
int bound = TypeUtilities.toInteger(qv.getValue()); | |
synchronized (RANDOM) { | |
return new BasicQualifiedValue(RANDOM.nextInt(bound), qv.getQuality()); | |
} | |
} | |
@Override | |
public String getArgDocString() { | |
return "random"; | |
} | |
@Override | |
protected String getFunctionDisplayName() { | |
return "random"; | |
} | |
@Override | |
public Class<?> getType() { | |
return Integer.class; | |
} | |
@Override | |
protected boolean validateNumArgs(int num) { | |
return num == 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment