Reminder: we have moved to GitHub .
2022-01-11 : version 1.4.8 is out, updated dependencies to remove Security warnings.
2014-08-26 : version 1.4.0 is out and contains a new Immutable and thread safe Currency Calculator. This uses a Builder pattern. You should use this for any calculation for a currency pair, do not forget to register the holidays for each currency prior to getting the calculator. Spot rules for Latin American currencies on T+1 are handled as well as the Arabic currencies with different weekends. More info about Currency Date Calculation and on the changes here . Finally, we are also introducing a new module the ObjectLab Kit FX Calculator use it for determining Cross Currency Market Conventions and calculating FX Cross Rates.
2014-05-26 : version 1.3.0 is out and contains a couple of changes to make the usage of the interfaces more fluent but mainly include a NEW MODULE FOR JDK8. We also officially release the objectlab-utils module for the first time, if you deal with BigDecimal, Collections etc and are fed up with nulls, this is for you! More info on the changes here .
2010-05-10 : version 1.2.0 is out and contains a couple of changes, including the ability to see the registered calendar and unregistering calendars (useful if calendars are dynamic). More info on the changes here . Also, one of the authors is on Twitter, follow your favorite Belgian at http://twitter.com/benoitx .
2007-03-25: version 1.1.0 is out and contains a couple of changes, a new HolidayHandler and the ability to define a valid range for the holiday (and if the calculation is beyond that range, an exception is thrown). This is done via HolidayCalendar which should replace the simple Set<E> for holidays. More info on the changes here .
ObjectLab Kit came out of our frustration to have to re-do the same kind of code over and over each time we joined a new company and Bank. Most banks will require basic Date calculation, we did spot another open source project for this but their licence forbids most financial institution from using it. This one is released under the business-friendly Apache 2.0 license.
DateCalc comes in 3 different implementations, both run on JDK 1.8 or higher :
If you're using Maven, setup is easy, as DateCalc is in the Maven Central Repository.
To use the JDK Version:
< dependency > |
< groupId >net.objectlab.kit</ groupId > |
< artifactId >datecalc-common</ artifactId > |
< version >1.4.1</ version > |
</ dependency > |
< dependency > |
< groupId >net.objectlab.kit</ groupId > |
< artifactId >datecalc-jdk</ artifactId > |
< version >1.4.1</ version > |
</ dependency > |
To use the JODA Version (recommended):
< dependency > |
< groupId >net.objectlab.kit</ groupId > |
< artifactId >datecalc-common</ artifactId > |
< version >1.4.1</ version > |
</ dependency > |
< dependency > |
< groupId >net.objectlab.kit</ groupId > |
< artifactId >datecalc-joda</ artifactId > |
< version >1.4.1</ version > |
</ dependency > |
To use the JDK8 Version:
< dependency > |
< groupId >net.objectlab.kit</ groupId > |
< artifactId >datecalc-common</ artifactId > |
< version >1.4.1</ version > |
</ dependency > |
< dependency > |
< groupId >net.objectlab.kit</ groupId > |
< artifactId >datecalc-jdk8</ artifactId > |
< version >1.4.1</ version > |
</ dependency > |
If you are not using Maven, you can download the jars here .
Apart from the very basic "add days" features, most business have to deal with Holidays and what to do when a calculated day falls on a holiday . This library does not attempt to create or guess the holidays, we all know that some bank holidays can be decided at a moment's notice in some markets. All financial institutions or big business will have their own official list of 'holidays' anyway.
Furthermore, "weekends" also need to be handled and some market have a different week to the conventional Monday -> Friday, our library provides you with full flexibility to design a Working Week.
As such a Non-working Day can be a holiday or a 'weekend'. Also note that CurrencyPairs have a specialised calculator as the rules can be 'weird'. See for more info .
At the moment, we support the following 6 algorithms:
See this page for some examples .
The main interfaces are:
Here are the examples of how to get a DateCalculator "forward" for the "UK" holidays (if you have registered the holidays). The WorkingWeek is Mon-Fri by default.
2 implementations for Pure 'old' JDK have been released
DateCalculator<Calendar> calc1 = CalendarKitCalculatorsFactory.forwardCalculator("UK"); | |
PeriodCountCalculator<Calendar> calc2 = CalendarKitCalculatorsFactory.getDefaultInstance().getPeriodCountCalculator(); | |
IMMDateCalculator<Calendar> calc3 = CalendarKitCalculatorsFactory.getDefaultInstance().getIMMDateCalculator(); |
DateCalculator<Date> calc1 = DateKitCalculatorsFactory.forwardCalculator("UK"); | |
PeriodCountCalculator<Date> calc2 = DateKitCalculatorsFactory.getDefaultInstance().getPeriodCountCalculator(); | |
IMMDateCalculator<Date> calc3 = DateKitCalculatorsFactory.getDefaultInstance().getIMMDateCalculator(); |
2 implementations for Joda have been released
DateCalculator<LocalDate> calc1 = LocalDateKitCalculatorsFactory.forwardCalculator("UK"); | |
PeriodCountCalculator<LocalDate> calc2 = LocalDateKitCalculatorsFactory.getDefaultInstance().getPeriodCountCalculator(); | |
IMMDateCalculator<LocalDate> calc3 = LocalDateKitCalculatorsFactory.getDefaultInstance().getIMMDateCalculator(); |
DateCalculator<YearMonthDay> calc1 = YearMonthDayKitCalculatorsFactory.forwardCalculator("UK"); | |
PeriodCountCalculator<YearMonthDay> calc2 = YearMonthDayKitCalculatorsFactory.getDefaultInstance().getPeriodCountCalculator(); | |
IMMDateCalculator<YearMonthDay> calc3 = YearMonthDayKitCalculatorsFactory.getDefaultInstance().getIMMDateCalculator(); |
1 implementations for JDK8 has been released
DateCalculator<LocalDate> calc1 = LocalDateKitCalculatorsFactory.forwardCalculator("UK"); | |
PeriodCountCalculator<LocalDate> calc2 = LocalDateKitCalculatorsFactory.getDefaultInstance().getPeriodCountCalculator(); | |
IMMDateCalculator<LocalDate> calc3 = LocalDateKitCalculatorsFactory.getDefaultInstance().getIMMDateCalculator(); | |
There are several steps
package net.objectlab.kit.datecalc.gist; | |
import java.util.HashSet; | |
import java.util.Set; | |
import net.objectlab.kit.datecalc.common.CurrencyDateCalculator; | |
import net.objectlab.kit.datecalc.common.CurrencyDateCalculatorBuilder; | |
import net.objectlab.kit.datecalc.common.DefaultHolidayCalendar; | |
import net.objectlab.kit.datecalc.common.HolidayCalendar; | |
import net.objectlab.kit.datecalc.common.SpotLag; | |
import net.objectlab.kit.datecalc.common.StandardTenor; | |
import net.objectlab.kit.datecalc.common.ccy.DefaultCurrencyCalculatorConfig; | |
import net.objectlab.kit.datecalc.joda.LocalDateCurrencyDateCalculator; | |
import net.objectlab.kit.datecalc.joda.LocalDateForwardHandler; | |
import net.objectlab.kit.datecalc.joda.LocalDateKitCalculatorsFactory; | |
import org.joda.time.LocalDate; | |
public class GistCurrencyCalculatorExample { | |
public static void main(final String[] args) { | |
simpleEurUsd(); | |
simpleEurUsd1M(); | |
crossEurGbp(); | |
crossGbpJpyWithHolidays(); | |
crossEurMxn(); | |
simpleUsdAed(); | |
simpleUsdJod(); | |
} | |
private static void simpleUsdJod() { | |
final LocalDateCurrencyDateCalculator calc = LocalDateKitCalculatorsFactory.forwardCurrencyDateCalculator(CurrencyDateCalculator.USD_CODE, | |
"JOD", SpotLag.T_2); | |
final LocalDate startDate = new LocalDate(2006, 7, 6); // Thursday | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects Tuesday 11 Jul"); | |
} | |
private static void simpleUsdAed() { | |
final LocalDateCurrencyDateCalculator calc = LocalDateKitCalculatorsFactory.forwardCurrencyDateCalculator(CurrencyDateCalculator.USD_CODE, | |
"AED", SpotLag.T_2); | |
final LocalDate startDate = new LocalDate(2006, 7, 6); // Thursday | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects Monday 10 Jul"); | |
} | |
private static void crossGbpJpyWithHolidays() { | |
final Set<LocalDate> gbpHholidays = new HashSet<LocalDate>(); | |
gbpHholidays.add(new LocalDate("2014-08-04")); | |
final HolidayCalendar<LocalDate> gbpCalendar = new DefaultHolidayCalendar<LocalDate>(gbpHholidays, new LocalDate("2014-01-01"), | |
new LocalDate("2014-12-31")); | |
LocalDateKitCalculatorsFactory.getDefaultInstance().registerHolidays("GBP", gbpCalendar); | |
final Set<LocalDate> jpyHolidays = new HashSet<LocalDate>(); | |
jpyHolidays.add(new LocalDate("2014-08-05")); | |
final HolidayCalendar<LocalDate> jpyCalendar = new DefaultHolidayCalendar<LocalDate>(jpyHolidays, new LocalDate("2014-01-01"), new LocalDate( | |
"2014-12-31")); | |
LocalDateKitCalculatorsFactory.getDefaultInstance().registerHolidays("JPY", jpyCalendar); | |
final LocalDateCurrencyDateCalculator calc = LocalDateKitCalculatorsFactory.forwardCurrencyDateCalculator("GBP", "JPY", SpotLag.T_2); | |
// set startDate, this will also set the current business date. | |
final LocalDate startDate = new LocalDate(2014, 8, 1); // Friday | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 6 Aug"); | |
} | |
private static void crossEurMxn() { | |
final Set<LocalDate> holidays = new HashSet<LocalDate>(); | |
holidays.add(new LocalDate("2006-07-04")); | |
// create the HolidayCalendar ASSUMING that the set covers 2006! | |
final HolidayCalendar<LocalDate> usdCalendar = new DefaultHolidayCalendar<LocalDate>(holidays, new LocalDate("2006-01-01"), new LocalDate( | |
"2006-12-31")); | |
final CurrencyDateCalculatorBuilder<LocalDate> builder = new CurrencyDateCalculatorBuilder<LocalDate>() // | |
.currencyPair("EUR", "MXN", SpotLag.T_2) // | |
.currencyCalculatorConfig(new DefaultCurrencyCalculatorConfig())// | |
.crossCcyCalendar(usdCalendar) // | |
.tenorHolidayHandler(new LocalDateForwardHandler()); | |
final LocalDateCurrencyDateCalculator calc = new LocalDateCurrencyDateCalculator(builder); | |
LocalDate startDate = new LocalDate(2006, 6, 30); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 5 Jul"); | |
startDate = new LocalDate(2006, 7, 2); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 6 Jul"); | |
startDate = new LocalDate(2006, 7, 3); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 6 Jul"); | |
} | |
private static void simpleEurUsd() { | |
final Set<LocalDate> holidays = new HashSet<LocalDate>(); | |
holidays.add(new LocalDate("2006-07-04")); | |
// create the HolidayCalendar ASSUMING that the set covers 2006! | |
final HolidayCalendar<LocalDate> usdCalendar = new DefaultHolidayCalendar<LocalDate>(holidays, new LocalDate("2006-01-01"), new LocalDate( | |
"2006-12-31")); | |
final CurrencyDateCalculatorBuilder<LocalDate> builder = new CurrencyDateCalculatorBuilder<LocalDate>() // | |
.currencyPair("EUR", CurrencyDateCalculator.USD_CODE, SpotLag.T_2) // | |
.currencyCalculatorConfig(new DefaultCurrencyCalculatorConfig())// | |
.crossCcyCalendar(usdCalendar) // | |
.ccy2Calendar(usdCalendar)// | |
.tenorHolidayHandler(new LocalDateForwardHandler()); | |
final LocalDateCurrencyDateCalculator calc = new LocalDateCurrencyDateCalculator(builder); | |
LocalDate startDate = new LocalDate(2006, 6, 30); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 5 Jul"); | |
startDate = new LocalDate(2006, 7, 2); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 5 Jul"); | |
startDate = new LocalDate(2006, 7, 3); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 5 Jul"); | |
} | |
private static void simpleEurUsd1M() { | |
final LocalDateCurrencyDateCalculator calc = LocalDateKitCalculatorsFactory.forwardCurrencyDateCalculator("EUR", "USD", SpotLag.T_2); | |
LocalDate startDate = new LocalDate(2006, 6, 26); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 30 Jun"); | |
System.out.println(calc.getName() + " TD: " + startDate + " 1-M " + calc.calculateTenorDate(startDate, StandardTenor.T_1M) | |
+ " expects 30 Jul"); | |
} | |
private static void crossEurGbp() { | |
final Set<LocalDate> holidays = new HashSet<LocalDate>(); | |
holidays.add(new LocalDate("2006-07-04")); | |
// create the HolidayCalendar ASSUMING that the set covers 2006! | |
final HolidayCalendar<LocalDate> usdCalendar = new DefaultHolidayCalendar<LocalDate>(holidays, new LocalDate("2006-01-01"), new LocalDate( | |
"2006-12-31")); | |
final CurrencyDateCalculatorBuilder<LocalDate> builder = new CurrencyDateCalculatorBuilder<LocalDate>() // | |
.currencyPair("EUR", "GBP", SpotLag.T_2) // | |
.currencyCalculatorConfig(new DefaultCurrencyCalculatorConfig())// | |
.crossCcyCalendar(usdCalendar) // | |
.tenorHolidayHandler(new LocalDateForwardHandler()); | |
final LocalDateCurrencyDateCalculator calc = new LocalDateCurrencyDateCalculator(builder); | |
LocalDate startDate = new LocalDate(2006, 7, 2); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 5 Jul"); | |
startDate = new LocalDate(2006, 6, 30); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 5 Jul"); | |
} | |
} |
package net.objectlab.kit.datecalc.gist; | |
import java.util.HashSet; | |
import java.util.Set; | |
import net.objectlab.kit.datecalc.common.CurrencyDateCalculatorBuilder; | |
import net.objectlab.kit.datecalc.common.DefaultHolidayCalendar; | |
import net.objectlab.kit.datecalc.common.HolidayCalendar; | |
import net.objectlab.kit.datecalc.common.SpotLag; | |
import net.objectlab.kit.datecalc.common.WorkingWeek; | |
import net.objectlab.kit.datecalc.common.ccy.DefaultCurrencyCalculatorConfig; | |
import net.objectlab.kit.datecalc.joda.LocalDateCurrencyDateCalculator; | |
import net.objectlab.kit.datecalc.joda.LocalDateForwardHandler; | |
import org.joda.time.LocalDate; | |
public class GistCurrencyCalculatorWithBuilderExample { | |
public static void main(final String[] args) { | |
simpleEurUsd(); | |
} | |
private static void simpleEurUsd() { | |
final Set<LocalDate> holidays = new HashSet<LocalDate>(); | |
holidays.add(new LocalDate("2006-07-04")); | |
// create the HolidayCalendar ASSUMING that the set covers 2006! | |
final HolidayCalendar<LocalDate> usdCalendar = new DefaultHolidayCalendar<LocalDate>(holidays, new LocalDate("2006-01-01"), new LocalDate( | |
"2006-12-31")); | |
final CurrencyDateCalculatorBuilder<LocalDate> builder = new CurrencyDateCalculatorBuilder<LocalDate>() // | |
.currencyPair("EUR", "USD", SpotLag.T_2) // | |
.ccy1Calendar(new DefaultHolidayCalendar<LocalDate>()) // empty | |
.ccy1Week(WorkingWeek.DEFAULT) // Mon-Fri | |
.ccy2Calendar(usdCalendar) // | |
.ccy2Week(WorkingWeek.DEFAULT) // Mon-Fri | |
.crossCcyCalendar(usdCalendar) // | |
.crossCcyWeek(WorkingWeek.DEFAULT) // Mon-Fri; | |
.adjustStartDateWithCurrencyPair(true) // default is true, Move the startDate to a working date for ccy1 and ccy2 | |
.tenorHolidayHandler(new LocalDateForwardHandler()) // Forward | |
.brokenDateAllowed(true) // use USD holidays on Spot Date | |
.currencyCalculatorConfig(new DefaultCurrencyCalculatorConfig()) // Will be used for finding Working Weeks if not provided and Latin | |
// American ccy USD handling. | |
; | |
final LocalDateCurrencyDateCalculator calc = new LocalDateCurrencyDateCalculator(builder); | |
LocalDate startDate = new LocalDate(2006, 6, 30); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 5 Jul"); | |
startDate = new LocalDate(2006, 7, 2); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 5 Jul"); | |
startDate = new LocalDate(2006, 7, 3); | |
System.out.println(calc.getName() + " TD: " + startDate + " Spot " + calc.calculateSpotDate(startDate) + " expects 5 Jul"); | |
} | |
} |
// create or get the Holidays | |
final Set<LocalDate> holidays = new HashSet<LocalDate>(); | |
holidays.add(new LocalDate("2006-08-28")); | |
//... keep adding all holidays for 2006 | |
// create the HolidayCalendar ASSUMING that the set covers 2006! | |
final HolidayCalendar<LocalDate> calendar = new DefaultHolidayCalendar<LocalDate>( holidays, new LocalDate("2006-01-01"), new LocalDate("2006-12-31")); | |
// register the holidays, any calculator with name "UK" | |
// asked from now on will receive an IMMUTABLE reference to this calendar | |
LocalDateKitCalculatorsFactory.getDefaultInstance().registerHolidays("UK", calendar); | |
// ask for a LocalDate calculator for "UK" | |
// (evenif a new set of holidays is registered, this one calculator is not affected | |
DateCalculator<LocalDate> cal = | |
LocalDateKitCalculatorsFactory.forwardCalculator("UK"); | |
// set startDate, this will also set the current business date. | |
cal.setStartDate(new LocalDate("2006-08-28")); | |
// startDate stays 28 Aug 06 BUT the currentDate has moved, | |
// according to Forward handler to 29 Aug 2006. | |
LocalDate start = cal.getStartDate(); // 28 Aug 06 | |
LocalDate current = cal.getCurrentBusinessDate(); // 29 Aug 06 | |
LocalDate newCurrent = cal.moveByDays(4).getCurrentBusinessDate(); // 4 Sept 06 due to weekend! | |
// Example with Tenor, 1W with a 2 day spot lag | |
LocalDate date1WeekFromSpot = cal.moveByTenor(StandardTenor.T_1W, 2).getCurrentBusinessDate(); |
// create or get the Holidays | |
final Set<Calendar> holidays = new HashSet<Calendar>(); | |
Calendar calendar = Calendar.getInstance(); | |
calendar.set(Calendar.DAY_OF_MONTH, 28); | |
calendar.set(Calendar.MONTH, Calendar.AUGUST); | |
calendar.set(Calendar.YEAR, 2006); | |
holidays.add(calendar); | |
//... keep adding all holidays for 2006 | |
// create the HolidayCalendar ASSUMING that the set covers 2006! | |
final HolidayCalendar<Calendar> calendar = new DefaultHolidayCalendar<Calendar> | |
(holidays, new Calendar("2006-01-01"), new Calendar("2006-12-31")); | |
// register the holidays, any calculator with name "UK" | |
// asked from now on will receive an IMMUTABLE reference to this calendar | |
CalendarKitCalculatorsFactory.getDefaultInstance().registerHolidays("UK", calendar); | |
// ask for a LocalDate calculator for "UK" | |
// (even if a new set of holidays is registered, this one calculator is not affected | |
DateCalculator<Calendar> cal = LocalDateKitCalculatorsFactory.getDefaultInstance() | |
.getDateCalculator("UK", HolidayHandlerType.FORWARD); | |
Calendar initial = Calendar.getInstance(); | |
initial.set(Calendar.DAY_OF_MONTH, 28); | |
initial.set(Calendar.MONTH, Calendar.AUGUST); | |
initial.set(Calendar.YEAR, 2006); | |
// set startDate, this will also set the current business date. | |
cal.setStartDate(initial); | |
// the startDate stays 28 Aug 06 BUT the currentDate has moved, | |
// according to Forward handler to 29 Aug 2006. | |
Calendar start = cal.getStartDate(); // 28 Aug 06 | |
Calendar current = cal.getCurrentBusinessDate(); // 29 Aug 06 | |
Calendar newCurrent = cal.moveByDays(4).getCurrentBusinessDate(); // 4 Sept 06 due to weekend! | |
// Example with Tenor, 1W with a 2 day spot lag | |
Calendar date1WeekFromSpot = cal.moveByTenor(StandardTenor.T_1W, 2).getCurrentBusinessDate(); |
// create or get the Holidays | |
final Set<LocalDate> holidays = new HashSet<LocalDate>(); | |
holidays.add(LocalDate.parse("2006-08-28")); | |
//... keep adding all holidays for 2006 | |
// create the HolidayCalendar ASSUMING that the set covers 2006! | |
final HolidayCalendar<LocalDate> calendar = new DefaultHolidayCalendar<LocalDate> | |
(holidays, LocalDate.parse("2006-01-01"), LocalDate.parse("2006-12-31")); | |
// register the holidays, any calculator with name "UK" | |
// asked from now on will receive an IMMUTABLE reference to this calendar | |
LocalDateKitCalculatorsFactory.getDefaultInstance().registerHolidays("UK", calendar); | |
// ask for a LocalDate calculator for "UK" | |
// (even if a new set of holidays is registered, this one calculator is not affected | |
DateCalculator<LocalDate> cal = LocalDateKitCalculatorsFactory.forwardCalculator("UK"); | |
// set startDate, this will also set the current business date. | |
cal.setStartDate(LocalDate.parse("2006-08-28")); | |
// startDate stays 28 Aug 06 BUT the currentDate has moved, | |
// according to Forward handler to 29 Aug 2006. | |
LocalDate start = cal.getStartDate(); // 28 Aug 06 | |
LocalDate current = cal.getCurrentBusinessDate(); // 29 Aug 06 | |
LocalDate newCurrent = cal.moveByDays(4).getCurrentBusinessDate(); // 4 Sept 06 due to weekend! | |
// Example with Tenor, 1W with a 2 day spot lag | |
LocalDate date1WeekFromSpot = cal.moveByTenor(StandardTenor.T_1W, 2).getCurrentBusinessDate(); |
package net.objectlab.kit.util.example; | |
import java.math.BigDecimal; | |
import net.objectlab.kit.util.Total; | |
public class TotalExample { | |
public static void main(final String[] args) { | |
final Total total = new Total(); | |
total.add(new BigDecimal(1)); | |
total.add(new BigDecimal(21)); | |
total.add(new BigDecimal(30)); | |
System.out.println("Number of data Points (3): " + total.getCount()); | |
System.out.println("Total (52): " + total.getTotal()); | |
} | |
/* | |
* Output | |
Number of data Points (3): 3 | |
Total (52): 52 | |
*/ | |
} |
package net.objectlab.kit.util.example; | |
import java.math.BigDecimal; | |
import net.objectlab.kit.util.Average; | |
public class AverageExample { | |
public static void main(String[] args) { | |
final Average avg = new Average(3); // scale for the result, otherwise it uses the scale of the first datapoint! | |
avg.add(new BigDecimal(1)); | |
avg.add(new BigDecimal(2)); | |
System.out.println("Number of data Points (2): " + avg.getDataPoints()); | |
System.out.println("Average upto 3 dec. pt (1.500): " + avg.getAverage()); | |
} | |
} |
package net.objectlab.kit.util.example; | |
import java.math.BigDecimal; | |
import net.objectlab.kit.util.WeightedAverage; | |
public class WeightedAverageExample { | |
public static void main(String[] args) { | |
// Credit card weight | |
// Credit Card 1: Rate 20% Balance $6,413 | |
// Credit Card 2: Rate 15% Balance $3,500 | |
// Credit Card 3: Rate 5% Balance $11,400 | |
final WeightedAverage avg = new WeightedAverage(false); // exclude zeros | |
avg.add(new BigDecimal(0.20), new BigDecimal(6413)); | |
avg.add(new BigDecimal(0.15), new BigDecimal(3500)); | |
avg.add(new BigDecimal(0.05), new BigDecimal(11400)); | |
System.out.println("Number of positions (3): " + avg.getCount()); | |
System.out.println("Total Balance $21,313: " + avg.getTotal()); | |
System.out.println("Weighted Average Rate (~11.15%): " + avg.getWeightedAverage()); | |
} | |
} |
package net.objectlab.kit.util.example; | |
import java.math.BigDecimal; | |
import java.util.HashSet; | |
import java.util.Set; | |
import net.objectlab.kit.util.Pair; | |
public class PairExample { | |
public static void main(String[] args) { | |
Pair<String, BigDecimal> p1 = Pair.create("AMGN", new BigDecimal("114.13")); | |
Pair<String, BigDecimal> p2 = Pair.create("AAPL", new BigDecimal("614.88")); | |
Pair<String, BigDecimal> p3 = Pair.create("AAPL", new BigDecimal("614.88")); | |
Set<Pair<String, BigDecimal>> set = new HashSet<Pair<String, BigDecimal>>(); | |
set.add(p1); | |
set.add(p2); | |
set.add(p3); | |
System.out.println("Only 2 elements! (same pair twice):" + set.size()); | |
} | |
} |