-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
112 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
postman/src/main/java/com/workday/postman/util/CollectionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.workday.postman.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
|
||
|
||
/** | ||
* Some stuff copied from Google's guava library. | ||
* | ||
* @author Nathan Taylor | ||
* @since 2015-08-04 | ||
*/ | ||
public class CollectionUtils { | ||
|
||
/** | ||
* The largest power of two that can be represented as an {@code int}. | ||
* | ||
* @since 10.0 | ||
*/ | ||
public static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2); | ||
|
||
public static <E> ArrayList<E> newArrayList(E... elements) { | ||
Preconditions.checkNotNull(elements, "elements"); | ||
ArrayList<E> list = new ArrayList<>(elements.length); | ||
Collections.addAll(list, elements); | ||
return list; | ||
} | ||
|
||
@SafeVarargs | ||
public static <E> HashSet<E> newHashSet(E... elements) { | ||
Preconditions.checkNotNull(elements, "elements"); | ||
HashSet<E> set = newHashSetWithExpectedSize(elements.length); | ||
Collections.addAll(set, elements); | ||
return set; | ||
} | ||
|
||
private static <E> HashSet<E> 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). | ||
*/ | ||
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 | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
postman/src/main/java/com/workday/postman/util/Preconditions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.workday.postman.util; | ||
|
||
/** | ||
* @author Nathan Taylor | ||
* @since 2015-08-04 | ||
*/ | ||
public class Preconditions { | ||
|
||
public static void checkState(boolean expression, String message) { | ||
if (!expression) { | ||
throw new IllegalStateException(message); | ||
} | ||
} | ||
|
||
public static void checkArgument(boolean expression, String message) { | ||
if (!expression) { | ||
throw new IllegalArgumentException(message); | ||
} | ||
} | ||
|
||
public static void checkNotNull(Object reference, String message) { | ||
if (reference == null) { | ||
throw new NullPointerException(message); | ||
} | ||
} | ||
} |