diff --git a/demo/src/test/java/com/workday/postman/demo/MyParcelableTest.java b/demo/src/test/java/com/workday/postman/demo/MyParcelableTest.java index c0b9d56..6595282 100644 --- a/demo/src/test/java/com/workday/postman/demo/MyParcelableTest.java +++ b/demo/src/test/java/com/workday/postman/demo/MyParcelableTest.java @@ -9,10 +9,11 @@ import android.os.Parcel; import android.os.Parcelable; -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; + import com.workday.postman.Postman; import com.workday.postman.PostmanException; +import com.workday.postman.util.CollectionUtils; + import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; @@ -48,7 +49,8 @@ public void testParcelable() { in.myChildParcelable.aBoolean = true; in.myChildParcelable.aString = "Robby"; in.myChildParcelable.notParceled = "I shouldn't be retained."; - in.myParcelableList = Lists.newArrayList(new MyChildParcelable("a", false), new MyChildParcelable("b", true)); + in.myParcelableList = CollectionUtils.newArrayList(new MyChildParcelable("a", false), + new MyChildParcelable("b", true)); in.myStringMap = new HashMap<>(); in.myStringMap.put("one key", "one value"); in.myStringMap.put("two key", "two value"); @@ -77,7 +79,7 @@ public void testParcelable() { @Test public void testStringArrayList() { MyParcelable in = new MyParcelable(); - ArrayList stringList = Lists.newArrayList("one", "two"); + ArrayList stringList = CollectionUtils.newArrayList("one", "two"); in.myStringList = stringList; MyParcelable out = writeAndReadParcelable(in); @@ -88,7 +90,7 @@ public void testStringArrayList() { @Test public void testCharSequenceArrayList() { MyParcelable in = new MyParcelable(); - ArrayList charSequenceList = Lists. newArrayList("one", "two"); + ArrayList charSequenceList = CollectionUtils. newArrayList("one", "two"); in.myCharSequenceList = charSequenceList; MyParcelable out = writeAndReadParcelable(in); @@ -99,7 +101,7 @@ public void testCharSequenceArrayList() { @Test public void testSet() { MyParcelable in = new MyParcelable(); - Set set = Sets.newHashSet(1, 2, 3); + Set set = CollectionUtils.newHashSet(1, 2, 3); in.myIntegerSet = set; MyParcelable out = writeAndReadParcelable(in); @@ -153,7 +155,7 @@ public void testNonParceledClassThrowsPostmanException() { public void testParcelableWithPostCreateAction() { MyParcelableWithPostCreateAction in = new MyParcelableWithPostCreateAction(); in.myChildParcelable = new MyChildParcelable("child", false); - in.myChildren = Lists.newArrayList(new MyChildParcelable("list child", false)); + in.myChildren = CollectionUtils.newArrayList(new MyChildParcelable("list child", false)); in.mySerializable = new MySerializable(); in.myMap = new HashMap<>(); in.myMap.put(new MyChildParcelable("key", false), new MyChildParcelable("value", false)); diff --git a/postman/build.gradle b/postman/build.gradle index 7fa4e4b..2d4d679 100644 --- a/postman/build.gradle +++ b/postman/build.gradle @@ -31,7 +31,6 @@ idea { dependencies { provided 'com.google.android:android:2.3.1' - compile 'com.google.guava:guava:18.0' compile 'com.squareup:javawriter:2.5.0' compile 'org.apache.commons:commons-lang3:3.3.2' compile 'com.workday:metajava:1.0' diff --git a/postman/src/main/java/com/workday/postman/Postman.java b/postman/src/main/java/com/workday/postman/Postman.java index a829f1a..46b2089 100644 --- a/postman/src/main/java/com/workday/postman/Postman.java +++ b/postman/src/main/java/com/workday/postman/Postman.java @@ -10,12 +10,12 @@ import android.os.Parcel; import android.os.Parcelable; import android.os.Parcelable.Creator; -import com.google.common.collect.Maps; import com.workday.meta.ConcreteTypeNames; import com.workday.postman.codegen.Names; import com.workday.postman.annotations.Parceled; import com.workday.postman.parceler.Parceler; +import java.util.HashMap; import java.util.Map; /** @@ -28,8 +28,8 @@ */ public class Postman { - private static Map, Parceler> parcelerMap = Maps.newHashMap(); - private static Map, Parcelable.Creator> creatorMap = Maps.newHashMap(); + private static Map, Parceler> parcelerMap = new HashMap<>(); + private static Map, Parcelable.Creator> creatorMap = new HashMap<>(); /** * Write the specified Object to a {@link Parcel}. diff --git a/postman/src/main/java/com/workday/postman/codegen/BoxableSaveStatementWriter.java b/postman/src/main/java/com/workday/postman/codegen/BoxableSaveStatementWriter.java index 5b3887a..d65df5a 100644 --- a/postman/src/main/java/com/workday/postman/codegen/BoxableSaveStatementWriter.java +++ b/postman/src/main/java/com/workday/postman/codegen/BoxableSaveStatementWriter.java @@ -9,6 +9,7 @@ import com.squareup.javawriter.JavaWriter; import com.workday.meta.MetaTypes; + import org.apache.commons.lang3.StringUtils; import javax.lang.model.element.ExecutableElement; diff --git a/postman/src/main/java/com/workday/postman/codegen/ParcelerGenerator.java b/postman/src/main/java/com/workday/postman/codegen/ParcelerGenerator.java index c8f35e5..6b32632 100644 --- a/postman/src/main/java/com/workday/postman/codegen/ParcelerGenerator.java +++ b/postman/src/main/java/com/workday/postman/codegen/ParcelerGenerator.java @@ -7,7 +7,6 @@ package com.workday.postman.codegen; -import com.google.common.collect.Lists; import com.squareup.javawriter.JavaWriter; import com.workday.meta.MetaTypeNames; import com.workday.meta.MetaTypes; @@ -147,7 +146,7 @@ private com.workday.postman.codegen.SaveStatementWriter getSaveStatementWriter(V } private List createSaveStatementWriterList() { - List list = Lists.newArrayList(); + List list = new ArrayList<>(); list.add(new BoxableSaveStatementWriter(metaTypes)); list.add(new StringSaveStatementWriter(metaTypes)); list.add(new CharSequenceSaveStatementWriter(metaTypes)); diff --git a/postman/src/main/java/com/workday/postman/codegen/PostmanProcessor.java b/postman/src/main/java/com/workday/postman/codegen/PostmanProcessor.java index dfc0713..160d4f2 100644 --- a/postman/src/main/java/com/workday/postman/codegen/PostmanProcessor.java +++ b/postman/src/main/java/com/workday/postman/codegen/PostmanProcessor.java @@ -7,11 +7,11 @@ package com.workday.postman.codegen; -import com.google.common.collect.Sets; import com.workday.postman.annotations.NotParceled; import com.workday.postman.annotations.Parceled; import com.workday.postman.annotations.PostCreateChild; import com.workday.postman.parceler.Parceler; +import com.workday.postman.util.CollectionUtils; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.Processor; @@ -68,8 +68,9 @@ public boolean process(Set annotations, RoundEnvironment @Override public Set getSupportedAnnotationTypes() { - return Sets.newHashSet(Parceled.class.getCanonicalName(), NotParceled.class.getCanonicalName(), - PostCreateChild.class.getCanonicalName()); + return CollectionUtils.newHashSet(Parceled.class.getCanonicalName(), + NotParceled.class.getCanonicalName(), + PostCreateChild.class.getCanonicalName()); } @Override diff --git a/postman/src/main/java/com/workday/postman/parceler/ArrayListBundler.java b/postman/src/main/java/com/workday/postman/parceler/ArrayListBundler.java index c46a417..8c9bb40 100644 --- a/postman/src/main/java/com/workday/postman/parceler/ArrayListBundler.java +++ b/postman/src/main/java/com/workday/postman/parceler/ArrayListBundler.java @@ -9,9 +9,11 @@ import android.os.Bundle; import android.os.Parcelable; -import com.google.common.base.Preconditions; + +import com.workday.postman.util.Preconditions; import java.util.ArrayList; +import java.util.Locale; /** * @author nathan.taylor @@ -93,8 +95,8 @@ private static InnerListBundler getListBundlerForItemClass(Class clazz innerListBundler = (InnerListBundler) PARCELABLE_LIST_BUNDLER; } Preconditions.checkArgument(innerListBundler != null, - String.format("Postman cannot bundle lists containing items of type %s", - clazz.getName())); + String.format(Locale.US, "Postman cannot bundle lists containing items of type %s", + clazz.getName())); return innerListBundler; } diff --git a/postman/src/main/java/com/workday/postman/parceler/MapBundler.java b/postman/src/main/java/com/workday/postman/parceler/MapBundler.java index 98a010f..54c6118 100644 --- a/postman/src/main/java/com/workday/postman/parceler/MapBundler.java +++ b/postman/src/main/java/com/workday/postman/parceler/MapBundler.java @@ -9,9 +9,11 @@ import android.os.Bundle; import android.os.Parcelable; -import com.google.common.base.Preconditions; + +import com.workday.postman.util.Preconditions; import java.util.ArrayList; +import java.util.Locale; import java.util.Map; /** @@ -77,8 +79,8 @@ public static void readMapFromBundle(Map map, Bundle bundle, Class< } Preconditions.checkState(keys.size() == values.size(), - String.format("Expected keys.size() (%d) and values.size() (%d) to be the same.", - keys.size(), values.size()) + String.format(Locale.US, "Expected keys.size() (%d) and values.size() (%d) to be the same.", + keys.size(), values.size()) ); for (int i = 0; i < keys.size(); i++) { diff --git a/postman/src/main/java/com/workday/postman/util/CollectionUtils.java b/postman/src/main/java/com/workday/postman/util/CollectionUtils.java new file mode 100644 index 0000000..a7e3e10 --- /dev/null +++ b/postman/src/main/java/com/workday/postman/util/CollectionUtils.java @@ -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 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). + */ + 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 + } +} diff --git a/postman/src/main/java/com/workday/postman/util/Preconditions.java b/postman/src/main/java/com/workday/postman/util/Preconditions.java new file mode 100644 index 0000000..f65f57a --- /dev/null +++ b/postman/src/main/java/com/workday/postman/util/Preconditions.java @@ -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); + } + } +}