Skip to content

Commit

Permalink
Fix Properties.putProducts()
Browse files Browse the repository at this point in the history
Previously putProducts was storing an array instead of copying the
array.

With Segment specifically this also caused issues with serialization.

```
"properties": {
    "products": "[Lcom.segment.analytics.Properties$Product;@36db3d6"
}
```
  • Loading branch information
f2prateek committed Nov 27, 2016
1 parent ae89184 commit 8649050
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.segment.analytics;

import com.segment.analytics.Properties.Product;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class PropertiesTest {
@Test public void products() {
Product product = new Product("foo", "bar", 10);
Properties properties = new Properties();
properties.putProducts(product);

assertThat(properties.products()).containsExactly(product);
}
}
15 changes: 14 additions & 1 deletion analytics/src/main/java/com/segment/analytics/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

package com.segment.analytics;

import com.segment.analytics.internal.Utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -336,10 +339,20 @@ public Properties putCoupon(String coupon) {
* @see <a href="https://segment.com/docs/api/tracking/ecommerce/">Ecommerce API</a>
*/
public Properties putProducts(Product... products) {
return putValue(PRODUCTS_KEY, products);
if (Utils.isNullOrEmpty(products)) {
throw new IllegalArgumentException("products cannot be null or empty.");
}
List<Product> productList = new ArrayList<>(products.length);
Collections.addAll(productList, products);
return putValue(PRODUCTS_KEY, Collections.unmodifiableList(productList));
}

/** @deprecated Use {@link #products()} instead. */
public List<Product> products(Product... products) {
return products();
}

public List<Product> products() {
return getList(PRODUCTS_KEY, Product.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,16 @@ public static boolean isNullOrEmpty(CharSequence text) {
return TextUtils.isEmpty(text) || TextUtils.getTrimmedLength(text) == 0;
}

/** Returns true if the collection or has a size 0. */
/** Returns true if the collection is null or has a size of 0. */
public static boolean isNullOrEmpty(Collection collection) {
return collection == null || collection.size() == 0;
}

/** Returns true if the array is null or has a size of 0. */
public static <T> boolean isNullOrEmpty(T[] data) {
return data == null || data.length == 0;
}

/** Returns true if the map is null or empty, false otherwise. */
public static boolean isNullOrEmpty(Map map) {
return map == null || map.size() == 0;
Expand Down

0 comments on commit 8649050

Please sign in to comment.