Is there any possible way to setup constant compile time value using a runtime call method? In "Spring in Action" book, I got this piece of code:
private static final String MAX_LONG_AS_STRING = Long.toString(Long.MAX_VALUE);
@RequestMapping(method = RequestMethod.GET)
public List<Spittle> spittles(
@RequestParam(value = "max", defaultValue = MAX_LONG_AS_STRING) long max,
@RequestParam(value = "count", defaultValue = "20") int count) {
return spittleRepository.findSpittles(max, count);
}
the problem is with MAX_LONG_AS_STRING, because defaultValue param needs to be a String constant but MAX_LONG_AS_STRING is not a constant compile time variable, is there any possible way to get Long max value as a constant String value? Maybe there is something which can help me to call toString method during compile time, or retrieve this value in any other way ?
From Oracle: long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).
The Long. MAX_VALUE is the static variable that contains a constant value in the Java wrapper Long class, and 9,223,372,036,854,775,807 is considered a maximum value of a long variable.
long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.
static long MAX_VALUE − This is a constant holding the maximum value a long can have, 263-1. static long MIN_VALUE − This is a constant holding the minimum value a long can have, -263. static int SIZE − This is the number of bits used to represent a long value in two's complement binary form.
You can achieve this as shown in the below steps:
(1) Get the Max value first long MAXVALUE = Long.MAX_VALUE;
(2) Set the @RequestParam
value as defaultValue = MAXVALUE+""
(converts long to string)
Complete Code:
private static final long MAXVALUE = Long.MAX_VALUE;//Get the long value first
@RequestMapping(method = RequestMethod.GET)
public void spittles(
@RequestParam(value = "max", defaultValue = MAXVALUE+"") long max,
@RequestParam(value = "count", defaultValue = "20") int count) {
// return spittleRepository.findSpittles(max, count);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With