mirror of
https://github.com/octoleo/plantuml.git
synced 2024-11-28 15:56:31 +00:00
Update and rename Random.java to RandomFunction.java
Feat: improve to accept 0, 1, 2 args
This commit is contained in:
parent
dbaf8ac2cd
commit
9e18d34936
@ -36,6 +36,7 @@ package net.sourceforge.plantuml.tim.stdlib;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import net.sourceforge.plantuml.tim.EaterException;
|
import net.sourceforge.plantuml.tim.EaterException;
|
||||||
@ -46,22 +47,35 @@ import net.sourceforge.plantuml.tim.TMemory;
|
|||||||
import net.sourceforge.plantuml.tim.expression.TValue;
|
import net.sourceforge.plantuml.tim.expression.TValue;
|
||||||
import net.sourceforge.plantuml.utils.LineLocation;
|
import net.sourceforge.plantuml.utils.LineLocation;
|
||||||
|
|
||||||
public class Random extends SimpleReturnFunction {
|
public class RandomFunction extends SimpleReturnFunction {
|
||||||
|
|
||||||
public TFunctionSignature getSignature() {
|
public TFunctionSignature getSignature() {
|
||||||
return new TFunctionSignature("%random", 1);
|
return new TFunctionSignature("%random", 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean canCover(int nbArg, Set<String> namedArgument) {
|
public boolean canCover(int nbArg, Set<String> namedArgument) {
|
||||||
return nbArg == 1;
|
return nbArg == 0 || nbArg == 1 || nbArg == 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final Random random = new Random();
|
||||||
|
|
||||||
public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List<TValue> values,
|
public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List<TValue> values,
|
||||||
Map<String, TValue> named) throws EaterException, EaterExceptionLocated {
|
Map<String, TValue> named) throws EaterException, EaterExceptionLocated {
|
||||||
if (values.get(0).isNumber()) {
|
switch (values.size()) {
|
||||||
final int limit = values.get(0).toInt();
|
case 0:
|
||||||
return TValue.fromInt((int) (Math.random() * limit));
|
return TValue.fromInt(random.nextInt(2));
|
||||||
}
|
|
||||||
return TValue.fromInt(0);
|
case 1:
|
||||||
|
final Integer mx = values.get(0).toInt();
|
||||||
|
return TValue.fromInt(random.nextInt(mx));
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
final Integer min = values.get(0).toInt();
|
||||||
|
final Integer max = values.get(1).toInt();
|
||||||
|
return TValue.fromInt(random.nextInt(max - min) + min);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw EaterException.located("Error on Random: Too many argument");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user