@@ -61,7 +61,7 @@ public class PrettyTime
6161 private volatile Date reference ;
6262 private volatile Locale locale = Locale .getDefault ();
6363 private volatile Map <TimeUnit , TimeFormat > units = new LinkedHashMap <TimeUnit , TimeFormat >();
64- private List <TimeUnit > mCachedUnits ;
64+ private List <TimeUnit > mCachedUnits ;
6565
6666 /**
6767 * Default constructor
@@ -226,6 +226,14 @@ public List<Duration> calculatePreciseDuration(final Date then)
226226 while (0 != duration .getDelta ())
227227 {
228228 duration = calculateDuration (duration .getDelta ());
229+ if (result .size () > 0 )
230+ {
231+ Duration last = result .get (result .size () - 1 );
232+ if (last .getUnit ().equals (duration .getUnit ()))
233+ {
234+ break ;
235+ }
236+ }
229237 result .add (duration );
230238 }
231239 return result ;
@@ -246,11 +254,11 @@ public String format(Date then)
246254 Duration d = approximateDuration (then );
247255 return format (d );
248256 }
249-
257+
250258 /**
251- * Format the given {@link Calendar} object. This method applies the {@code PrettyTime.approximateDuration(date)} method
252- * to perform its calculation. If {@code then} is null, it will default to {@code new Date()}; also decorate for
253- * past/future tense.
259+ * Format the given {@link Calendar} object. This method applies the {@code PrettyTime.approximateDuration(date)}
260+ * method to perform its calculation. If {@code then} is null, it will default to {@code new Date()}; also decorate
261+ * for past/future tense.
254262 *
255263 * @param duration the {@link Calendar} whose date is to be formatted
256264 * @return A formatted string representing {@code then}
@@ -333,43 +341,40 @@ public String format(final List<Duration> durations)
333341 duration = durations .get (i );
334342 format = getFormat (duration .getUnit ());
335343
336- boolean isLast = (i == durations .size () - 1 );
337- if (!isLast ) {
338- builder .append (format .formatUnrounded (duration ));
339- builder .append (" " );
340- }
341- else {
342- builder .append (format .format (duration ));
343- }
344+ builder .append (format .formatUnrounded (duration ));
345+ builder .append (" " );
344346 }
345347 result = format .decorateUnrounded (duration , builder .toString ());
346348 }
349+
347350 return result ;
348351 }
349352
350353 /**
351- * Given a date, returns a non-relative format string for the
352- * approximate duration of the difference between the date and now.
354+ * Given a date, returns a non-relative format string for the approximate duration of the difference between the date
355+ * and now.
353356 *
354357 * @param date the date to be formatted
355358 * @return A formatted string of the approximate duration
356359 */
357- public String formatApproximateDuration (Date date ) {
358- Duration duration = approximateDuration (date );
359- return formatDuration (duration );
360+ public String formatApproximateDuration (Date date )
361+ {
362+ Duration duration = approximateDuration (date );
363+ return formatDuration (duration );
360364 }
361-
365+
362366 /**
363367 * Given a duration, returns a non-relative format string.
364368 *
365369 * @param duration the duration to be formatted
366370 * @return A formatted string of the duration
367371 */
368- public String formatDuration (Duration duration ) {
369- TimeFormat timeFormat = getFormat (duration .getUnit ());
370- return timeFormat .format (duration );
372+ public String formatDuration (Duration duration )
373+ {
374+ TimeFormat timeFormat = getFormat (duration .getUnit ());
375+ return timeFormat .format (duration );
371376 }
372-
377+
373378 /**
374379 * Get the registered {@link TimeFormat} for the given {@link TimeUnit} or null if none exists.
375380 */
@@ -417,14 +422,14 @@ public PrettyTime setReference(final Date timestamp)
417422 */
418423 public List <TimeUnit > getUnits ()
419424 {
420- if (mCachedUnits == null ) {
421- List <TimeUnit > result = new ArrayList <TimeUnit >(units .keySet ());
422- Collections .sort (result , new TimeUnitComparator ());
423- mCachedUnits = Collections .unmodifiableList (result );
424- }
425+ if (mCachedUnits == null ) {
426+ List <TimeUnit > result = new ArrayList <TimeUnit >(units .keySet ());
427+ Collections .sort (result , new TimeUnitComparator ());
428+ mCachedUnits = Collections .unmodifiableList (result );
429+ }
425430
426- return mCachedUnits ;
427- }
431+ return mCachedUnits ;
432+ }
428433
429434 /**
430435 * Get the registered {@link TimeUnit} for the given {@link TimeUnit} type or null if none exists.
@@ -434,16 +439,16 @@ public List<TimeUnit> getUnits()
434439 @ SuppressWarnings ("unchecked" )
435440 public <UNIT extends TimeUnit > UNIT getUnit (final Class <UNIT > unitType )
436441 {
437- if (unitType == null )
438- throw new IllegalArgumentException ("Unit type to get must not be null." );
439-
440- for (TimeUnit unit : units .keySet ()) {
441- if (unitType .isAssignableFrom (unit .getClass ()))
442- {
443- return (UNIT ) unit ;
444- }
445- }
446- return null ;
442+ if (unitType == null )
443+ throw new IllegalArgumentException ("Unit type to get must not be null." );
444+
445+ for (TimeUnit unit : units .keySet ()) {
446+ if (unitType .isAssignableFrom (unit .getClass ()))
447+ {
448+ return (UNIT ) unit ;
449+ }
450+ }
451+ return null ;
447452 }
448453
449454 /**
@@ -458,9 +463,9 @@ public PrettyTime registerUnit(final TimeUnit unit, TimeFormat format)
458463 if (format == null )
459464 throw new IllegalArgumentException ("Format to register must not be null." );
460465
461- mCachedUnits = null ;
466+ mCachedUnits = null ;
462467
463- units .put (unit , format );
468+ units .put (unit , format );
464469 if (unit instanceof LocaleAware )
465470 ((LocaleAware <?>) unit ).setLocale (locale );
466471 if (format instanceof LocaleAware )
@@ -481,9 +486,9 @@ public <UNIT extends TimeUnit> TimeFormat removeUnit(final Class<UNIT> unitType)
481486 for (TimeUnit unit : units .keySet ()) {
482487 if (unitType .isAssignableFrom (unit .getClass ()))
483488 {
484- mCachedUnits = null ;
489+ mCachedUnits = null ;
485490
486- return units .remove (unit );
491+ return units .remove (unit );
487492 }
488493 }
489494 return null ;
@@ -499,9 +504,9 @@ public TimeFormat removeUnit(final TimeUnit unit)
499504 if (unit == null )
500505 throw new IllegalArgumentException ("Unit to remove must not be null." );
501506
502- mCachedUnits = null ;
507+ mCachedUnits = null ;
503508
504- return units .remove (unit );
509+ return units .remove (unit );
505510 }
506511
507512 /**
@@ -545,8 +550,8 @@ public List<TimeUnit> clearUnits()
545550 {
546551 List <TimeUnit > result = getUnits ();
547552
548- mCachedUnits = null ;
549- units .clear ();
553+ mCachedUnits = null ;
554+ units .clear ();
550555 return result ;
551556 }
552557
0 commit comments