co.elastic.otel.profiler.collections.CollectionUtil Maven / Gradle / Ivy
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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
*
* http://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.
*/
/*
* Copyright 2014-2020 Real Logic Limited.
*
* Licensed 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 co.elastic.otel.profiler.collections;
/** Utility functions for collection objects. */
public class CollectionUtil {
/**
* Validate that a load factor is in the range of 0.1 to 0.9.
*
* Load factors in the range 0.5 - 0.7 are recommended for open-addressing with linear probing.
*
* @param loadFactor to be validated.
*/
public static void validateLoadFactor(final float loadFactor) {
if (loadFactor < 0.1f || loadFactor > 0.9f) {
throw new IllegalArgumentException(
"load factor must be in the range of 0.1 to 0.9: " + loadFactor);
}
}
/**
* Fast method of finding the next power of 2 greater than or equal to the supplied value.
*
*
If the value is <= 0 then 1 will be returned.
*
*
This method is not suitable for {@link Integer#MIN_VALUE} or numbers greater than 2^30. When
* provided then {@link Integer#MIN_VALUE} will be returned.
*
* @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) {
return 1 << (Integer.SIZE - Integer.numberOfLeadingZeros(value - 1));
}
/**
* Fast method of finding the next power of 2 greater than or equal to the supplied value.
*
*
If the value is <= 0 then 1 will be returned.
*
*
This method is not suitable for {@link Long#MIN_VALUE} or numbers greater than 2^62. When
* provided then {@link Long#MIN_VALUE} will be returned.
*
* @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 long findNextPositivePowerOfTwo(final long value) {
return 1L << (Long.SIZE - Long.numberOfLeadingZeros(value - 1));
}
}