public static < C extends Comparable > C center ( final Range < C > range ) { checkNotNull ( range ) ; if ( ! range . hasLowerBound ( ) && ! range . hasUpperBound ( ) ) { throw new IllegalStateException ( "cannot find the center of a range without bounds" ) ; } if ( ! range . hasLowerBound ( ) ) { return range . upperEndpoint ( ) ; } if ( ! range . hasUpperBound ( ) ) { return range . lowerEndpoint ( ) ; } C lowerEndpoint = range . lowerEndpoint ( ) ; C upperEndpoint = range . upperEndpoint ( ) ; if ( upperEndpoint instanceof Integer ) { Integer upper = ( Integer ) upperEndpoint ; Integer lower = ( Integer ) lowerEndpoint ; return ( C ) Integer . valueOf ( ( upper . intValue ( ) + lower . intValue ( ) ) / 2 ) ; } if ( upperEndpoint instanceof Long ) { Long upper = ( Long ) upperEndpoint ; Long lower = ( Long ) lowerEndpoint ; return ( C ) Long . valueOf ( ( upper . longValue ( ) + lower . longValue ( ) ) / 2L ) ; } if ( upperEndpoint instanceof BigInteger ) { BigInteger upper = ( BigInteger ) upperEndpoint ; BigInteger lower = ( BigInteger ) lowerEndpoint ; BigInteger two = BigInteger . valueOf ( 2L ) ; return ( C ) upper . subtract ( lower ) . divide ( two ) ; } // todo: could potentially calculate the center of any range with a discrete domain throw new IllegalStateException ( "cannot find the center of a range whose endpoint type is not Integer, Long, or BigInteger" ) ; }
Return the center of the specified range .