test/jdk/java/lang/IntegralPrimitiveToString.java
author jlaskey
Fri, 18 May 2018 08:43:49 -0300
changeset 50175 589ed2770141
parent 47216 71c04702a3d5
permissions -rw-r--r--
8200436: String::isBlank Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17929
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 17929
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
17929
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
     4
 *
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
     8
 *
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    13
 * accompanied this code).
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    14
 *
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    18
 *
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    21
 * questions.
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    22
 */
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    23
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    24
import org.testng.annotations.DataProvider;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    25
import org.testng.annotations.Test;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    26
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    27
import java.math.BigInteger;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    28
import java.util.ArrayList;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    29
import java.util.Iterator;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    30
import java.util.Arrays;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    31
import java.util.List;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    32
import java.util.function.LongFunction;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    33
import java.util.function.Function;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    34
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    35
import static org.testng.Assert.assertEquals;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    36
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    37
/**
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    38
 * @test
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    39
 * @run testng IntegralPrimitiveToString
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    40
 * @summary test string conversions for primitive integral types.
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    41
 * @author Mike Duigou
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    42
 */
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    43
public class IntegralPrimitiveToString {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    44
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    45
    @Test(dataProvider="numbers")
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    46
    public <N extends Number> void testToString(String description,
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    47
        Function<N, BigInteger> converter,
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    48
        Function<N, BigInteger> unsignedConverter,
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    49
        N[] values,
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    50
        Stringifier<N>[] stringifiers) {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    51
        System.out.printf("%s : conversions: %d values: %d\n", description, stringifiers.length, values.length);
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    52
        for( N value : values) {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    53
            BigInteger asBigInt = converter.apply(value);
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    54
            BigInteger asUnsignedBigInt = unsignedConverter.apply(value);
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    55
            for(Stringifier<N> stringifier : stringifiers) {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    56
                stringifier.assertMatchingToString(value, asBigInt, asUnsignedBigInt, description);
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    57
            }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    58
        }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    59
    }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    60
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    61
    static class Stringifier<N extends Number> {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    62
        final boolean signed;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    63
        final int  radix;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    64
        final Function<N,String> toString;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    65
        Stringifier(boolean signed, int radix, Function<N,String> toString) {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    66
            this.signed = signed;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    67
            this.radix = radix;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    68
            this.toString = toString;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    69
        }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    70
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    71
        public void assertMatchingToString(N value, BigInteger asSigned, BigInteger asUnsigned, String description) {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    72
            String expected = signed
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    73
                ? asSigned.toString(radix)
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    74
                : asUnsigned.toString(radix);
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    75
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    76
            String actual = toString.apply(value);
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    77
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    78
            assertEquals(actual, expected, description + " conversion should be the same");
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    79
        }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    80
    }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    81
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    82
    @DataProvider(name="numbers", parallel=true)
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    83
    public Iterator<Object[]> testSetProvider() {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    84
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    85
    return Arrays.asList(
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    86
        new Object[] { "Byte",
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    87
            (Function<Byte,BigInteger>) b -> BigInteger.valueOf((long) b),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    88
            (Function<Byte,BigInteger>) b -> BigInteger.valueOf(Integer.toUnsignedLong((byte) b)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    89
            numberProvider((LongFunction<Byte>) l -> Byte.valueOf((byte) l), Byte.SIZE),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    90
            new Stringifier[] {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    91
                new Stringifier<Byte>(true, 10, b -> b.toString()),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    92
                new Stringifier<Byte>(true, 10, b -> Byte.toString(b))
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    93
            }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    94
        },
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    95
        new Object[] { "Short",
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    96
            (Function<Short,BigInteger>) s -> BigInteger.valueOf((long) s),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    97
            (Function<Short,BigInteger>) s -> BigInteger.valueOf(Integer.toUnsignedLong((short) s)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    98
            numberProvider((LongFunction<Short>) l -> Short.valueOf((short) l), Short.SIZE),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
    99
            new Stringifier[] {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   100
                new Stringifier<Short>(true, 10, s -> s.toString()),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   101
                new Stringifier<Short>(true, 10, s -> Short.toString( s))
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   102
            }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   103
        },
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   104
        new Object[] { "Integer",
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   105
            (Function<Integer,BigInteger>) i -> BigInteger.valueOf((long) i),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   106
            (Function<Integer,BigInteger>) i -> BigInteger.valueOf(Integer.toUnsignedLong(i)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   107
            numberProvider((LongFunction<Integer>) l -> Integer.valueOf((int) l), Integer.SIZE),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   108
            new Stringifier[] {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   109
                new Stringifier<Integer>(true, 10, i -> i.toString()),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   110
                new Stringifier<Integer>(true, 10, i -> Integer.toString(i)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   111
                new Stringifier<Integer>(false, 2, Integer::toBinaryString),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   112
                new Stringifier<Integer>(false, 16, Integer::toHexString),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   113
                new Stringifier<Integer>(false, 8, Integer::toOctalString),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   114
                new Stringifier<Integer>(true, 2, i -> Integer.toString(i, 2)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   115
                new Stringifier<Integer>(true, 8, i -> Integer.toString(i, 8)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   116
                new Stringifier<Integer>(true, 10, i -> Integer.toString(i, 10)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   117
                new Stringifier<Integer>(true, 16, i -> Integer.toString(i, 16)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   118
                new Stringifier<Integer>(true, Character.MAX_RADIX, i -> Integer.toString(i, Character.MAX_RADIX)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   119
                new Stringifier<Integer>(false, 10, i -> Integer.toUnsignedString(i)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   120
                new Stringifier<Integer>(false, 2, i -> Integer.toUnsignedString(i, 2)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   121
                new Stringifier<Integer>(false, 8, i -> Integer.toUnsignedString(i, 8)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   122
                new Stringifier<Integer>(false, 10, i -> Integer.toUnsignedString(i, 10)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   123
                new Stringifier<Integer>(false, 16, i -> Integer.toUnsignedString(i, 16)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   124
                new Stringifier<Integer>(false, Character.MAX_RADIX, i -> Integer.toUnsignedString(i, Character.MAX_RADIX))
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   125
            }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   126
        },
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   127
        new Object[] { "Long",
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   128
            (Function<Long, BigInteger>) BigInteger::valueOf,
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   129
            (Function<Long, BigInteger>) l -> {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   130
                if (l >= 0) {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   131
                    return BigInteger.valueOf((long) l);
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   132
                } else {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   133
                    int upper = (int)(l >>> 32);
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   134
                    int lower = (int) (long) l;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   135
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   136
                    // return (upper << 32) + lower
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   137
                    return (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32).
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   138
                    add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   139
                }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   140
            },
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   141
            numberProvider((LongFunction<Long>) Long::valueOf, Long.SIZE),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   142
            new Stringifier[] {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   143
                new Stringifier<Long>(true, 10, l -> l.toString()),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   144
                new Stringifier<Long>(true, 10, l -> Long.toString(l)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   145
                new Stringifier<Long>(false, 2, Long::toBinaryString),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   146
                new Stringifier<Long>(false, 16, Long::toHexString),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   147
                new Stringifier<Long>(false, 8, Long::toOctalString),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   148
                new Stringifier<Long>(true, 2, l -> Long.toString(l, 2)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   149
                new Stringifier<Long>(true, 8, l -> Long.toString(l, 8)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   150
                new Stringifier<Long>(true, 10, l -> Long.toString(l, 10)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   151
                new Stringifier<Long>(true, 16, l -> Long.toString(l, 16)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   152
                new Stringifier<Long>(true, Character.MAX_RADIX, l -> Long.toString(l, Character.MAX_RADIX)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   153
                new Stringifier<Long>(false, 10, Long::toUnsignedString),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   154
                new Stringifier<Long>(false, 2, l -> Long.toUnsignedString(l, 2)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   155
                new Stringifier<Long>(false, 8, l-> Long.toUnsignedString(l, 8)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   156
                new Stringifier<Long>(false, 10, l -> Long.toUnsignedString(l, 10)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   157
                new Stringifier<Long>(false, 16, l -> Long.toUnsignedString(l, 16)),
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   158
                new Stringifier<Long>(false, Character.MAX_RADIX, l -> Long.toUnsignedString(l, Character.MAX_RADIX))
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   159
            }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   160
        }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   161
        ).iterator();
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   162
    }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   163
    private static final long[] SOME_PRIMES = {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   164
        3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L,
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   165
        59L, 61L, 71L, 73L, 79L, 83L, 89L, 97L, 101L, 103L, 107L, 109L, 113L,
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   166
        5953L, 5981L, 5987L, 6007L, 6011L, 6029L, 6037L, 6043L, 6047L, 6053L,
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   167
        16369L, 16381L, 16411L, 32749L, 32771L, 65521L, 65537L,
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   168
        (long) Integer.MAX_VALUE };
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   169
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   170
    public <N extends Number> N[] numberProvider(LongFunction<N> boxer, int bits, N... extras) {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   171
        List<N> numbers = new ArrayList<>();
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   172
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   173
        for(int bitmag = 0; bitmag < bits; bitmag++) {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   174
            long value = 1L << bitmag;
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   175
            numbers.add(boxer.apply(value));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   176
            numbers.add(boxer.apply(value - 1));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   177
            numbers.add(boxer.apply(value + 1));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   178
            numbers.add(boxer.apply(-value));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   179
            for(int divisor = 0; divisor < SOME_PRIMES.length && value < SOME_PRIMES[divisor]; divisor++) {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   180
                numbers.add(boxer.apply(value - SOME_PRIMES[divisor]));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   181
                numbers.add(boxer.apply(value + SOME_PRIMES[divisor]));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   182
                numbers.add(boxer.apply(value * SOME_PRIMES[divisor]));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   183
                numbers.add(boxer.apply(value / SOME_PRIMES[divisor]));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   184
                numbers.add(boxer.apply(value | SOME_PRIMES[divisor]));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   185
                numbers.add(boxer.apply(value & SOME_PRIMES[divisor]));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   186
                numbers.add(boxer.apply(value ^ SOME_PRIMES[divisor]));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   187
            }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   188
        }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   189
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   190
        numbers.addAll(Arrays.asList(extras));
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   191
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   192
        return (N[]) numbers.toArray(new Number[numbers.size()]);
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   193
    }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
diff changeset
   194
}