Repository: altbeacon/android-beacon-library
Issue: Stackoverflow error

After updating the library.
I get Stackoverflow error at java.util.Collections

Checking DDMS shows that java.util.Collections are allocated when in writeToParcel:

```
  at java.util.Collections$UnmodifiableCollection.iterator(Collections.java:952)    
  at java.util.Collections$UnmodifiableCollection$1.<init>(Collections.java:953)    
  at java.util.Collections$UnmodifiableCollection.iterator(Collections.java:952)    
  at java.util.Collections$UnmodifiableCollection$1.<init>(Collections.java:953)    
  at java.util.Collections$UnmodifiableCollection.iterator(Collections.java:952)    
  at java.util.Collections$UnmodifiableCollection$1.<init>(Collections.java:953)    
  at java.util.Collections$UnmodifiableCollection.iterator(Collections.java:952)    
  at org.altbeacon.beacon.Beacon.writeToParcel(Beacon.java:524) 
  at org.altbeacon.beacon.AltBeacon.writeToParcel(AltBeacon.java:115)   
  at android.os.Parcel.writeParcelable(Parcel.java:1285)    
  at android.os.Parcel.writeParcelableArray(Parcel.java:1984)   
  at org.altbeacon.beacon.service.RangingData.writeToParcel(RangingData.java:62)    
  at android.os.Parcel.writeParcelable(Parcel.java:1285)    
  at android.os.Parcel.writeValue(Parcel.java:1204) 
  at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)   
  at android.os.Bundle.writeToParcel(Bundle.java:1692)  
```

It crashed in Emulator, also on Galaxy S5, 4.4.4


Changed files:
src/test/java/org/altbeacon/beacon/BeaconTest.java, src/main/java/org/altbeacon/beacon/Beacon.java

diff --git a/src/main/java/org/altbeacon/beacon/Beacon.java b/src/main/java/org/altbeacon/beacon/Beacon.java
index 7944ae9..6d4dc3c 100644
--- a/src/main/java/org/altbeacon/beacon/Beacon.java
+++ b/src/main/java/org/altbeacon/beacon/Beacon.java
@@ -25,6 +25,7 @@ package org.altbeacon.beacon;
 
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.util.Log;
 
 import org.altbeacon.beacon.client.BeaconDataFactory;
 import org.altbeacon.beacon.client.NullBeaconDataFactory;
@@ -57,6 +58,11 @@ import java.util.List;
 public class Beacon implements Parcelable {
     private static final String TAG = "Beacon";
 
+    private static final List<Long> UNMODIFIABLE_LIST_OF_LONG =
+            Collections.unmodifiableList(new ArrayList<Long>());
+    private static final List<Identifier> UNMODIFIABLE_LIST_OF_IDENTIFIER =
+            Collections.unmodifiableList(new ArrayList<Identifier>());
+
     /**
      * Determines whether a the bluetoothAddress (mac address) must be the same for two Beacons
      * to be configured equal.
@@ -137,7 +143,7 @@ public class Beacon implements Parcelable {
      * beacon, this field will be -1
      */
 
-    protected int mServiceUuid;
+    protected int mServiceUuid = -1;
 
     /**
      * The Bluetooth device name.  This is a field transmitted by the remote beacon device separate
@@ -329,7 +335,12 @@ public class Beacon implements Parcelable {
      * @return dataFields
      */
     public List<Long> getDataFields() {
-        return Collections.unmodifiableList(mDataFields);
+        if (mDataFields.getClass().isInstance(UNMODIFIABLE_LIST_OF_LONG)) {
+            return mDataFields;
+        }
+        else {
+            return Collections.unmodifiableList(mDataFields);
+        }
     }
 
     /**
@@ -337,7 +348,12 @@ public class Beacon implements Parcelable {
      * @return dataFields
      */
     public List<Long> getExtraDataFields() {
-        return Collections.unmodifiableList(mExtraDataFields);
+        if (mExtraDataFields.getClass().isInstance(UNMODIFIABLE_LIST_OF_LONG)) {
+            return mExtraDataFields;
+        }
+        else {
+            return Collections.unmodifiableList(mExtraDataFields);
+        }
     }
 
     /**
@@ -353,7 +369,12 @@ public class Beacon implements Parcelable {
      * @return identifier
      */
     public List<Identifier> getIdentifiers() {
-        return Collections.unmodifiableList(mIdentifiers);
+        if (mIdentifiers.getClass().isInstance(UNMODIFIABLE_LIST_OF_IDENTIFIER)) {
+            return mIdentifiers;
+        }
+        else {
+            return Collections.unmodifiableList(mIdentifiers);
+        }
     }
 
 
@@ -754,4 +775,6 @@ public class Beacon implements Parcelable {
         }
 
     }
+
+
 }
diff --git a/src/test/java/org/altbeacon/beacon/BeaconTest.java b/src/test/java/org/altbeacon/beacon/BeaconTest.java
index 8f78d4f..e77b664 100644
--- a/src/test/java/org/altbeacon/beacon/BeaconTest.java
+++ b/src/test/java/org/altbeacon/beacon/BeaconTest.java
@@ -15,6 +15,8 @@ import org.robolectric.annotation.Config;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
 
 @Config(emulateSdk = 18)
 
@@ -183,5 +185,16 @@ public class BeaconTest {
         assertEquals("data field 0 is the same after deserialization", beacon.getDataFields().get(0), beacon2.getDataFields().get(0));
         assertEquals("data field 0 is the right value", beacon.getDataFields().get(0), (Long) 100l);
     }
+    @Test
+    public void noDoubleWrappingOfExtraDataFields() {
+        org.robolectric.shadows.ShadowLog.stream = System.err;
+        Beacon beacon = new AltBeacon.Builder().setId1("1").setId2("2").setId3("3").setRssi(4)
+                .setBeaconTypeCode(5).setTxPower(6).setBluetoothName("xx")
+                .setBluetoothAddress("1:2:3:4:5:6").setDataFields(Arrays.asList(100l)).build();
+        List<Long> list = beacon.getExtraDataFields();
+        beacon.setExtraDataFields(list);
+        assertTrue("getter should return same object after first wrap ", beacon.getExtraDataFields() == list);
+    }
+
 
 }
\\ No newline at end of file