<commit_before>package zhangshe.json;

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Hashtable;

public class JSONObject
    implements JSONValue
{
  /**
   * A PrintWriter for print()
   */
  static PrintWriter pen = new PrintWriter(System.out, true);
  // +--------+----------------------------------------------------------
  // | Fields |
  // +--------+
  /**
   * A Hashtable to store objects
   */
  Hashtable<JSONValue, JSONValue> hash;

  /**
   * The size of this JSONObject
   */
  int length = 0;

  // +-------------+----------------------------------------------------------
  // | Constructor |
  // +-------------+
  /**
   * Construct a JSONObject
   */
  public JSONObject()
  {
    this.hash = new Hashtable<JSONValue, JSONValue>();
    length++; // '{';
  } // JSONObject()

  // +---------+----------------------------------------------------------
  // | Methods |
  // +---------+
  /**
   * Add paris into JSONObject
   * 
   * @param key
   *          A JSONValue
   * @param value
   *          A JSONValue
   * @pre Both key and value should be valid JSONValue
   * @post this JSONObject contains one more pair, and the size is incremented
   *       by 1
   */
  public void add(JSONValue key, JSONValue value)
  {
    this.hash.put(key, value);
    length += (key.size() + value.size() + 1); // ':'
  } // add(String, JSONValue)

  /**
   * Convert a JSONObject into a string
   * 
   * @pre none
   * @post a String is given
   */
  @Override
  public String toJSONString()
  {

    String str = "{"; // initialize the string
    Enumeration<JSONValue> keys = hash.keys(); // get all keys in the HashTable

    while (keys.hasMoreElements())
      {
        JSONValue temp = keys.nextElement();
        str += temp.toJSONString() + ":" + hash.get(temp).toJSONString();
        if (keys.hasMoreElements())
          str += ",";
      } // for(i)
    str += "}";
    return str;
  } // toJSONString()

  /**
   * Get the size of the JSONObject
   * 
   * @post An Integer is given
   */
  @Override
  public int size()
  {
    return length;
  } // size()

  @Override
  public void print()
  {
    this.print(1);
  } // print()

  @Override
  public void print(int format)
  {
    pen.format("%" + format + "s", "");
    pen.print("{");

    Enumeration<JSONValue> keys = hash.keys(); // get all keys in the HashTable
    if (!keys.hasMoreElements())
      {
        pen.print("}");
        pen.flush();
        return;
      } // if keys have no more elements
    while (keys.hasMoreElements())
      {
        pen.println();
        JSONValue temp = keys.nextElement();
        temp.print(format + 2);
        pen.print(":");
        pen.flush();
        hash.get(temp).print(format+1);
        if (keys.hasMoreElements())
          pen.print(",");
      } // for(i)
    pen.println();
    pen.format("%" + format + "s", "");
    pen.print("}");
    pen.flush();
  } // print(String)
} // class JSONObject<commit_msg>add comments and get method in JSONObject<commit_after>package zhangshe.json;

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Hashtable;

public class JSONObject
    implements JSONValue
{
  /**
   * A PrintWriter for print()
   */
  static PrintWriter pen = new PrintWriter(System.out, true);

  // +--------+----------------------------------------------------------
  // | Fields |
  // +--------+
  /**
   * A Hashtable to store objects
   */
  Hashtable<JSONValue, JSONValue> hash;

  /**
   * Store the number of characters in the JSONObject, including the open and closed brackets.
   */
  int length = 0;

  // +-------------+----------------------------------------------------------
  // | Constructor |
  // +-------------+
  /**
   * Construct a JSONObject
   */
  public JSONObject()
  {
    this.hash = new Hashtable<JSONValue, JSONValue>();
    length++; // '{';
  } // JSONObject()

  // +---------+----------------------------------------------------------
  // | Methods |
  // +---------+

  /**
   * Add paris into JSONObject
   * 
   * @param key
   *          A JSONValue
   * @param value
   *          A JSONValue
   * @pre Both key and value should be valid JSONValue
   * @post this JSONObject contains one more pair, and the size is incremented
   *       by 1
   */
  public void add(JSONValue key, JSONValue value)
  {
    this.hash.put(key, value);
    length += (key.size() + value.size() + 1); // ':'
  } // add(String, JSONValue)

  /**
   * Convert a JSONObject into a string
   * 
   * @pre none
   * @post a String is given
   */
  @Override
  public String toJSONString()
  {

    String str = "{"; // initialize the string
    Enumeration<JSONValue> keys = hash.keys(); // get all keys in the HashTable

    while (keys.hasMoreElements())
      {
        JSONValue temp = keys.nextElement();
        str += temp.toJSONString() + ":" + hash.get(temp).toJSONString();
        if (keys.hasMoreElements())
          str += ",";
      } // for(i)
    str += "}";
    return str;
  } // toJSONString()

  /**
   * Get the size of the JSONObject
   * 
   * @post An Integer is given
   */
  @Override
  public int size()
  {
    return length;
  } // size()

  /**
   * Get the value in the Object by key
   * 
   * @param key
   * @return 
   * The value paired with the given key
   */
  public JSONValue get(JSONValue key)
  {
    return this.hash.get(key);
  } // get(JSONValue)


  /**
   * print the content in the JSONObject
   */
  @Override
  public void print()
  {
    // print the object with format = 1
    this.print(1);
  } // print()

  /**
   * print the content in the JSONObject, format the result by given spaces
   */
  @Override
  public void print(int format)
  {
    // format and print the open bracket
    pen.format("%" + format + "s", "");
    pen.print("{");

    // get all keys in the HashTable
    Enumeration<JSONValue> keys = hash.keys();
    // if the Object is empty, print the closed bracket and return
    if (!keys.hasMoreElements())
      {
        pen.print("}");
        pen.flush();
        return;
      } // if keys have no more elements

    // otherwise, print all keys and values in the JSONObject
    while (keys.hasMoreElements())
      {
        pen.println();
        JSONValue temp = keys.nextElement();
        temp.print(format + 2);
        pen.print(":");
        pen.flush();
        hash.get(temp).print(format + 1);
        if (keys.hasMoreElements())
          pen.print(",");
      } // for(i)

    // start a new line and put the closed bracket
    pen.println();
    pen.format("%" + format + "s", "");
    pen.print("}");
    pen.flush();
  } // print(String)
} // class JSONObject
{'fraction_non_alphanumeric': 0.20817120622568094, 'fraction_numerical': 0.0019455252918287938, 'mean_word_length': 1.6980218005652, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 11, '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': '3498139', 'n_tokens_mistral': 2027, 'n_tokens_neox': 1976, 'n_words': 551}