<commit_before>/*
 * COSC326 - 2016 S2 - Étude 12 - Supersizing Ants
 * <NAME>, <NAME>, <NAME>
 * Java 8
 */

import model.Ant;
import model.AntType;
import model.Universe;

import java.awt.Point;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

public class InputHandler {
  private static Universe universe;

  public static Universe initialiseUniverse (InputStream in) {
    universe = new Universe();
    HashSet<Character> worldStates = new HashSet<>();
    ArrayList<Ant> ants = new ArrayList<>();

    Scanner sc = new Scanner(in);
    String[] line;

    while (sc.hasNextLine()) {
      line = sc.nextLine().trim().split("\\s+");

      if (line.length == 0) {
        continue;
      }

      if (line.length == 2) {
        if (line[0].equalsIgnoreCase("ant")) {
          if (validateAntName(line[1])) {
            universe.species.put(line[1], parseAnt(sc, line[1]));
            continue;
          } else {
            printAndExit("Duplicate or invalid Ant name: ", line);
          }
        }

        if (line[0].equalsIgnoreCase("World")) {
          universe.defaultState = line[1].charAt(0);
          for (char c : line[1].toCharArray()) {
            worldStates.add(c);
          }
          continue;
        }
      }

      if (line.length == 3) {
        int x = 0, y = 0;

        if (universe.species.containsKey(line[0])) {
          try {
            x = Integer.valueOf(line[1]);
            y = Integer.valueOf(line[2]);
          } catch (NumberFormatException e) {
            printAndExit("Invalid ant position:", line);
          }
          Ant ant = new Ant(universe, universe.species.get(line[0]));
          ant.position = new Point(x, y);
          ants.add(ant);
          continue;
        }
        
        if (line[0].equalsIgnoreCase("size")) {
          try {
            x = Integer.valueOf(line[0]);
            y = Integer.valueOf(line[1]);
          } catch (NumberFormatException e) {
            printAndExit("Invalid size:", line);
          }
          universe.wrap = true;
          universe.width = x;
          universe.height = y;
          continue;
        }

        if (line[2].length() == 1) {
          try {
            x = Integer.valueOf(line[0]);
            y = Integer.valueOf(line[1]);
          } catch (NumberFormatException e) {
            printAndExit("Invalid ant position:", line);
          }
          universe.world.put(new Point(x, y), line[2].charAt(0));
          continue;
        }
      }

      printAndExit("Invalid line:", line);
    }

    if (worldStates.isEmpty()) {
      printAndExit("Allowed states not specified.", null);<commit_msg>Improved error reporting<commit_after>/*
 * COSC326 - 2016 S2 - Étude 12 - Supersizing Ants
 * <NAME>, <NAME>, <NAME>
 * Java 8
 */

import model.Ant;
import model.AntType;
import model.Universe;

import java.awt.Point;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

public class InputHandler {
  private static Universe universe;

  public static Universe initialiseUniverse (InputStream in) {
    universe = new Universe();
    HashSet<Character> worldStates = new HashSet<>();
    ArrayList<Ant> ants = new ArrayList<>();

    Scanner sc = new Scanner(in);
    String[] line;

    while (sc.hasNextLine()) {
      line = sc.nextLine().trim().split("\\s+");

      if (line.length == 0) {
        continue;
      }

      if (line.length == 2) {
        if (line[0].equalsIgnoreCase("ant")) {
          if (validateAntName(line[1])) {
            universe.species.put(line[1], parseAnt(sc, line[1]));
            continue;
          } else {
            printAndExit("Duplicate or invalid Ant name:", line);
          }
        }

        if (line[0].equalsIgnoreCase("World")) {
          universe.defaultState = line[1].charAt(0);
          for (char c : line[1].toCharArray()) {
            worldStates.add(c);
          }
          continue;
        }
      }

      if (line.length == 3) {
        int x = 0, y = 0;

        if (universe.species.containsKey(line[0])) {
          try {
            x = Integer.valueOf(line[1]);
            y = Integer.valueOf(line[2]);
          } catch (NumberFormatException e) {
            printAndExit("Invalid ant position:", line);
          }
          Ant ant = new Ant(universe, universe.species.get(line[0]));
          ant.position = new Point(x, y);
          ants.add(ant);
          continue;
        }
        
        if (line[0].equalsIgnoreCase("size")) {
          if (universe.wrap) {
            printAndExit("Duplicate size declaration:", line);
          }
          try {
            x = Integer.valueOf(line[0]);
            y = Integer.valueOf(line[1]);
          } catch (NumberFormatException e) {
            printAndExit("Invalid size:", line);
          }
          if (x <= 0 || y <= 0){
            printAndExit("Invalid size:", line);
          }
          universe.wrap = true;
          universe.width = x;
          universe.height = y;
          continue;
        }

        if (line[2].length() == 1) {
          try {
            x = Integer.valueOf(line[0]);
            y = Integer.valueOf(line[1]);
          } catch (NumberFormatException e) {
            printAndExit("Invalid ant position:", line);
          }
          universe.world.put(new Point(x, y), line[2].charAt(0));
          continue;
        }
      }

      printAndExit("Invalid line:", line);
    }

    if (worldStates.isEmpty()) {
      printAndExit("Allowed states not specified.", null);
{'fraction_non_alphanumeric': 0.140435403283369, 'fraction_numerical': 0.013561741613133477, 'mean_word_length': 1.867007672634271, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, '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': '3586530', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1620, 'n_words': 371}