
io.github.microcks.util.el.function.RandomIntELFunction Maven / Gradle / Ivy
/*
* Licensed to Laurent Broudoux (the "Author") under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Author licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.github.microcks.util.el.function;
import io.github.microcks.util.el.EvaluationContext;
import java.util.Random;
/**
* This is an implementation of ELFunction that generates random integer values.
* When invoked with no arg a integer between Integer.MIN_VALUE
and
* Integer.MAX_VALUE
is generated. When invoked with one argument, a
* positive integer bounded by arg value is generated. When invoked with 2 arguments,
* an integer in designated intervfal is generated.
* @author laurent
*/
public class RandomIntELFunction implements ELFunction {
@Override
public String evaluate(EvaluationContext evaluationContext, String... args) {
Random generator = new Random();
if (args != null) {
switch (args.length) {
case 1:
int maxValue = Integer.MAX_VALUE;
try {
maxValue = Integer.parseInt(args[0]);
} catch (NumberFormatException nfe) {
// Ignore, we'll stick to integer max value.
}
return String.valueOf(generator.nextInt(maxValue));
case 2:
int minValue = 0;
maxValue = Integer.MAX_VALUE;
try {
minValue = Integer.parseInt(args[0]);
maxValue = Integer.parseInt(args[1]);
} catch (NumberFormatException nfe) {
// Ignore, we'll stick to the defaults.
}
return String.valueOf(generator.nextInt(maxValue - minValue) + minValue);
default:
return String.valueOf(generator.nextInt());
}
}
return String.valueOf(generator.nextInt());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy