Skip to content

Commit

Permalink
Merge pull request #5 from Workday/remove-guava
Browse files Browse the repository at this point in the history
Remove dependency on guava
  • Loading branch information
ndtaylor committed Aug 5, 2015
2 parents b23b271 + 4e2835b commit 60074f6
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -77,7 +79,7 @@ public void testParcelable() {
@Test
public void testStringArrayList() {
MyParcelable in = new MyParcelable();
ArrayList<String> stringList = Lists.newArrayList("one", "two");
ArrayList<String> stringList = CollectionUtils.newArrayList("one", "two");
in.myStringList = stringList;

MyParcelable out = writeAndReadParcelable(in);
Expand All @@ -88,7 +90,7 @@ public void testStringArrayList() {
@Test
public void testCharSequenceArrayList() {
MyParcelable in = new MyParcelable();
ArrayList<CharSequence> charSequenceList = Lists.<CharSequence> newArrayList("one", "two");
ArrayList<CharSequence> charSequenceList = CollectionUtils.<CharSequence> newArrayList("one", "two");
in.myCharSequenceList = charSequenceList;

MyParcelable out = writeAndReadParcelable(in);
Expand All @@ -99,7 +101,7 @@ public void testCharSequenceArrayList() {
@Test
public void testSet() {
MyParcelable in = new MyParcelable();
Set<Integer> set = Sets.newHashSet(1, 2, 3);
Set<Integer> set = CollectionUtils.newHashSet(1, 2, 3);
in.myIntegerSet = set;

MyParcelable out = writeAndReadParcelable(in);
Expand Down Expand Up @@ -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));
Expand Down
1 change: 0 additions & 1 deletion postman/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions postman/src/main/java/com/workday/postman/Postman.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -28,8 +28,8 @@
*/
public class Postman {

private static Map<Class<?>, Parceler<?>> parcelerMap = Maps.newHashMap();
private static Map<Class<?>, Parcelable.Creator<?>> creatorMap = Maps.newHashMap();
private static Map<Class<?>, Parceler<?>> parcelerMap = new HashMap<>();
private static Map<Class<?>, Parcelable.Creator<?>> creatorMap = new HashMap<>();

/**
* Write the specified Object to a {@link Parcel}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,7 +146,7 @@ private com.workday.postman.codegen.SaveStatementWriter getSaveStatementWriter(V
}

private List<com.workday.postman.codegen.SaveStatementWriter> createSaveStatementWriterList() {
List<SaveStatementWriter> list = Lists.newArrayList();
List<SaveStatementWriter> list = new ArrayList<>();
list.add(new BoxableSaveStatementWriter(metaTypes));
list.add(new StringSaveStatementWriter(metaTypes));
list.add(new CharSequenceSaveStatementWriter(metaTypes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,8 +68,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

@Override
public Set<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,8 +95,8 @@ private static <T> InnerListBundler<T> getListBundlerForItemClass(Class<T> clazz
innerListBundler = (InnerListBundler<T>) 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -77,8 +79,8 @@ public static <K, V> void readMapFromBundle(Map<K, V> 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++) {
Expand Down
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 postman/src/main/java/com/workday/postman/util/Preconditions.java
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);
}
}
}

0 comments on commit 60074f6

Please sign in to comment.