package org.apache.avro;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;
import java.io.IOException;
import java.util.AbstractSet;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import org.apache.avro.util.MapEntry;
import org.apache.avro.util.internal.Accessor;
import org.apache.avro.util.internal.JacksonUtils;

/* loaded from: avro-1.11.2.jar:org/apache/avro/JsonProperties.class */
public abstract class JsonProperties {
    public static final Null NULL_VALUE;
    private ConcurrentMap<String, JsonNode> props = new AnonymousClass2();
    private Set<String> reserved;

    static {
        Accessor.setAccessor(new Accessor.JsonPropertiesAccessor() { // from class: org.apache.avro.JsonProperties.1
            @Override // org.apache.avro.util.internal.Accessor.JsonPropertiesAccessor
            protected void addProp(JsonProperties props, String name, JsonNode value) {
                props.addProp(name, value);
            }
        });
        NULL_VALUE = new Null();
    }

    /* loaded from: avro-1.11.2.jar:org/apache/avro/JsonProperties$Null.class */
    public static class Null {
        private Null() {
        }
    }

    /* renamed from: org.apache.avro.JsonProperties$2, reason: invalid class name */
    /* loaded from: avro-1.11.2.jar:org/apache/avro/JsonProperties$2.class */
    class AnonymousClass2 extends ConcurrentHashMap<String, JsonNode> {
        private static final long serialVersionUID = 1;
        private Queue<MapEntry<String, JsonNode>> propOrder = new ConcurrentLinkedQueue();

        AnonymousClass2() {
        }

        @Override // java.util.concurrent.ConcurrentHashMap, java.util.Map, java.util.concurrent.ConcurrentMap
        public JsonNode putIfAbsent(String key, JsonNode value) {
            JsonNode r = (JsonNode) super.putIfAbsent((AnonymousClass2) key, (String) value);
            if (r == null) {
                this.propOrder.add(new MapEntry<>(key, value));
            }
            return r;
        }

        @Override // java.util.concurrent.ConcurrentHashMap, java.util.AbstractMap, java.util.Map
        public JsonNode put(String key, JsonNode value) {
            return putIfAbsent(key, value);
        }

        @Override // java.util.concurrent.ConcurrentHashMap, java.util.AbstractMap, java.util.Map
        public Set<Map.Entry<String, JsonNode>> entrySet() {
            return new AbstractSet<Map.Entry<String, JsonNode>>() { // from class: org.apache.avro.JsonProperties.2.1
                @Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
                public Iterator<Map.Entry<String, JsonNode>> iterator() {
                    return new Iterator<Map.Entry<String, JsonNode>>() { // from class: org.apache.avro.JsonProperties.2.1.1
                        Iterator<MapEntry<String, JsonNode>> it;

                        {
                            this.it = AnonymousClass2.this.propOrder.iterator();
                        }

                        @Override // java.util.Iterator
                        public boolean hasNext() {
                            return this.it.hasNext();
                        }

                        /* JADX WARN: Can't rename method to resolve collision */
                        @Override // java.util.Iterator
                        public Map.Entry<String, JsonNode> next() {
                            return this.it.next();
                        }
                    };
                }

                @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
                public int size() {
                    return AnonymousClass2.this.propOrder.size();
                }
            };
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public JsonProperties(Set<String> reserved) {
        this.reserved = reserved;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public JsonProperties(Set<String> reserved, Map<String, ?> propMap) {
        TextNode jsonNode;
        this.reserved = reserved;
        for (Map.Entry<String, ?> a : propMap.entrySet()) {
            Object v = a.getValue();
            if (v instanceof String) {
                jsonNode = TextNode.valueOf((String) v);
            } else if (v instanceof JsonNode) {
                jsonNode = (JsonNode) v;
            } else {
                jsonNode = JacksonUtils.toJsonNode(v);
            }
            this.props.put(a.getKey(), jsonNode);
        }
    }

    public String getProp(String name) {
        JsonNode value = getJsonProp(name);
        if (value == null || !value.isTextual()) {
            return null;
        }
        return value.textValue();
    }

    private JsonNode getJsonProp(String name) {
        return this.props.get(name);
    }

    public Object getObjectProp(String name) {
        return JacksonUtils.toObject(this.props.get(name));
    }

    public void addProp(String name, String value) {
        addProp(name, (JsonNode) TextNode.valueOf(value));
    }

    public void addProp(String name, Object value) {
        if (value instanceof JsonNode) {
            addProp(name, (JsonNode) value);
        } else {
            addProp(name, JacksonUtils.toJsonNode(value));
        }
    }

    public void putAll(JsonProperties np) {
        for (Map.Entry<? extends String, ? extends JsonNode> e : np.props.entrySet()) {
            addProp(e.getKey(), e.getValue());
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void addProp(String name, JsonNode value) {
        if (this.reserved.contains(name)) {
            throw new AvroRuntimeException("Can't set reserved property: " + name);
        }
        if (value == null) {
            throw new AvroRuntimeException("Can't set a property to null: " + name);
        }
        JsonNode old = this.props.putIfAbsent(name, value);
        if (old != null && !old.equals(value)) {
            throw new AvroRuntimeException("Can't overwrite property: " + name);
        }
    }

    public void addAllProps(JsonProperties properties) {
        for (Map.Entry<String, JsonNode> entry : properties.props.entrySet()) {
            addProp(entry.getKey(), entry.getValue());
        }
    }

    public Map<String, Object> getObjectProps() {
        Map<String, Object> result = new LinkedHashMap<>();
        for (Map.Entry<String, JsonNode> e : this.props.entrySet()) {
            result.put(e.getKey(), JacksonUtils.toObject(e.getValue()));
        }
        return Collections.unmodifiableMap(result);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void writeProps(JsonGenerator gen) throws IOException {
        for (Map.Entry<String, JsonNode> e : this.props.entrySet()) {
            gen.writeObjectField(e.getKey(), e.getValue());
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public int propsHashCode() {
        return this.props.hashCode();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean propsEqual(JsonProperties np) {
        return this.props.equals(np.props);
    }

    public boolean hasProps() {
        return !this.props.isEmpty();
    }
}
