Could be in Optional really anything

I found implemented this code (method where this part of code is used is returning Optional<File>, this part of code had to be added for verification if values are correct and case can be saved): if (!CaseChecker.checkValues(case)) { return Optional.of(new File("FALSE")); } When I asked the person, why he is returning something like this. The… Read More Could be in Optional really anything

What am i doing wrong in this Java optional – orElse statement

List<String> list = Arrays.asList("TEST1", "TEST2", "TEST3"); final String finalStr = Optional.ofNullable(list.stream() .filter(s -> s.contains("pattern:a:b:")) .map(str -> str.substring(str.lastIndexOf(‘:’) + 1)) .findFirst()) .orElse(list.stream() .filter(s -> !s.contains("pattern:c:d:")) .findFirst()) .orElseThrow(); It will work if the list contains pattern:a:b: but for above example always throw an exception if that pattern is not available. In the orElse part it doesnt even… Read More What am i doing wrong in this Java optional – orElse statement

Optional counter behaviour

I’am currently studying for OCP and I’m simply asking why the first optional does not increment "counter" : Integer counter = 1; Optional<Integer> optional = (Optional<Integer>) Optional.empty().orElse( Optional.of(counter +1)); System.out.println(counter); //print 1 AtomicInteger counter2 = new AtomicInteger(1); Optional<Integer> option = (Optional<Integer>) Optional.empty().orElse( Optional.of(counter2.getAndIncrement())); System.out.println(counter2); // print 2 >Solution : Because expression counter + 1 does… Read More Optional counter behaviour

Nested Optional.get generates warning when checked and chained in orElse()

I’ve just stumbled upon a warning generated by IntelliJ and I’m wondering, do I miss something or is IntelliJ just ignoring the right side of the following or clause? Example Code: Random random = new Random(); public void test(){ Optional<String> a = Optional.ofNullable(random.nextInt(10)>5?"something":null); Optional<String> b = Optional.ofNullable(random.nextInt(10)>5?"something":null); if(a.isPresent() || b.isPresent()){ log.info(a.orElse(b.get())); } } The warning… Read More Nested Optional.get generates warning when checked and chained in orElse()

How to pass specified parameters in Javascript

I would like to pass value to specified parameters and use default value by optional parameters for the rest. A sample is made as below. Current result is ‘b23’. But I would like to obtain the result of ‘1b3’. function runThis() { test(b=’b’); } function test(a=’1′,b=’2′,c=’3′){ console.println(a+b+c); } I also try to run test({b:’b’}) and… Read More How to pass specified parameters in Javascript

cannot cast to jsonb if non-negative numeric vlaue have plus sign

the following command working select ‘[0,1, -2, -0.3444, 5.6]’::jsonb; However the following 3 not working. select ‘[0,1, -2, (+0.3444), 5.6]’::jsonb; select ‘[0,1, -2, +0.3444, 5.6]’::jsonb; select ‘[0,1, -2, +0, 5.6]’::jsonb; The following working. select +0.1; select (+0.1)::text; >Solution : The first working example is a string containing a valid JSON document being cast as JSONB;… Read More cannot cast to jsonb if non-negative numeric vlaue have plus sign

Regex to not match more than one trailing slash in string

Looking for a regex to not match more than 1 occurrence of a trailing slash api/v1 /api/v1 /api/2v1/21/ /api/blah/v1/ /api/ether/v1// /api/23v1/// Expected match /api/v1 /api/2v1/21/ /api/blah/v1/ What I tried: ^\/([^?&#\s]*)(^[\/{2,}\s])$ >Solution : In the pattern that you tried, the second part of the pattern can not match, it asserts the start of the string ^… Read More Regex to not match more than one trailing slash in string