Files
react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromArray.java
Sophie Alpert 1490ab12ef Update license headers for MIT license
Summary:
Includes React Native and its dependencies Fresco, Metro, and Yoga. Excludes samples/examples/docs.

find: ^(?:( *)|( *(?:[\*~#]|::))( )? *)?Copyright (?:\(c\) )?(\d{4})\b.+Facebook[\s\S]+?BSD[\s\S]+?(?:this source tree|the same directory)\.$
replace: $1$2$3Copyright (c) $4-present, Facebook, Inc.\n$2\n$1$2$3This source code is licensed under the MIT license found in the\n$1$2$3LICENSE file in the root directory of this source tree.

Reviewed By: TheSavior, yungsters

Differential Revision: D7007050

fbshipit-source-id: 37dd6bf0ffec0923bfc99c260bb330683f35553e
2018-02-16 18:31:53 -08:00

107 lines
2.5 KiB
Java

/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.bridge;
import javax.annotation.Nullable;
import android.support.v4.util.Pools;
/**
* Implementation of Dynamic wrapping a ReadableArray.
*/
public class DynamicFromArray implements Dynamic {
private static final Pools.SimplePool<DynamicFromArray> sPool = new Pools.SimplePool<>(10);
private @Nullable ReadableArray mArray;
private int mIndex = -1;
// This is a pools object. Hide the constructor.
private DynamicFromArray() {}
public static DynamicFromArray create(ReadableArray array, int index) {
DynamicFromArray dynamic = sPool.acquire();
if (dynamic == null) {
dynamic = new DynamicFromArray();
}
dynamic.mArray = array;
dynamic.mIndex = index;
return dynamic;
}
@Override
public void recycle() {
mArray = null;
mIndex = -1;
sPool.release(this);
}
@Override
public boolean isNull() {
if (mArray == null) {
throw new IllegalStateException("This dynamic value has been recycled");
}
return mArray.isNull(mIndex);
}
@Override
public boolean asBoolean() {
if (mArray == null) {
throw new IllegalStateException("This dynamic value has been recycled");
}
return mArray.getBoolean(mIndex);
}
@Override
public double asDouble() {
if (mArray == null) {
throw new IllegalStateException("This dynamic value has been recycled");
}
return mArray.getDouble(mIndex);
}
@Override
public int asInt() {
if (mArray == null) {
throw new IllegalStateException("This dynamic value has been recycled");
}
return mArray.getInt(mIndex);
}
@Override
public String asString() {
if (mArray == null) {
throw new IllegalStateException("This dynamic value has been recycled");
}
return mArray.getString(mIndex);
}
@Override
public ReadableArray asArray() {
if (mArray == null) {
throw new IllegalStateException("This dynamic value has been recycled");
}
return mArray.getArray(mIndex);
}
@Override
public ReadableMap asMap() {
if (mArray == null) {
throw new IllegalStateException("This dynamic value has been recycled");
}
return mArray.getMap(mIndex);
}
@Override
public ReadableType getType() {
if (mArray == null) {
throw new IllegalStateException("This dynamic value has been recycled");
}
return mArray.getType(mIndex);
}
}