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     */
017    package org.apache.logging.log4j.core.util;
018    
019    import org.apache.logging.log4j.util.Strings;
020    
021    /**
022     * Helps deal with integers.
023     */
024    public final class Integers {
025    
026        private Integers() {
027        }
028    
029        /**
030         * Parses the string argument as a signed decimal integer.
031         *
032         * @param s
033         *            a {@code String} containing the {@code int} representation to parse, may be {@code null} or {@code ""}
034         * @param defaultValue
035         *            the return value, use {@code defaultValue} if {@code s} is {@code null} or {@code ""}
036         * @return the integer value represented by the argument in decimal.
037         * @throws NumberFormatException
038         *             if the string does not contain a parsable integer.
039         */
040        public static int parseInt(final String s, final int defaultValue) {
041            return Strings.isEmpty(s) ? defaultValue : Integer.parseInt(s);
042        }
043    
044        /**
045         * Parses the string argument as a signed decimal integer.
046         *
047         * @param s
048         *            a {@code String} containing the {@code int} representation to parse, may be {@code null} or {@code ""}
049         * @return the integer value represented by the argument in decimal.
050         * @throws NumberFormatException
051         *             if the string does not contain a parsable integer.
052         */
053        public static int parseInt(final String s) {
054            return parseInt(s, 0);
055        }
056        
057        /**
058         * Calculate the next power of 2, greater than or equal to x.<p>
059         * From Hacker's Delight, Chapter 3, Harry S. Warren Jr.
060         *
061         * @param x Value to round up
062         * @return The next power of 2 from x inclusive
063         */
064        public static int ceilingNextPowerOfTwo(final int x) {
065            return 1 << (32 - Integer.numberOfLeadingZeros(x - 1));
066        }
067    }