Last active
April 1, 2016 18:52
-
-
Save AfzalivE/6c813c770aea498610f235001ee065ef to your computer and use it in GitHub Desktop.
Guava methods for todoapp-mvp
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
/* | |
* Copyright (C) 2007 The Guava Authors | |
* | |
* 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 | |
* | |
* 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. | |
*/ | |
package com.example.android.architecture.blueprints.todoapp.util; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import android.support.annotation.Nullable; | |
import android.support.annotation.VisibleForTesting; | |
/** | |
* Class containing most of Guava methods | |
* that this app depended on | |
*/ | |
public class GuavaUtils { | |
/** | |
* Ensures that an object reference passed as a parameter to the calling | |
* method is not null. | |
* | |
* @param reference an object reference | |
* @return the non-null reference that was validated | |
* @throws NullPointerException if {@code reference} is null | |
*/ | |
public static <T> T checkNotNull(T reference) { | |
if (reference == null) { | |
throw new NullPointerException(); | |
} | |
return reference; | |
} | |
/** | |
* Ensures that an object reference passed as a parameter to the calling | |
* method is not null. | |
* | |
* @param reference an object reference | |
* @param errorMessage the exception message to use if the check fails; will | |
* be converted to a string using {@link String#valueOf(Object)} | |
* @return the non-null reference that was validated | |
* @throws NullPointerException if {@code reference} is null | |
*/ | |
public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) { | |
if (reference == null) { | |
throw new NullPointerException(String.valueOf(errorMessage)); | |
} | |
return reference; | |
} | |
/** | |
* Ensures the truth of an expression involving one or more parameters to the | |
* calling method. | |
* | |
* @param expression a boolean expression | |
* @throws IllegalArgumentException if {@code expression} is false | |
*/ | |
public static void checkArgument(boolean expression) { | |
if (!expression) { | |
throw new IllegalArgumentException(); | |
} | |
} | |
/** | |
* Ensures the truth of an expression involving one or more parameters to the | |
* calling method. | |
* | |
* @param expression a boolean expression | |
* @param errorMessage the exception message to use if the check fails; will | |
* be converted to a string using {@link String#valueOf(Object)} | |
* @throws IllegalArgumentException if {@code expression} is false | |
*/ | |
public static void checkArgument(boolean expression, Object errorMessage) { | |
if (!expression) { | |
throw new IllegalArgumentException(String.valueOf(errorMessage)); | |
} | |
} | |
/** | |
* Generates a hash code for multiple values. The hash code is generated by | |
* calling {@link Arrays#hashCode(Object[])}. | |
* | |
* <p>This is useful for implementing {@link Object#hashCode()}. For example, | |
* in an object that has three properties, {@code x}, {@code y}, and | |
* {@code z}, one could write: | |
* <pre> | |
* public int hashCode() { | |
* return Objects.hashCode(getX(), getY(), getZ()); | |
* }</pre> | |
* | |
* <b>Warning</b>: When a single object is supplied, the returned hash code | |
* does not equal the hash code of that object. | |
*/ | |
public static int hashCode(@Nullable Object... objects) { | |
return Arrays.hashCode(objects); | |
} | |
/** | |
* Determines whether two possibly-null objects are equal. Returns: | |
* | |
* <ul> | |
* <li>{@code true} if {@code a} and {@code b} are both null. | |
* <li>{@code true} if {@code a} and {@code b} are both non-null and they are | |
* equal according to {@link Object#equals(Object)}. | |
* <li>{@code false} in all other situations. | |
* </ul> | |
* | |
* <p>This assumes that any non-null objects passed to this function conform | |
* to the {@code equals()} contract. | |
*/ | |
public static boolean equal(@Nullable Object a, @Nullable Object b) { | |
return a == b || (a != null && a.equals(b)); | |
} | |
/** | |
* Creates a <i>mutable</i> {@code ArrayList} instance containing the given | |
* elements. | |
* | |
* <p><b>Note:</b> if mutability is not required and the elements are | |
* non-null, use an overload of {@link ImmutableList#of()} (for varargs) or | |
* {@link ImmutableList#copyOf(Object[])} (for an array) instead. | |
* | |
* @param elements the elements that the list should contain, in order | |
* @return a new {@code ArrayList} containing those elements | |
*/ | |
public static <E> ArrayList<E> newArrayList(E... elements) { | |
checkNotNull(elements); | |
// Avoid integer overflow when a large array is passed in | |
int capacity = computeArrayListCapacity(elements.length); | |
ArrayList<E> list = new ArrayList<E>(capacity); | |
Collections.addAll(list, elements); | |
return list; | |
} | |
/** | |
* Creates a <i>mutable</i> {@code ArrayList} instance containing the given | |
* elements. | |
* | |
* <p><b>Note:</b> if mutability is not required and the elements are | |
* non-null, use {@link ImmutableList#copyOf(Iterator)} instead. | |
* | |
* @param elements the elements that the list should contain, in order | |
* @return a new {@code ArrayList} containing those elements | |
*/ | |
public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) { | |
checkNotNull(elements); // for GWT | |
// Let ArrayList's sizing logic work, if possible | |
return (elements instanceof Collection) | |
? new ArrayList<E>(cast(elements)) | |
: newArrayList(elements.iterator()); | |
} | |
/** | |
* Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557 | |
*/ | |
static <T> Collection<T> cast(Iterable<T> iterable) { | |
return (Collection<T>) iterable; | |
} | |
/** | |
* Creates a <i>mutable</i> {@code ArrayList} instance containing the given | |
* elements. | |
* | |
* <p><b>Note:</b> if mutability is not required and the elements are | |
* non-null, use {@link ImmutableList#copyOf(Iterator)} instead. | |
* | |
* @param elements the elements that the list should contain, in order | |
* @return a new {@code ArrayList} containing those elements | |
*/ | |
public static <E> ArrayList<E> newArrayList(Iterator<? extends E> elements) { | |
checkNotNull(elements); | |
ArrayList<E> list = newArrayList(); | |
while (elements.hasNext()) { | |
list.add(elements.next()); | |
} | |
return list; | |
} | |
@VisibleForTesting | |
static int computeArrayListCapacity(int arraySize) { | |
checkArgument(arraySize >= 0); | |
return saturatedCast(5L + arraySize + (arraySize / 10)); | |
} | |
/** | |
* Returns the {@code int} nearest in value to {@code value}. | |
* | |
* @param value any {@code long} value | |
* @return the same value cast to {@code int} if it is in the range of the | |
* {@code int} type, {@link Integer#MAX_VALUE} if it is too large, | |
* or {@link Integer#MIN_VALUE} if it is too small | |
*/ | |
public static int saturatedCast(long value) { | |
if (value > Integer.MAX_VALUE) { | |
return Integer.MAX_VALUE; | |
} | |
if (value < Integer.MIN_VALUE) { | |
return Integer.MIN_VALUE; | |
} | |
return (int) value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment