Skip to content

ResourcesTimeUnit should be either immutable or not use maxQuantity and millisPerUnit in hashCode() #152

Closed
@mkouba

Description

Time units are used as keys in a LinkedHashMap!
https://github.com/ocpsoft/prettytime/blob/master/core/src/main/java/org/ocpsoft/prettytime/PrettyTime.java#L68

As a result, if you do:

PrettyTime prettyTime = new PrettyTime();
prettyTime.getUnit(JustNow.class).setMaxQuantity(1000L * 2L);
prettyTime.format(new Date());

you will most probably get NPE because the TimeFormat for JustNow will not be found.

A workaround is to replace the unit:

PrettyTime prettyTime = new PrettyTime();
TimeFormat timeFormat = prettyTime.removeUnit(JustNow.class);
JustNow justNow = new JustNow();
justNow.setMaxQuantity(1000L * 2L);
prettyTime.registerUnit(justNow, timeFormat);
prettyTime.format(new Date());

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions