Skip to content

Commit

Permalink
Use CopyOnWriteArrayList in PrettyTime.
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Mar 3, 2023
1 parent 28787d0 commit 76dbc99
Showing 1 changed file with 22 additions and 36 deletions.
58 changes: 22 additions & 36 deletions core/src/main/java/org/ocpsoft/prettytime/PrettyTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.ocpsoft.prettytime.impl.DurationImpl;
import org.ocpsoft.prettytime.impl.ResourcesTimeFormat;
Expand Down Expand Up @@ -73,9 +66,9 @@ public class PrettyTime
{
private volatile Instant reference;
private volatile Locale locale = Locale.getDefault();
private final Map<TimeUnit, TimeFormat> units = new ConcurrentHashMap<>();
private volatile List<TimeUnit> cachedUnits;
private String overrideResourceBundle;
private final Map<TimeUnit, TimeFormat> unitsAndFormats = new ConcurrentHashMap<>();
private final List<TimeUnit> cachedUnits = new CopyOnWriteArrayList<>();
private final String overrideResourceBundle;

/**
* Create a new {@link PrettyTime} instance that will always use the current value of
Expand Down Expand Up @@ -1308,13 +1301,13 @@ public TimeFormat getFormat(TimeUnit unit)
{
if (unit == null)
return null;
if (units.get(unit) != null) {
return units.get(unit);
if (unitsAndFormats.get(unit) != null) {
return unitsAndFormats.get(unit);
}
else {
// Trying to transform the TimeUnit to String does the trick
Map<String, TimeFormat> map = new ConcurrentHashMap<>();
units.keySet().forEach(key -> map.put(key.toString(), units.get(key)));
unitsAndFormats.keySet().forEach(key -> map.put(key.toString(), unitsAndFormats.get(key)));
return map.get(unit.toString());
}
}
Expand Down Expand Up @@ -1441,13 +1434,8 @@ public PrettyTime setReference(final LocalDate localDate, final ZoneId zoneId)
*/
public List<TimeUnit> getUnits()
{
if (cachedUnits == null) {
List<TimeUnit> result = new ArrayList<>(units.keySet());
Collections.sort(result, Comparator.comparing(TimeUnit::getMillisPerUnit));
cachedUnits = Collections.unmodifiableList(result);
}

return cachedUnits;
cachedUnits.sort(Comparator.comparingLong(TimeUnit::getMillisPerUnit));
return Collections.unmodifiableList(new ArrayList<>(cachedUnits));
}

/**
Expand All @@ -1459,7 +1447,7 @@ public <UNIT extends TimeUnit> UNIT getUnit(final Class<UNIT> unitType)
if (unitType == null)
return null;

for (TimeUnit unit : units.keySet()) {
for (TimeUnit unit : unitsAndFormats.keySet()) {
if (unitType.isAssignableFrom(unit.getClass())) {
return (UNIT) unit;
}
Expand All @@ -1486,10 +1474,9 @@ public TimeUnit getUnit(final ChronoUnit unit)
*/
public PrettyTime registerUnit(final TimeUnit unit, TimeFormat format)
{
cachedUnits = null;
cachedUnits.add(unit);
unitsAndFormats.put(unit, format);

units.put(Objects.requireNonNull(unit, "TimeUnit to register must not be null."),
Objects.requireNonNull(format, "TimeFormat to register must not be null."));
if (unit instanceof LocaleAware)
((LocaleAware<?>) unit).setLocale(locale);
if (format instanceof LocaleAware)
Expand Down Expand Up @@ -1548,11 +1535,11 @@ public <UNIT extends TimeUnit> TimeFormat removeUnit(final Class<UNIT> unitType)
if (unitType == null)
return null;

for (TimeUnit unit : units.keySet()) {
for (TimeUnit unit : unitsAndFormats.keySet()) {
if (unitType.isAssignableFrom(unit.getClass())) {
cachedUnits = null;
cachedUnits.remove(unit);

return units.remove(unit);
return unitsAndFormats.remove(unit);
}
}
return null;
Expand All @@ -1568,9 +1555,9 @@ public TimeFormat removeUnit(final TimeUnit unit)
if (unit == null)
return null;

cachedUnits = null;
cachedUnits.remove(unit);

return units.remove(unit);
return unitsAndFormats.remove(unit);
}

/**
Expand Down Expand Up @@ -1603,15 +1590,14 @@ public PrettyTime setLocale(Locale locale)
locale = Locale.getDefault();

this.locale = locale;
for (TimeUnit unit : units.keySet()) {
for (TimeUnit unit : unitsAndFormats.keySet()) {
if (unit instanceof LocaleAware)
((LocaleAware<?>) unit).setLocale(locale);
}
for (TimeFormat format : units.values()) {
for (TimeFormat format : unitsAndFormats.values()) {
if (format instanceof LocaleAware)
((LocaleAware<?>) format).setLocale(locale);
}
cachedUnits = null;
return this;
}

Expand All @@ -1627,8 +1613,8 @@ public String toString()
public List<TimeUnit> clearUnits()
{
List<TimeUnit> result = getUnits();
cachedUnits = null;
units.clear();
cachedUnits.clear();
unitsAndFormats.clear();
return result;
}

Expand Down

0 comments on commit 76dbc99

Please sign in to comment.