mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-02-13 22:29:45 +08:00
Move SimpleArray and SimpleMap from test to src.
Summary: As a part of this change I'm also renaming SimpleArray to JavaOnlyArray and SimpleMap to JavaOnlyMap. The main reason for the change is to support use-cases such as driving animations form the native code. In the case of native "animated" I'd like to be able to use the same interface as JS is using for updating the View properties. As view setters can take ReadableMap and ReadableArray as an argument in some cases it is necessary to create and pass those types to the setter. Using WritableNativeArray and WritableNativeMap for this purpose seems to me like a misuse and IMO will be less performant (vs java-only map/array) as those implementations of ReadableMap and ReadableArray proxies all their methods through JNI. I'm also adding some additional class-level comments for the moved classes to avoid confusion and hopefuly prevent people from using those classess accidentally while writing native modules or methods that calls to JS. Closes https://github.com/facebook/react-native/pull/5816 Reviewed By: svcscm Differential Revision: D2911339 Pulled By: foghina fb-gh-sync-id: 5b9a98d64f48d8bba34c15e3eecba2151da3577a shipit-source-id: 5b9a98d64f48d8bba34c15e3eecba2151da3577a
This commit is contained in:
committed by
facebook-github-bot-3
parent
47dac4ff50
commit
788e25894e
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Java {@link ArrayList} backed impementation of {@link ReadableArray} and {@link WritableArray}
|
||||
* Instances of this class SHOULD NOT be used for communication between java and JS, use instances
|
||||
* of {@link WritableNativeArray} created via {@link Arguments#createArray} or just
|
||||
* {@link ReadableArray} interface if you want your "native" module method to take an array from JS
|
||||
* as an argument.
|
||||
*
|
||||
* Main purpose for this class is to be used in java-only unit tests, but could also be used outside
|
||||
* of tests in the code that operates only in java and needs to communicate with RN modules via
|
||||
* their JS-exposed API.
|
||||
*/
|
||||
public class JavaOnlyArray implements ReadableArray, WritableArray {
|
||||
|
||||
private final List mBackingList;
|
||||
|
||||
public static JavaOnlyArray from(List list) {
|
||||
return new JavaOnlyArray(list);
|
||||
}
|
||||
|
||||
public static JavaOnlyArray of(Object... values) {
|
||||
return new JavaOnlyArray(values);
|
||||
}
|
||||
|
||||
private JavaOnlyArray(Object... values) {
|
||||
mBackingList = Arrays.asList(values);
|
||||
}
|
||||
|
||||
private JavaOnlyArray(List list) {
|
||||
mBackingList = new ArrayList(list);
|
||||
}
|
||||
|
||||
public JavaOnlyArray() {
|
||||
mBackingList = new ArrayList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return mBackingList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNull(int index) {
|
||||
return mBackingList.get(index) == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(int index) {
|
||||
return (Double) mBackingList.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(int index) {
|
||||
return (Integer) mBackingList.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(int index) {
|
||||
return (String) mBackingList.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaOnlyArray getArray(int index) {
|
||||
return (JavaOnlyArray) mBackingList.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(int index) {
|
||||
return (Boolean) mBackingList.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaOnlyMap getMap(int index) {
|
||||
return (JavaOnlyMap) mBackingList.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableType getType(int index) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pushBoolean(boolean value) {
|
||||
mBackingList.add(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pushDouble(double value) {
|
||||
mBackingList.add(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pushInt(int value) {
|
||||
mBackingList.add(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pushString(String value) {
|
||||
mBackingList.add(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pushArray(WritableArray array) {
|
||||
mBackingList.add(array);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pushMap(WritableMap map) {
|
||||
mBackingList.add(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pushNull() {
|
||||
mBackingList.add(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return mBackingList.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
JavaOnlyArray that = (JavaOnlyArray) o;
|
||||
|
||||
if (mBackingList != null ? !mBackingList.equals(that.mBackingList) : that.mBackingList != null)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return mBackingList != null ? mBackingList.hashCode() : 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Java {@link HashMap} backed impementation of {@link ReadableMap} and {@link WritableMap}
|
||||
* Instances of this class SHOULD NOT be used for communication between java and JS, use instances
|
||||
* of {@link WritableNativeMap} created via {@link Arguments#createMap} or just {@link ReadableMap}
|
||||
* interface if you want your "native" module method to take a map from JS as an argument.
|
||||
*
|
||||
* Main purpose for this class is to be used in java-only unit tests, but could also be used outside
|
||||
* of tests in the code that operates only in java and needs to communicate with RN modules via
|
||||
* their JS-exposed API.
|
||||
*/
|
||||
public class JavaOnlyMap implements ReadableMap, WritableMap {
|
||||
|
||||
private final Map mBackingMap;
|
||||
|
||||
public static JavaOnlyMap of(Object... keysAndValues) {
|
||||
return new JavaOnlyMap(keysAndValues);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param keysAndValues keys and values, interleaved
|
||||
*/
|
||||
private JavaOnlyMap(Object... keysAndValues) {
|
||||
if (keysAndValues.length % 2 != 0) {
|
||||
throw new IllegalArgumentException("You must provide the same number of keys and values");
|
||||
}
|
||||
mBackingMap = new HashMap();
|
||||
for (int i = 0; i < keysAndValues.length; i += 2) {
|
||||
mBackingMap.put(keysAndValues[i], keysAndValues[i + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
public JavaOnlyMap() {
|
||||
mBackingMap = new HashMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasKey(String name) {
|
||||
return mBackingMap.containsKey(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNull(String name) {
|
||||
return mBackingMap.get(name) == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name) {
|
||||
return (Boolean) mBackingMap.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(String name) {
|
||||
return (Double) mBackingMap.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String name) {
|
||||
return (Integer) mBackingMap.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name) {
|
||||
return (String) mBackingMap.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaOnlyMap getMap(String name) {
|
||||
return (JavaOnlyMap) mBackingMap.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaOnlyArray getArray(String name) {
|
||||
return (JavaOnlyArray) mBackingMap.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableType getType(String name) {
|
||||
throw new UnsupportedOperationException("Method not implemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableMapKeySetIterator keySetIterator() {
|
||||
return new ReadableMapKeySetIterator() {
|
||||
Iterator<String> mIterator = mBackingMap.keySet().iterator();
|
||||
|
||||
@Override
|
||||
public boolean hasNextKey() {
|
||||
return mIterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String nextKey() {
|
||||
return mIterator.next();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putBoolean(String key, boolean value) {
|
||||
mBackingMap.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putDouble(String key, double value) {
|
||||
mBackingMap.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putInt(String key, int value) {
|
||||
mBackingMap.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putString(String key, String value) {
|
||||
mBackingMap.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putNull(String key) {
|
||||
mBackingMap.put(key, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putMap(String key, WritableMap value) {
|
||||
mBackingMap.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void merge(ReadableMap source) {
|
||||
mBackingMap.putAll(((JavaOnlyMap) source).mBackingMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putArray(String key, WritableArray value) {
|
||||
mBackingMap.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return mBackingMap.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
JavaOnlyMap that = (JavaOnlyMap) o;
|
||||
|
||||
if (mBackingMap != null ? !mBackingMap.equals(that.mBackingMap) : that.mBackingMap != null)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return mBackingMap != null ? mBackingMap.hashCode() : 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user