All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shade.com.alibaba.fastjson2.reader.FieldReaderAtomicLongArrayReadOnly Maven / Gradle / Ivy

There is a newer version: 1.3.7
Show newest version
package com.alibaba.fastjson2.reader;

import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.schema.JSONSchema;
import com.alibaba.fastjson2.util.TypeUtils;

import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.atomic.AtomicLongArray;

final class FieldReaderAtomicLongArrayReadOnly
        extends FieldReader {
    FieldReaderAtomicLongArrayReadOnly(
            String fieldName,
            Class fieldType,
            int ordinal,
            JSONSchema jsonSchema,
            Method method
    ) {
        super(fieldName, fieldType, fieldType, ordinal, 0, null, null, null, jsonSchema, method, null);
    }

    @Override
    public boolean isReadOnly() {
        return true;
    }

    @Override
    public void accept(T object, Object value) {
        if (value == null) {
            return;
        }

        try {
            AtomicLongArray atomic = (AtomicLongArray) method.invoke(object);
            if (value instanceof AtomicLongArray) {
                AtomicLongArray array = (AtomicLongArray) value;
                for (int i = 0; i < array.length(); i++) {
                    atomic.set(i, array.get(i));
                }
            } else {
                List values = (List) value;
                for (int i = 0; i < values.size(); i++) {
                    int itemValue = TypeUtils.toIntValue(values.get(i));
                    atomic.set(i, itemValue);
                }
            }
        } catch (Exception e) {
            throw new JSONException("set " + fieldName + " error", e);
        }
    }

    @Override
    public void readFieldValue(JSONReader jsonReader, T object) {
        if (jsonReader.readIfNull()) {
            return;
        }

        AtomicLongArray atomic;
        try {
            atomic = (AtomicLongArray) method.invoke(object);
        } catch (Exception e) {
            throw new JSONException(jsonReader.info("set " + fieldName + " error"), e);
        }
        if (jsonReader.nextIfArrayStart()) {
            for (int i = 0; ; ++i) {
                if (jsonReader.nextIfArrayEnd()) {
                    break;
                }

                long value = jsonReader.readInt64Value();
                if (atomic != null && i < atomic.length()) {
                    atomic.set(i, value);
                }
            }
        }
    }

    @Override
    public Object readFieldValue(JSONReader jsonReader) {
        if (jsonReader.nextIfNull()) {
            return null;
        }
        return jsonReader.readArray(Long.class);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy