private MethodSpec buildIntervalMethod ( Map < String , Map < String , String > > intervalFormats , String fallback ) { MethodSpec . Builder method = MethodSpec . methodBuilder ( "formatInterval" ) . addAnnotation ( Override . class ) . addModifiers ( PUBLIC ) . addParameter ( ZonedDateTime . class , "s" ) . addParameter ( ZonedDateTime . class , "e" ) . addParameter ( String . class , "k" ) . addParameter ( DateTimeField . class , "f" ) . addParameter ( StringBuilder . class , "b" ) ; // Only enter the switches if both params are non-null. method . beginControlFlow ( "if (k != null && f != null)" ) ; // BEGIN switch (k) method . beginControlFlow ( "switch (k)" ) ; for ( Map . Entry < String , Map < String , String > > format : intervalFormats . entrySet ( ) ) { String skeleton = format . getKey ( ) ; // BEGIN "case skeleton:" method . beginControlFlow ( "case $S:" , skeleton ) ; method . beginControlFlow ( "switch (f)" ) ; for ( Map . Entry < String , String > entry : format . getValue ( ) . entrySet ( ) ) { String field = entry . getKey ( ) ; // Split the interval pattern on the boundary. We end up with two patterns, one for // start and end respectively. Pair < List < Node > , List < Node > > patterns = DATETIME_PARSER . splitIntervalPattern ( entry . getValue ( ) ) ; // BEGIN "case field:" // Render this pair of patterns when the given field matches. method . beginControlFlow ( "case $L:" , DateTimeField . fromString ( field ) ) ; method . addComment ( "$S" , DateTimePatternParser . render ( patterns . _1 ) ) ; addIntervalPattern ( method , patterns . _1 , "s" ) ; method . addComment ( "$S" , DateTimePatternParser . render ( patterns . _2 ) ) ; addIntervalPattern ( method , patterns . _2 , "e" ) ; method . addStatement ( "return" ) ; method . endControlFlow ( ) ; // END "case field:" } method . addStatement ( "default: break" ) ; method . endControlFlow ( ) ; // switch (f) method . addStatement ( "break" ) ; method . endControlFlow ( ) ; // END "case skeleton:" } method . addStatement ( "default: break" ) ; method . endControlFlow ( ) ; // END switch (k) // One of the parameters was null, or nothing matched, so render the // fallback, e.g. format the start / end separately as "{0} - {1}" addIntervalFallback ( method , fallback ) ; method . endControlFlow ( ) ; return method . build ( ) ; }
Build methods to format date time intervals using the field of greatest difference .