Skip to content

Commit

Permalink
optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
sgehrman authored and lincolnthree committed Mar 10, 2014
1 parent 558a091 commit 545fcbf
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions core/src/main/java/org/ocpsoft/prettytime/PrettyTime.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class PrettyTime
private volatile Date reference; private volatile Date reference;
private volatile Locale locale = Locale.getDefault(); private volatile Locale locale = Locale.getDefault();
private volatile Map<TimeUnit, TimeFormat> units = new LinkedHashMap<TimeUnit, TimeFormat>(); private volatile Map<TimeUnit, TimeFormat> units = new LinkedHashMap<TimeUnit, TimeFormat>();
private List<TimeUnit> mCachedUnits;


/** /**
* Default constructor * Default constructor
Expand Down Expand Up @@ -147,22 +148,21 @@ private Duration calculateDuration(final long difference)
long absoluteDifference = Math.abs(difference); long absoluteDifference = Math.abs(difference);


// Required for thread-safety // Required for thread-safety
List<TimeUnit> units = new ArrayList<TimeUnit>(getUnits().size()); List<TimeUnit> localUnits = getUnits();
units.addAll(getUnits());


DurationImpl result = new DurationImpl(); DurationImpl result = new DurationImpl();


for (int i = 0; i < units.size(); i++) for (int i = 0; i < localUnits.size(); i++)
{ {
TimeUnit unit = units.get(i); TimeUnit unit = localUnits.get(i);
long millisPerUnit = Math.abs(unit.getMillisPerUnit()); long millisPerUnit = Math.abs(unit.getMillisPerUnit());
long quantity = Math.abs(unit.getMaxQuantity()); long quantity = Math.abs(unit.getMaxQuantity());


boolean isLastUnit = (i == units.size() - 1); boolean isLastUnit = (i == localUnits.size() - 1);


if ((0 == quantity) && !isLastUnit) if ((0 == quantity) && !isLastUnit)
{ {
quantity = units.get(i + 1).getMillisPerUnit() / unit.getMillisPerUnit(); quantity = localUnits.get(i + 1).getMillisPerUnit() / unit.getMillisPerUnit();
} }


// does our unit encompass the time duration? // does our unit encompass the time duration?
Expand Down Expand Up @@ -418,10 +418,14 @@ public PrettyTime setReference(final Date timestamp)
*/ */
public List<TimeUnit> getUnits() public List<TimeUnit> getUnits()
{ {
List<TimeUnit> result = new ArrayList<TimeUnit>(units.keySet()); if (mCachedUnits == null) {
Collections.sort(result, new TimeUnitComparator()); List<TimeUnit> result = new ArrayList<TimeUnit>(units.keySet());
return Collections.unmodifiableList(result); Collections.sort(result, new TimeUnitComparator());
} mCachedUnits = Collections.unmodifiableList(result);
}

return mCachedUnits;
}


/** /**
* Get the registered {@link TimeUnit} for the given {@link TimeUnit} type or null if none exists. * Get the registered {@link TimeUnit} for the given {@link TimeUnit} type or null if none exists.
Expand Down Expand Up @@ -455,7 +459,9 @@ public PrettyTime registerUnit(final TimeUnit unit, TimeFormat format)
if (format == null) if (format == null)
throw new IllegalArgumentException("Format to register must not be null."); throw new IllegalArgumentException("Format to register must not be null.");


units.put(unit, format); mCachedUnits = null;

units.put(unit, format);
if (unit instanceof LocaleAware) if (unit instanceof LocaleAware)
((LocaleAware<?>) unit).setLocale(locale); ((LocaleAware<?>) unit).setLocale(locale);
if (format instanceof LocaleAware) if (format instanceof LocaleAware)
Expand All @@ -476,7 +482,9 @@ public <UNIT extends TimeUnit> TimeFormat removeUnit(final Class<UNIT> unitType)
for (TimeUnit unit : units.keySet()) { for (TimeUnit unit : units.keySet()) {
if (unitType.isAssignableFrom(unit.getClass())) if (unitType.isAssignableFrom(unit.getClass()))
{ {
return units.remove(unit); mCachedUnits = null;

return units.remove(unit);
} }
} }
return null; return null;
Expand All @@ -492,7 +500,9 @@ public TimeFormat removeUnit(final TimeUnit unit)
if (unit == null) if (unit == null)
throw new IllegalArgumentException("Unit to remove must not be null."); throw new IllegalArgumentException("Unit to remove must not be null.");


return units.remove(unit); mCachedUnits = null;

return units.remove(unit);
} }


/** /**
Expand Down Expand Up @@ -535,7 +545,9 @@ public String toString()
public List<TimeUnit> clearUnits() public List<TimeUnit> clearUnits()
{ {
List<TimeUnit> result = getUnits(); List<TimeUnit> result = getUnits();
units.clear();
mCachedUnits = null;
units.clear();
return result; return result;
} }


Expand Down

0 comments on commit 545fcbf

Please sign in to comment.