<commit_before>/*
 * The contents of this file are subject to the OpenMRS Public License
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.openhmis.commons.api;

import java.util.Calendar;

/**
 * General utility methods.
 */
public class Utility {
	protected Utility() {}
	
	/**
	 * Returns the specified object as the specified class or returns null if the cast is not supported.
	 * @param cls The generic class to cast the object to.
	 * @param o The object to cast.
	 * @param <T> The generic class to cast the object to.
	 * @return The object cast to the specified class or {@code null} if the cast is not supported.
	 */
	public static <T> T as(Class<T> cls, Object o) {
		if (cls.isInstance(o)) {
			return cls.cast(o);
		}
		return null;
	}
	
	/**
	 * Clears the time portion of the specified {@link Calendar}, setting the hour, minute, second, and millisecond parts to
	 * 0.
	 * @param cal The calendar object to clear the time portion from.
	 */
	public static void clearCalendarTime(Calendar cal) {
		if (cal == null) {
			throw new IllegalArgumentException("The calendar must be defined.");
		}
		
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
	}
}<commit_msg>Added helper method for parsing dates.<commit_after>/*
 * The contents of this file are subject to the OpenMRS Public License
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.openhmis.commons.api;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * General utility methods.
 */
public class Utility {
	private static final Log LOG = LogFactory.getLog(Utility.class);
	private static final int DATE_ONLY_TEXT_LENGTH = 10;
	
	protected Utility() {}
	
	/**
	 * Returns the specified object as the specified class or returns null if the cast is not supported.
	 * @param cls The generic class to cast the object to.
	 * @param o The object to cast.
	 * @param <T> The generic class to cast the object to.
	 * @return The object cast to the specified class or {@code null} if the cast is not supported.
	 */
	public static <T> T as(Class<T> cls, Object o) {
		if (cls.isInstance(o)) {
			return cls.cast(o);
		}
		return null;
	}
	
	/**
	 * Clears the time portion of the specified {@link Calendar}, setting the hour, minute, second, and millisecond parts to
	 * 0.
	 * @param cal The calendar object to clear the time portion from.
	 */
	public static void clearCalendarTime(Calendar cal) {
		if (cal == null) {
			throw new IllegalArgumentException("The calendar must be defined.");
		}
		
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
	}
	
	/**
	 * Parses a standard OpenHMIS formatted (openhmis.dateFormat) date returning the {@link java.util.Date} object.
	 * @param dateText The date text to parse
	 * @return The date or null if the text cannot be parsed.
	 */
	public static Date parseOpenhmisDateString(String dateText) {
		if (StringUtils.isEmpty(dateText)) {
			return null;
		}
		
		SimpleDateFormat dateFormat = null;
		if (dateText.length() == DATE_ONLY_TEXT_LENGTH) {
			dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		} else {
			dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
		}
		
		try {
			return dateFormat.parse(dateText);
		} catch (ParseException pex) {
			LOG.warn("Could not parse the date string '" + dateText + "'.", pex);
			
			return null;
		}
	}
}
{'fraction_non_alphanumeric': 0.09958140559594625, 'fraction_numerical': 0.003525005507821106, 'mean_word_length': 3.6279306829765545, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2893758', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1315, 'n_words': 539}