private UnitPatterns parseUnitPatterns ( JsonObject node , String width ) { node = resolve ( node , width ) ; UnitPatterns result = new UnitPatterns ( ) ; result . compoundUnitPattern = string ( resolve ( node , "per" ) , "compoundUnitPattern" ) ; result . coordinatePatterns = new HashMap <> ( ) ; JsonObject parent = resolve ( node , "coordinateUnit" ) ; for ( String direction : objectKeys ( parent ) ) { result . coordinatePatterns . put ( direction , parent . get ( direction ) . getAsString ( ) ) ; } node . remove ( "per" ) ; node . remove ( "coordinateUnit" ) ; Set < PluralCategory > categories = new HashSet <> ( ) ; result . unitPatterns = new EnumMap < Unit , UnitData . UnitPattern > ( Unit . class ) ; for ( String unitKey : objectKeys ( node ) ) { JsonObject obj = node . get ( unitKey ) . getAsJsonObject ( ) ; // Special case as "generic" is ambiguous Unit unit = null ; if ( unitKey . equalsIgnoreCase ( "temperature-generic" ) ) { unit = Unit . TEMPERATURE_GENERIC ; } else { int index = unitKey . indexOf ( ' ' ) ; String identifier = unitKey . substring ( index + 1 ) ; unit = Unit . fromIdentifier ( identifier ) ; } if ( unit == null ) { throw new RuntimeException ( "unsupported unit found: " + unitKey ) ; } UnitPattern pattern = new UnitPattern ( ) ; pattern . displayName = string ( obj , "displayName" ) ; pattern . perUnitPattern = string ( obj , "perUnitPattern" , null ) ; pattern . patterns = new HashMap <> ( ) ; for ( String patternKey : objectKeys ( obj ) ) { if ( ! patternKey . startsWith ( "unitPattern-count-" ) ) { continue ; } String value = string ( obj , patternKey ) ; PluralCategory category = PluralCategory . fromString ( patternKey . split ( "-" ) [ 2 ] ) ; categories . add ( category ) ; pattern . patterns . put ( category , value ) ; } result . unitPatterns . put ( unit , pattern ) ; } // Fill in default of OTHER where necessary for ( Unit unit : Unit . values ( ) ) { UnitPattern pattern = result . unitPatterns . get ( unit ) ; String other = pattern . patterns . get ( PluralCategory . OTHER ) ; for ( PluralCategory category : categories ) { if ( pattern . patterns . get ( category ) == null ) { pattern . patterns . put ( category , other ) ; } } } return result ; }
Parse unit patterns of a given width .