@@ -295,33 +295,15 @@ public ChronoUnit getUnit() {
295295        }
296296    }
297297
298-     /** 
299-      * Formats the given estimated temporal amount to a string. 
300-      * <p> 
301-      *     Examples: 
302-      * </p> 
303-      * <ul> 
304-      *     <li>Duration of 30 seconds: {@code 30s}</li> 
305-      *     <li>Duration of 25 hours: {@code 1d1h}</li> 
306-      *     <li>Duration of 1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes and 7 seconds: {@code 1y2mo3w4d5h6m7s}</li> 
307-      *     <li>Duration of 1 hours and 61 minutes: {@code 2h1m}</li> 
308-      *     <li>Past duration of 1 hours and 61 minutes: {@code -2h1m}</li> 
309-      *     <li>Period of 1 year, 2 months, 4 days: {@code 1y2mo4d}</li> 
310-      *     <li>Past period of 1 year, 2 months, 4 days: {@code -1y2mo4d}</li> 
311-      * </ul> 
312-      * 
313-      * @param temporalAmount the temporal amount to format. Must not be null. 
314-      * @return the formatted string 
315-      */ 
316-     public  String  format (T  temporalAmount ) {
317-         StringBuilder  builder  = new  StringBuilder ();
298+     public  TimeResult  prepare (T  temporalAmount ) {
299+         List <TimePart > parts  = new  ArrayList <>();
318300        Duration  duration  = this .toDuration (this .baseForTimeEstimation , temporalAmount );
319301        for  (TimeModifier  modifier  : this .modifiers ) {
320302            duration  = modifier .modify (duration );
321303        }
322304
305+         boolean  isNegative  = duration .isNegative ();
323306        if  (duration .isNegative ()) {
324-             builder .append ('-' );
325307            duration  = duration .negated ();
326308        }
327309
@@ -345,18 +327,52 @@ public String format(T temporalAmount) {
345327
346328            BigInteger  nanosCountCleared  = count .multiply (nanosInOneUnit );
347329
348-             builder . append ( count ). append ( key );
330+             parts . add ( new   TimePart ( count ,  key ,  unit ) );
349331            duration  = duration .minusNanos (nanosCountCleared .longValue ());
350332        }
351333
352-         String  result  = builder .toString ();
334+         return  new  TimeResult (parts , isNegative );
335+     }
353336
354-         if  (result .isEmpty ()) {
337+     /** 
338+      * Formats the given estimated temporal amount to a string. 
339+      * <p> 
340+      *     Examples: 
341+      * </p> 
342+      * <ul> 
343+      *     <li>Duration of 30 seconds: {@code 30s}</li> 
344+      *     <li>Duration of 25 hours: {@code 1d1h}</li> 
345+      *     <li>Duration of 1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes and 7 seconds: {@code 1y2mo3w4d5h6m7s}</li> 
346+      *     <li>Duration of 1 hours and 61 minutes: {@code 2h1m}</li> 
347+      *     <li>Past duration of 1 hours and 61 minutes: {@code -2h1m}</li> 
348+      *     <li>Period of 1 year, 2 months, 4 days: {@code 1y2mo4d}</li> 
349+      *     <li>Past period of 1 year, 2 months, 4 days: {@code -1y2mo4d}</li> 
350+      * </ul> 
351+      * 
352+      * @param temporalAmount the temporal amount to format. Must not be null. 
353+      * @return the formatted string 
354+      */ 
355+     public  String  format (T  temporalAmount ) {
356+         TimeResult  result  = this .prepare (temporalAmount );
357+         if  (result .parts ().isEmpty ()) {
355358            String  defaultSymbol  = this .defaultZeroSymbol .get ();
356359            return  "0"  + defaultSymbol ;
357360        }
358361
359-         return  result ;
362+         StringBuilder  builder  = new  StringBuilder ();
363+         if  (result .isNegative ()) {
364+             builder .append ('-' );
365+         }
366+ 
367+         for  (TimePart  part  : result .parts ()) {
368+             if  (part .count ().equals (BigInteger .ZERO )) {
369+                 continue ;
370+             }
371+ 
372+             builder .append (part .count ()).append (part .name ());
373+         }
374+ 
375+         return  builder .toString ();
360376    }
361377
362378    protected  abstract  Duration  toDuration (LocalDateTimeProvider  baseForTimeEstimation , T  temporalAmount );
0 commit comments