Utils Introduction

The Utils package provides classes for BigDecimal, Boolean, Collection, etc mainly dealing with the possibility of Null!. The package requires JDK8.

It also provides Total, Average and WeightedAverage that handle BigDecimals (including nulls) for you.

Examples

Using Util Total

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
*/
}

Using Util Average

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());
}
}

Using Util WeightedAverage

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());
}
}

Using Util Pair

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());
}
}
Similar usage for Triplet and Quadruplet (but you would probably better model those as proper domain classes).

Using Utils

package net.objectlab.kit.util.example;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import net.objectlab.kit.util.Util;
/**
* This is a VERY basic parsing/creation of CSV, if you seen anything superior then use FlatPack @see http://flatpack.sf.net
*/
public class BasicCsvExample {
public static void main(final String[] args) {
List<Object> list = new ArrayList<>();
list.add("Hello");
list.add(10);
list.add(new BigDecimal("1254"));
final String csvString = Util.listToCSVString(list);
System.out.println(csvString);
final List<String> list2 = Util.listify(csvString, ",");
System.out.println(list2);
}
/* OUTPUT:
Hello,10,1254
[Hello, 10, 1254]
*/
}