<commit_before>/*
 * @(#) JSONObject.java
 *
 * jsonutil JSON Utility Library
 * Copyright (c) 2014 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.pwall.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;

import net.pwall.util.Strings;

/**
 * A JSON object.
 *
 * @author <NAME>
 */
public class JSONObject implements JSONValue, Iterable<String> {

    private List<Entry> list;
    private Map<String, Integer> map;

    public JSONObject() {
        list = new ArrayList<>();
        map = new HashMap<>();
    }

    public JSONValue get(String key) {
        Integer index = map.get(Objects.requireNonNull(key));
        return index == null ? null : list.get(index).getValue();
    }

    public void put(String key, JSONValue value) {
        Integer index = map.get(Objects.requireNonNull(key));
        if (index != null)
            list.get(index).setValue(value);
        else {
            map.put(key, list.size());
            list.add(new Entry(key, value));
        }
    }

    public void putValue(String key, CharSequence cs) {
        put(key, new JSONString(cs));
    }

    public void putValue(String key, int value) {
        put(key, value == 0 ? JSONNumber.ZERO : new JSONNumber(value));
    }

    public void putValue(String key, long value) {
        put(key, new JSONNumber(value));
    }

    public void putValue(String key, float value) {
        put(key, new JSONNumber(value));
    }

    public void putValue(String key, double value) {
        put(key, new JSONNumber(value));
    }

    public void putValue(String key, Number value) {
        put(key, new JSONNumber(value));
    }

    public void putValue(String key, boolean value) {
        put(key, JSONBoolean.valueOf(value));
    }

    public void putValue(String key, Boolean value) {
        put(key, JSONBoolean.valueOf(Objects.requireNonNull(value).booleanValue()));
    }

    public void putNull(String key) {
        put(key, null);
    }

    public void remove(String key) {
        Integer index = map.remove(Objects.requireNonNull(key));
        if (index != null)
            list.remove(index.intValue());
    }

    /**
     * Get an {@link Iterator} over the names (keys) of the members of the object.
     *
     * @return  an {@link Iterator}
     */
    @Override
    public Iterator<String> iterator() {
        return new Iter();
    }

    @Override
    public String toJSON() {
        StringBuilder sb = new StringBuilder();
        try {
            appendJSON(sb);
        }
        catch (IOException e) {
            // can't happen - StringBuilder does not throw IOException
        }
        return sb.toString();
    }

    @Override
    public void appendJSON(Appendable a) throws IOException {<commit_msg>Simplified code - removed Map<commit_after>/*
 * @(#) JSONObject.java
 *
 * jsonutil JSON Utility Library
 * Copyright (c) 2014 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.pwall.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;

import net.pwall.util.Strings;

/**
 * A JSON object.
 *
 * @author <NAME>
 */
public class JSONObject implements JSONValue, Iterable<String> {

    private List<Entry> list;

    public JSONObject() {
        list = new ArrayList<>();
    }

    public JSONValue get(String key) {
        int index = findIndex(Objects.requireNonNull(key));
        return index < 0 ? null : list.get(index).getValue();
    }

    public boolean containsKey(String key) {
        return findIndex(Objects.requireNonNull(key)) >= 0;
    }

    public void put(String key, JSONValue value) {
        int index = findIndex(Objects.requireNonNull(key));
        if (index >= 0)
            list.get(index).setValue(value);
        else
            list.add(new Entry(key, value));
    }

    public void putValue(String key, CharSequence cs) {
        put(key, new JSONString(cs));
    }

    public void putValue(String key, int value) {
        put(key, value == 0 ? JSONNumber.ZERO : new JSONNumber(value));
    }

    public void putValue(String key, long value) {
        put(key, new JSONNumber(value));
    }

    public void putValue(String key, float value) {
        put(key, new JSONNumber(value));
    }

    public void putValue(String key, double value) {
        put(key, new JSONNumber(value));
    }

    public void putValue(String key, Number value) {
        put(key, new JSONNumber(value));
    }

    public void putValue(String key, boolean value) {
        put(key, JSONBoolean.valueOf(value));
    }

    public void putValue(String key, Boolean value) {
        put(key, JSONBoolean.valueOf(Objects.requireNonNull(value).booleanValue()));
    }

    public void putNull(String key) {
        put(key, null);
    }

    public void remove(String key) {
        int index = findIndex(Objects.requireNonNull(key));
        if (index >= 0)
            list.remove(index);
    }

    private int findIndex(String key) {
        for (int i = 0, n = list.size(); i < n; i++)
            if (list.get(i).key.equals(key))
                return i;
        return -1;
    }

    /**
     * Get an {@link Iterator} over the names (keys) of the members of the object.
     *
     * @return  an {@link Iterator}
     */
    @Override
    public Iterator<String> iterator() {
        return new Iter();
    }

    @Override
    public String toJSON() {
        StringBuilder sb = new StringBuilder();
        try {
            appendJSON(sb);
        }
        catch (IOException e) {
            // can't happen - StringBuilder does not throw IOException
        }
        return sb.toString();
    }

    @Override
    public void appendJSON(Appendable a) throws IOException {
{'fraction_non_alphanumeric': 0.099279324055666, 'fraction_numerical': 0.0019880715705765406, 'mean_word_length': 2.782424812030075, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5315713', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2162, 'n_words': 802}