Giter Site home page Giter Site logo

expressionj's Introduction

ExpressionJ

Maven Central

Java CI with Gradle codecov

sonar-reliability-rating sonar-security-rating sonar-sqale-rating

Java library for supporting the following expressions:

  • If expression

Java versions:

  • 8 or later

If expression

"FizzBuzz" can be implemented as following:

import static jp.gr.java_conf.spica.expressionj.Expressions.ifExp;

/*..snip..*/

String fizzBuzz = ifExp(i % 15 == 0).then(() -> "FizzBuzz")
    .elseIf(() -> i % 3 == 0).then(() -> "Fizz")
    .elseIf(() -> i % 5 == 0).then(() -> "Buzz")
    .elseExp(() -> String.valueOf(i));

Switch expression

If you use Java14 or later, you can use the official switch expression .

import static jp.gr.java_conf.spica.expressionj.Expressions.switchExp;
import static jp.gr.java_conf.spica.expressionj.switchexpression.Cases.caseEq;
import static jp.gr.java_conf.spica.expressionj.switchexpression.Cases.caseIn;

import jp.gr.java_conf.spica.expressionj.switchexpression.Cases;

/*..snip..*/

int length = switchExp(day).cases(
    caseIn(MONDAY, FRIDAY, SUNDAY).yield(() -> 6),
    caseEq(TUESDAY).yield(() -> 7),
    caseIn(THURSDAY, SATURDAY).yield(() -> 8),
    caseIn(WEDNESDAY).yield(() -> 9),
    Cases.<Day>caseDefault().yield(() -> -1));
import static jp.gr.java_conf.spica.expressionj.Expressions.switchExp;

import jp.gr.java_conf.spica.expressionj.switchexpression.Cases;

/*..snip..*/

double perimeter = switchExp(shape).cases(
    Cases.<Shape, Rectangle>caseInstanceOf(Rectangle.class)
        .yield(r -> r.length * 2 + r.width * 2),
    Cases.<Shape, Circle>caseInstanceOf(Circle.class)
        .yield(r -> 2 * r.radius * Math.PI));

Sample code

See also the sample code

Performance

If expression

If expression is a little slower than If statement, but it's enough fast when number of calls is not huge (e.g. over 10000000(10^7)).

Here is a result of the benchmark in the CI environment.

The benchmark just performs solve FizzBuzz and sum up of the length of the result.

  • java.vm.vendor:AdoptOpenJDK
  • java.vm.version:11.0.11+9
  • os.name:Linux
  • os.version:5.13.0-1023-azure

Benchmark of FizzBuzz.

Loop count is 10000000.

Elapsed Time in ms:

Count If statement with method If Expression
1 252 880
2 266 585
3 270 582
4 270 581
5 265 594
6 269 580

Switch expression

Switch expression is slower than Switch statement, and might be slower than official switch expression. When the number of calls is little huge (e.g. over 1000000(10^6)), please use switch statement or official switch expression.

Here is a result of the benchmark in the CI environment.

  • java.vm.vendor:AdoptOpenJDK
  • java.vm.version:11.0.11+9
  • os.name:Linux
  • os.version:5.13.0-1023-azure

Benchmark of length count of days. Loop count is 125000. Elapsed Time in ms:

Count Switch statement with method Switch Expression
1 95 747
2 51 525
3 51 481
4 28 477
5 20 478
6 20 476

Changelog

See CHANGELOG.md

expressionj's People

Contributors

sakata1222 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.