class Utils {

    public static List<String> calculateRanges(final Collection<Integer> numbers) {
        // Copy list
        final List<Integer> sorted = new ArrayList<>();
        sorted.addAll(numbers);

        // Ensure numbers are sorted
        Collections.sort(sorted, Comparator.naturalOrder());

        // TODO filter duplicate numbers out?

        final List<String> ranges = new ArrayList<>();

        if (sorted.size() == 1) {
            ranges.add(String.valueOf(sorted.get(0)));
            return ranges;
        }

        final Iterator<Integer> numberIterator = numbers.iterator();
        if (!numberIterator.hasNext()) {
            return new ArrayList<>();
        }

        // Start calculating range?
        Integer first = null;
        Integer last = null;
        do {
            Integer number = numberIterator.next();

            if (first == null) {
                first = number;
                last = number;

                if (!numberIterator.hasNext()) {
                    ranges.add(String.valueOf(first));
                }
                continue;
            }

            if (number.equals(last + 1)) {
                last = number;

                if (!numberIterator.hasNext()) {
                    ranges.add(String.valueOf(first) + "-" + String.valueOf(last));
                }
                continue;
            } else {
                if (first.equals(last)) {
                    ranges.add(String.valueOf(first));
                } else {
                    ranges.add(String.valueOf(first) + "-" + String.valueOf(last));
                }
                first = number;
                last = number;

                if (!numberIterator.hasNext()) {
                    ranges.add(String.valueOf(first));
                }
                continue;
            }


        } while (numberIterator.hasNext());


        return ranges;
    }

}