GithubHelp home page GithubHelp logo

bgerstle / result-java Goto Github PK

View Code? Open in Web Editor NEW
19.0 3.0 7.0 73 KB

A simple Result type in Java for functional exception handling.

License: MIT License

Java 100.00%
java java-exceptions java-streams microlibrary

result-java's People

Contributors

bgerstle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

result-java's Issues

ResultCollector not compliant with parallelStream requirements

hi,

I'm trying to learn java and i found your article very interesting and useful, able to solve in a clear and simple way what seems to me a deficiency in the current implementation of java.

By trying to use the library I noticed that the characteristics of ResultCollector were not specified.

public Set<Characteristics> characteristics() { return Set.of(); }

So I tried to verify its behavior with a parallel stream and I noticed a wrong result.

  @Test
  void collectorSyncronization() {
    Supplier<Integer> fn = () -> new Random().nextInt(10);
 
    // ok this is std collector
    assertDoesNotThrow(() -> Stream.generate(fn).parallel().limit(100).collect(Collectors.toList()));

    // trying the same with a CheckedSupplier and a Result
    Supplier<Result<Integer,Exception>> randomNumbersSupp=() -> Result.attempt(() -> fn.get());
    Stream<Result<Integer,Exception>> streamOfValues = Stream.generate(randomNumbersSupp)
                                                             .limit(100);

    // with a serial stream all is ok
    List<Integer> values = assertDoesNotThrow( () ->
                                streamOfValues
                                      .collect(new ResultCollector<>())
                                      .getValue().get()
                      );

    // with a parallelStream something go wrong  
    assertThrows( Exception.class, () ->
                          streamOfValues
                                      .parallel()
                                      .collect(new ResultCollector<>())
                                      .getValue().get()
                      );

  }

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.