All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.netty.util.internal.MathUtil Maven / Gradle / Ivy

Go to download

This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up with different versions on classes on the class path).

There is a newer version: 34.0.0.Final
Show newest version
/*
 * Copyright 2015 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package io.netty.util.internal;

/**
 * Math utility methods.
 */
public final class MathUtil {

    private MathUtil() {
    }

    /**
     * Fast method of finding the next power of 2 greater than or equal to the supplied value.
     *
     * 

If the value is {@code <= 0} then 1 will be returned. * This method is not suitable for {@link Integer#MIN_VALUE} or numbers greater than 2^30. * * @param value from which to search for next power of 2 * @return The next power of 2 or the value itself if it is a power of 2 */ public static int findNextPositivePowerOfTwo(final int value) { assert value > Integer.MIN_VALUE && value < 0x40000000; return 1 << (32 - Integer.numberOfLeadingZeros(value - 1)); } /** * Fast method of finding the next power of 2 greater than or equal to the supplied value. *

This method will do runtime bounds checking and call {@link #findNextPositivePowerOfTwo(int)} if within a * valid range. * @param value from which to search for next power of 2 * @return The next power of 2 or the value itself if it is a power of 2. *

Special cases for return values are as follows: *

    *
  • {@code <= 0} -> 1
  • *
  • {@code >= 2^30} -> 2^30
  • *
*/ public static int safeFindNextPositivePowerOfTwo(final int value) { return value <= 0 ? 1 : value >= 0x40000000 ? 0x40000000 : findNextPositivePowerOfTwo(value); } /** * Determine if the requested {@code index} and {@code length} will fit within {@code capacity}. * @param index The starting index. * @param length The length which will be utilized (starting from {@code index}). * @param capacity The capacity that {@code index + length} is allowed to be within. * @return {@code false} if the requested {@code index} and {@code length} will fit within {@code capacity}. * {@code true} if this would result in an index out of bounds exception. */ public static boolean isOutOfBounds(int index, int length, int capacity) { return (index | length | capacity | (index + length) | (capacity - (index + length))) < 0; } /** * Compares two {@code int} values. * * @param x the first {@code int} to compare * @param y the second {@code int} to compare * @return the value {@code 0} if {@code x == y}; * {@code -1} if {@code x < y}; and * {@code 1} if {@code x > y} */ public static int compare(final int x, final int y) { // do not subtract for comparison, it could overflow return x < y ? -1 : (x > y ? 1 : 0); } /** * Compare two {@code long} values. * @param x the first {@code long} to compare. * @param y the second {@code long} to compare. * @return *
    *
  • 0 if {@code x == y}
  • *
  • {@code > 0} if {@code x > y}
  • *
  • {@code < 0} if {@code x < y}
  • *
*/ public static int compare(long x, long y) { return (x < y) ? -1 : (x > y) ? 1 : 0; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy