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

com.workday.autoparse.json.utils.CollectionUtils Maven / Gradle / Ivy

Go to download

A java library built specifically for Android that uses code generation to parse JSON into custom objects in your project.

There is a newer version: 0.8
Show newest version
/*
 * Copyright 2016 Workday, Inc.
 *
 * This software is available under the MIT license.
 * Please see the LICENSE.txt file in this project.
 */

package com.workday.autoparse.json.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

/**
 * @author nathan.taylor
 * @since 2016-04-07.
 */
public class CollectionUtils {

    /**
     * The largest power of two that can be represented as an {@code int}.
     */
    public static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2);

    private CollectionUtils() {
    }

    @SafeVarargs
    public static  ArrayList newArrayList(E... elements) {
        Preconditions.checkNotNull(elements, "elements");
        ArrayList list = new ArrayList<>(elements.length);
        Collections.addAll(list, elements);
        return list;
    }

    @SafeVarargs
    public static  HashSet newHashSet(E... elements) {
        Preconditions.checkNotNull(elements, "elements");
        HashSet set = newHashSetWithExpectedSize(elements.length);
        Collections.addAll(set, elements);
        return set;
    }

    private static  HashSet newHashSetWithExpectedSize(int expectedSize) {
        return new HashSet<>(mapCapacity(expectedSize));
    }

    /**
     * Returns a capacity that is sufficient to keep the map from being resized as long as it grows no larger than
     * expectedSize and the load factor is >= its default (0.75).
     */
    private static int mapCapacity(int expectedSize) {
        if (expectedSize < 3) {
            Preconditions.checkArgument(expectedSize >= 0, "Size must be nonnegative but was " + expectedSize);
            return expectedSize + 1;
        }
        if (expectedSize < MAX_POWER_OF_TWO) {
            return expectedSize + expectedSize / 3;
        }
        return Integer.MAX_VALUE; // any large value
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy