public static List<ExInterval> unionExIntervals(List<ExInterval> I) {

        // Empty input.
        List<ExInterval> uni = new ArrayList<>();
        if (I.isEmpty()) {
            return uni;
        }

        // Sorts ExIntervals according to their left endpoints.
        Collections.sort(I);
        ExInterval curr = new ExInterval();
        curr = I.get(0);

        for (int i = 1; i < I.size(); ++i) {
            if (I.get(i).left.val < curr.right.val
                    || (I.get(i).left.val == curr.right.val
                    && (I.get(i).left.isClose || curr.right.isClose))) {
                if (I.get(i).right.val > curr.right.val
                        || (I.get(i).right.val == curr.right.val
                        && I.get(i).right.isClose)) {
                    curr.right = I.get(i).right;
                }
            } else {
                uni.add(curr);
                curr = I.get(i);
            }
        }
        uni.add(curr);
        return uni;
    }