package seedu.manager.commons.util;

import static seedu.manager.commons.core.Messages.MESSAGE_CANNOT_PARSE_TO_DATE;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;

import com.joestelmach.natty.*;

import seedu.manager.commons.exceptions.IllegalValueException;

/**
 * Helper functions for handling strings.
 */
public class StringUtil {
    public static boolean containsIgnoreCase(String source, String query) {
        String[] split = source.toLowerCase().split("\\s+");
        List<String> strings = Arrays.asList(split);
        return strings.stream().filter(s -> s.equals(query.toLowerCase())).count() > 0;
    }

    /**
     * Returns a detailed message of the t, including the stack trace.
     */
    public static String getDetails(Throwable t){
        assert t != null;
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw));
        return t.getMessage() + "\n" + sw.toString();
    }

    /**
     * Returns true if s represents an unsigned integer e.g. 1, 2, 3, ... <br>
     *   Will return false for null, empty string, "-1", "0", "+1", and " 2 " (untrimmed) "3 0" (contains whitespace).
     * @param s Should be trimmed.
     */
    public static boolean isUnsignedInteger(String s){
        return s != null && s.matches("^0*[1-9]\\d*$");
    }
    
    /**
     * Attempts to validate an AMDate type
     * @param s Should be trimmed
     */
    public static void validateAMDate(String dateTime) throws IllegalValueException {
        Parser parser = new Parser();
        List<DateGroup> groups = parser.parse(dateTime);
        if (groups.size() > 0) {
            throw new IllegalValueException(String.format(MESSAGE_CANNOT_PARSE_TO_DATE, dateTime));
        }
    }
    
    /**
     * Returns true if s can be parsed as an AMDate type
     * @param s Should be trimmed
     */
    public static boolean isAMDate(String dateTime) {
        Parser parser = new Parser();
        List<DateGroup> groups = parser.parse(dateTime);
        return groups.size() > 0;
    }
}

package seedu.manager.commons.util;

import static seedu.manager.commons.core.Messages.MESSAGE_CANNOT_PARSE_TO_DATE;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;

import com.joestelmach.natty.*;

import seedu.manager.commons.exceptions.IllegalValueException;

/**
 * Helper functions for handling strings.
 */
public class StringUtil {
    public static boolean containsIgnoreCase(String source, String query) {
        String[] split = source.toLowerCase().split("\\s+");
        List<String> strings = Arrays.asList(split);
        return strings.stream().filter(s -> s.equals(query.toLowerCase())).count() > 0;
    }

    /**
     * Returns a detailed message of the t, including the stack trace.
     */
    public static String getDetails(Throwable t){
        assert t != null;
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw));
        return t.getMessage() + "\n" + sw.toString();
    }

    /**
     * Returns true if s represents an unsigned integer e.g. 1, 2, 3, ... <br>
     *   Will return false for null, empty string, "-1", "0", "+1", and " 2 " (untrimmed) "3 0" (contains whitespace).
     * @param s Should be trimmed.
     */
    public static boolean isUnsignedInteger(String s){
        return s != null && s.matches("^0*[1-9]\\d*$");
    }
    
    /**
     * Attempts to validate an AMDate type
     * @param s Should be trimmed
     */
    public static void validateAMDate(String dateTime) throws IllegalValueException {
        Parser parser = new Parser();
        List<DateGroup> groups = parser.parse(dateTime);
        if (groups.size() <= 0) {
            throw new IllegalValueException(String.format(MESSAGE_CANNOT_PARSE_TO_DATE, dateTime));
        }
    }
    
    /**
     * Returns true if s can be parsed as an AMDate type
     * @param s Should be trimmed
     */
    public static boolean isAMDate(String dateTime) {
        Parser parser = new Parser();
        List<DateGroup> groups = parser.parse(dateTime);
        return groups.size() > 0;
    }
}

Fix bug in validateAMDate