001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.util.lang; 018 019import org.slf4j.Logger; 020import org.slf4j.LoggerFactory; 021 022/** 023 * @since 1.5.8 024 */ 025public final class Numbers 026{ 027 private static final Logger LOG = LoggerFactory.getLogger(Numbers.class); 028 029 /** 030 * Prevent instantiation. 031 */ 032 private Numbers() 033 { 034 } 035 036 /** 037 * Returns the minimum value for the numberType's type 038 * 039 * @param numberType 040 * the type of the number for which the minimum value will be returned 041 * @return the minimum value of the numberType or Double if the numberType itself is either 042 * {@code null} or has no minimum value 043 */ 044 public static Number getMinValue(Class<? extends Number> numberType) 045 { 046 Number result; 047 if (Integer.class == numberType || int.class == numberType) 048 { 049 result = Integer.MIN_VALUE; 050 } 051 else if (Long.class == numberType || long.class == numberType) 052 { 053 result = Long.MIN_VALUE; 054 } 055 else if (Float.class == numberType || float.class == numberType) 056 { 057 result = -Float.MAX_VALUE; 058 } 059 else if (Double.class == numberType || double.class == numberType) 060 { 061 result = -Double.MAX_VALUE; 062 } 063 else if (Byte.class == numberType || byte.class == numberType) 064 { 065 result = Byte.MIN_VALUE; 066 } 067 else if (Short.class == numberType || short.class == numberType) 068 { 069 result = Short.MIN_VALUE; 070 } 071 else 072 { // null of any other Number 073 LOG.debug("'{}' has no minimum value. Falling back to Double.", numberType); 074 result = -Double.MAX_VALUE; 075 } 076 077 return result; 078 } 079 080 081 /** 082 * Returns the maximum value for the numberType's type 083 * 084 * @param numberType 085 * the type of the number for which the maximum value will be returned 086 * @return the maximum value of the numberType or {@value Double#MAX_VALUE} if the numberType 087 * itself is either {@code null} or has no maximum value 088 */ 089 public static Number getMaxValue(Class<? extends Number> numberType) 090 { 091 Number result; 092 if (Integer.class == numberType || int.class == numberType) 093 { 094 result = Integer.MAX_VALUE; 095 } 096 else if (Long.class == numberType || long.class == numberType) 097 { 098 result = Long.MAX_VALUE; 099 } 100 else if (Float.class == numberType || float.class == numberType) 101 { 102 result = Float.MAX_VALUE; 103 } 104 else if (Double.class == numberType || double.class == numberType) 105 { 106 result = Double.MAX_VALUE; 107 } 108 else if (Byte.class == numberType || byte.class == numberType) 109 { 110 result = Byte.MAX_VALUE; 111 } 112 else if (Short.class == numberType || short.class == numberType) 113 { 114 result = Short.MAX_VALUE; 115 } 116 else 117 { // null of any other Number 118 LOG.debug("'{}' has no maximum value. Falling back to Double.MAX_VALUE."); 119 result = Double.MAX_VALUE; 120 } 121 122 return result; 123 } 124}