//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.util;
public class MathUtils
{
private MathUtils()
{
}
Returns whether the sum of the arguments overflows an int
. Params: - a – the first value
- b – the second value
Returns: whether the sum of the arguments overflows an int
/**
* Returns whether the sum of the arguments overflows an {@code int}.
*
* @param a the first value
* @param b the second value
* @return whether the sum of the arguments overflows an {@code int}
*/
public static boolean sumOverflows(int a, int b)
{
try
{
Math.addExact(a, b);
return false;
}
catch (ArithmeticException x)
{
return true;
}
}
Returns the sum of its arguments, capping to Long.MAX_VALUE
if they overflow. Params: - a – the first value
- b – the second value
Returns: the sum of the values, capped to Long.MAX_VALUE
/**
* Returns the sum of its arguments, capping to {@link Long#MAX_VALUE} if they overflow.
*
* @param a the first value
* @param b the second value
* @return the sum of the values, capped to {@link Long#MAX_VALUE}
*/
public static long cappedAdd(long a, long b)
{
try
{
return Math.addExact(a, b);
}
catch (ArithmeticException x)
{
return Long.MAX_VALUE;
}
}
}