Implement equality for ReadableNativeMap

Summary: This diff implements equality for ReadableNativeMap, the underlying implementation relies on the method HashMap.equals()

Reviewed By: kathryngray

Differential Revision: D14019065

fbshipit-source-id: aeaec22ce1066241ed85f0527f5cd804e3c763dd
This commit is contained in:
David Vacca 2019-02-17 11:03:13 -08:00 коммит произвёл Facebook Github Bot
Родитель a8703fe10b
Коммит f4536422d6
3 изменённых файлов: 89 добавлений и 1 удалений

Просмотреть файл

@ -21,7 +21,9 @@ import com.facebook.react.bridge.ReadableNativeMap;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.UnexpectedNativeTypeException;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.modules.appstate.AppStateModule;
import com.facebook.react.modules.deviceinfo.DeviceInfoModule;
@ -476,6 +478,78 @@ public class CatalystNativeJSToJavaParametersTestCase extends ReactIntegrationTe
assertEquals("newvalue", dest.getString("newkey"));
}
public void testEqualityMapAfterMerge() {
mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnMapForMerge1();
waitForBridgeAndUIIdle();
List<ReadableMap> maps = mRecordingTestModule.getMapCalls();
assertEquals(1, maps.size());
WritableMap map1 = new WritableNativeMap();
map1.merge(maps.get(0));
WritableMap map2 = new WritableNativeMap();
map2.merge(maps.get(0));
assertTrue(map1.equals(map2));
}
public void testWritableNativeMapEquals() {
WritableMap map1 = new WritableNativeMap();
WritableMap map2 = new WritableNativeMap();
map1.putInt("key1", 123);
map2.putInt("key1", 123);
map1.putString("key2", "value");
map2.putString("key2", "value");
assertTrue(map1.equals(map2));
}
public void testWritableNativeMapArraysEquals() {
WritableMap map1 = new WritableNativeMap();
WritableMap map2 = new WritableNativeMap();
map1.putInt("key1", 123);
map2.putInt("key1", 123);
map1.putString("key2", "value");
map2.putString("key2", "value");
WritableArray array1 = new WritableNativeArray();
array1.pushInt(321);
array1.pushNull();
array1.pushString("test");
map1.putArray("key3", array1);
WritableArray array2 = new WritableNativeArray();
array1.pushInt(321);
array1.pushNull();
array1.pushString("test");
map2.putArray("key3", array2);
assertTrue(map1.equals(map2));
}
public void testWritableNativeMapArraysNonEquals() {
WritableMap map1 = new WritableNativeMap();
WritableMap map2 = new WritableNativeMap();
map1.putInt("key1", 123);
map2.putInt("key1", 123);
map1.putString("key2", "value");
map2.putString("key2", "value");
WritableArray array1 = new WritableNativeArray();
array1.pushInt(321);
array1.pushNull();
array1.pushString("test");
map1.putArray("key3", array1);
WritableArray array2 = new WritableNativeArray();
array1.pushNull();
array1.pushString("test");
map2.putArray("key3", array2);
assertTrue(map1.equals(map2));
}
public void testMapAccessibleAfterMerge() {
mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnMapForMerge1();
mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnMapForMerge2();

Просмотреть файл

@ -236,6 +236,20 @@ public class ReadableNativeMap extends NativeMap implements ReadableMap {
return new ReadableNativeMapKeySetIterator(this);
}
@Override
public int hashCode() {
return getLocalMap().hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ReadableNativeMap)) {
return false;
}
ReadableNativeMap other = (ReadableNativeMap) obj;
return getLocalMap().equals(other.getLocalMap());
}
@Override
public @Nonnull HashMap<String, Object> toHashMap() {
if (ReactFeatureFlags.useMapNativeAccessor) {

Просмотреть файл

@ -251,7 +251,7 @@ public class MountingManager {
&& newLocalData.hasKey("hash")
&& viewState.mCurrentLocalData.getDouble("hash") == newLocalData.getDouble("hash")
&& viewState.mCurrentLocalData.toString().equals(newLocalData.toString())) {
// TODO: T31905686 implement a proper equality method
// TODO: T31905686 implement a proper equality method
return;
}
viewState.mCurrentLocalData = newLocalData;