Showing posts with label objectlabKit. Show all posts
Showing posts with label objectlabKit. Show all posts

Sunday, 29 July 2018

A Human Readable Predicate with ObjectLab Kit

Recently, I have been working with Predicates.

I'm modelling some stuff for a bank but I needed to present updates and changes to the handwritten rules regularly. I don't know about you but wouldn't be nice if we could print the Predicate and not getting something like:

net.objectlab.kit.util.excel.ExcelWorkbookTest$$Lambda$1/868693306@13221655

Therefore, here comes the PrintablePredicate class in the next release of ObjectLab Kit.

See PrintablePredicate.java

The idea is to create an implementation of a Predicate and allow you to give it a name and the values it is comparing against.  We also support AND, OR and NEGATE as per a Predicate.

Let's imagine a small model, a financial Instrument 'Asset' and some basic Predicates.  Imagine that we create a predicate that detects instruments that are either Bonds or Commodities but that they should also be Active.

Using PrintablePredicate, I can combine 2 predicates and when I print the predicate (in an Excel spreadsheet that I generate automatically, more on that later) I can see a nice string "AssetClass in (Bond, Commodities) AND Active".  Here is the code for it:



This tiny class will come with ObjectLab Kit 1.4.1. Enjoy!

Monday, 26 May 2014

How to efficiently add BigDecimals

Anyone who deals with monetary values knows that double/float won't cut the mustard and if you deal with prices and FX rates, then BigDecimal is the only real option.

This comes with a lot of potential issues, BigDecimal methods do not handle null very well (i.e. not at all) and sometimes a bug crops up because BigDecimal returns new instances.


So the ObjectLabKit Util package will help, but here is a question for you... what is an efficient way to sum a list of BigDecimal coming from a Class.

Assume that we have a list of 500 Test instances and that we need to sum the Test.value and that value could be null.

We shall run the test 1,000 times.


Option 1: Use Total in a for loop

Option 2: Use Total with java8 forEach

Option 3: Use Total and java8 map()

Option 4: Use Java8 map and reduce

Option 5: Use Java8 map, reduce and accumulator

Option 6: Use Java8 and home-made Collector

Option 7: Use Java8 and ObjectLabKit Calculator

Option 8: Use Java8 and Parallel Stream



So what are the results?

On my 2012 MacBook Pro for a list of 500 Test instances.
AlgoAverage (ms)Min (ms)Max (ms)
Use Total in a for loop0.104
Use Total with java8 forEach0.1040
Use Total and java8 map()0.106
Use Java8 map and reduce002
Use Java8 map, reduce and accumulator002
Use Java8 and home-made Collector0.106
Use Java8 and ObjectLabKit Calculator002
Use Java8 and Parallel Stream0.1010


First of all, the value generated is the same for every algo, so no bug there it seems.

The results are quite similar except for the Max value, implying a greater deviation in the results. I've used JAmon for measuring min/max and average time.

Surprisingly, it seems that forEach has at least 1 execution at 40ms, which is way above the rest. Otherwise using the ObjectLabKit Calculator seems a good compromise between having to write the reduce correctly (! watch out if the BigDecimal on the right is null!) and using the raw map/reduce. 

The Parallel Stream is not as efficient, as it takes some time to coordinate the tasks and split the list. let's see if it gets any different with more data. 

On my 2012 MacBook Pro (QuadCore) for a list of 50,000 Test instances and the parallelStream is then becoming the most efficient.

AlgoAverage (ms)Min (ms)Max (ms)
Use Total in a for loop1020
Use Total with java8 forEach1.1048
Use Total and java8 map()2.1140
Use Java8 map and reduce1.219
Use Java8 map, reduce and accumulator1.2110
Use Java8 and home-made Collector1.4112
Use Java8 and ObjectLabKit Calculator1.2111
Use Java8 and Parallel Stream0.6017


So it looks like, when using single thread, that the RAW use of stream.map and reduce is the most efficient but one has to remember how to write it:

  final BigDecimal reduce = list.stream()
        .map(Test::getValue)
        .reduce(BigDecimal.ZERO, 
                (a, b) -> b != null ? a.add(b) : a);

Using the parallelStream (when suitable) reduces the average to 0.5ms but the max is 18ms
  final BigDecimal reduce = list.parallelStream()
        .map(Test::getValue)
        .reduce(BigDecimal.ZERO, 
                (a, b) -> b != null ? a.add(b) : a);


Full code available here at GitHub Gist

Friday, 12 March 2010

Sonar and BlackDuck.com for ObjectLab Kit.

A big thank you to Simon from SonarSource.org to include ObjectLab Kit in their demo site.

Check this out: http://nemo.sonarqube.org/dashboard/index/250253

But we're going to make it better... reach a higher % of compliance...

Also thanks to ohloh.net

https://www.openhub.net/p/objectlabkit

If you are using it... vote for it!

And, yepee, I am ranked 2,400 or something out of 315,000...

https://www.openhub.net/accounts/benoitx

Whatever that means.

Enjoy!

Friday, 6 April 2007

First post... why a combined blog?

Welcome to the ObjectLab Open Source News.

Our company, ObjectLab Financial Ltd is quite involved in some open source projects. We believe that, as a whole, this family of open source projects can help you.

So what are they?


  • QALab, our first project. It keeps track of QA statistics over time based on Checkstyle, PMD, FindBugs, Cobertura, Simian and more to come!

  • ObjectLab Kit, a series of Date calculators for business and finance. It is Apache 2.0 so feel free!

  • JTreeMap, the only open source Apache 2.0 HeatMap solution for java!

  • StatSVN, a cool way to discover who has done what and when in your Subversion repository (sorry we cannot help on the 'why' they did it!)

  • PZFileReader, a parser for delimited or fixed-length text. It is not as easy as it sounds... very flexible.

  • StatCVS, same as StatSVN but for CVS (we just contributed a bit...)



So, why a combined blog? So you can keep track of the news for all our projects in one place!

All news releases will announced here!

Cheers

Benoit & the rest of the team.