Coder Social home page Coder Social logo

trebuchet's Introduction

trebuchet

deprecated Build Status Coverage Status Codacy Badge codebeat badge Maintainability

code snippets for some problems when handling java.lang.Throwables in using Stream API .

Getting Start

Add the following snippet to any project's pom that depends on your project

<repositories>
  ...
  <repository>
    <id>savage-reflections</id>
    <url>https://raw.github.com/furplag/trebuchet/mvn-repo/</url>
    <snapshots>
      <enabled>true</enabled>
      <updatePolicy>always</updatePolicy>
    </snapshots>
  </repository>
</repositories>
...
<dependencies>
  ...
  <dependency>
    <groupId>jp.furplag.sandbox</groupId>
    <artifactId>trebuchet</artifactId>
    <version>3.0.1-FINAL</version>
  </dependency>
</dependencies>

Usage

public static void main(String[] args) {
  // @formatter:off

  // [1, 2, 3, 4, 5, 6, 7, 8, 9]
  Integer[] vars = IntStream.rangeClosed(1, 9).boxed().toArray(Integer[]::new);
  System.out.println(Arrays.toString(vars));

  // [0, 0, 0, 0, 0, 0, 0, 0, 0]
  System.out.println(Arrays.toString(
    Arrays.stream(vars).map((x) -> (x - 1) / x).toArray(Integer[]::new)
  ));

  try {
    // java.lang.ArithmeticException: zero divide ...
    System.out.println(Arrays.toString(
      Arrays.stream(vars).map((x) -> x / (x - 1)).toArray(Integer[]::new)
    ));
  } catch (Exception e) {/* so, what we have to do next ? */}

  // we decided to try to against Exception, the code was ugliness .
  // [null, 2, 1, 1, 1, 1, 1, 1, 1]
  System.out.println(Arrays.toString(
    IntStream.rangeClosed(1, 9).boxed().map((x) -> {
      try {
        return x / (x - 1);
      } catch (Exception e) {/* so, what we have to do next ? */}

      return null;
    }).toArray(Integer[]::new)
  ));
  // @formatter:on

  // we have to do like that which checking vars every time in those many,  many of code ?
  System.out.println(Arrays.toString(
    Arrays.stream(vars).map((x) -> x == 1 ? null : (x / (x - 1))).toArray(Integer[]::new)
  ));

  // no, not need. That's it.
  System.out.println(Arrays.toString(
      Arrays.stream(vars).map(ThrowableFunction.of((x) -> x / (x - 1), (x, ex) -> null)).toArray(Integer[]::new)
  ));
  System.out.println(Arrays.toString(
      Arrays.stream(vars).map((t) -> ThrowableFunction.orElse(t, (x) -> x / (x - 1), (x, ex) -> null)).toArray(Integer[]::new)
  ));
  System.out.println(Arrays.toString(
      Arrays.stream(vars).map((i) -> ThrowableFunction.orNull(i, (x) -> x / (x - 1))).toArray(Integer[]::new)
  ));
}

License

Code is under the Apache Licence v2.

trebuchet's People

Contributors

furplag avatar

Watchers

 avatar  avatar  avatar

trebuchet's Issues

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.