<commit_before>/*
 * #%L
 * Bitrepository Modifying Client
 * 
 * $Id$
 * $HeadURL$
 * %%
 * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */
package org.bitrepository.common.utils;

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

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.bitrepository.common.ArgumentValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility class for calendar issues. 
 */
public final class CalendarUtils {
    /** The log.*/
    private static Logger log = LoggerFactory.getLogger(CalendarUtils.class);

    /**
     * Private constructor to prevent instantiation of utility class.
     */
    private CalendarUtils() { }
    
    /**
     * Turns a date into a XMLGregorianCalendar.
     * @param date The date. If the argument is null, then epoch is returned.
     * @return The XMLGregorianCalendar.
     */
    public static XMLGregorianCalendar getXmlGregorianCalendar(Date date) {
        if(date == null) {
            log.debug("Cannot convert the date '" + date + "'. Returning epoch instead.");
            date = new Date(0);
        }
        
        GregorianCalendar gc = new GregorianCalendar();
        try {
            gc.setTime(date);
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
        } catch (Exception e) {
            throw new IllegalStateException("Could not convert the date '" + date + "' into the xml format.", e);
        }
    }

    /**
     * Method for easier retrieving the current date in XML format.
     * @param gregorianCalendar the calendar
     * @return The current date in XML format
     */
    public static XMLGregorianCalendar getXmlGregorianCalendar(GregorianCalendar gregorianCalendar) {
        try {
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
        } catch (Exception e) {
            throw new IllegalStateException("Could not convert the date '" + gregorianCalendar + "' into the xml format.", e);
        }
    }

    /**
     * Method for easier retrieving the current date in XML format.
     * @return The current date in XML format
     */
    public static XMLGregorianCalendar getNow() {
        return getXmlGregorianCalendar(new Date());
    }
    
    /**
     * Method for easier retrieving the date for Epoch (January 1, 1970 00:00:00.000 GMT).
     * @return Epoch in XMLGregorianCalendar format.
     */
    public static XMLGregorianCalendar getEpoch() {
        return getXmlGregorianCalendar(new Date(0));
    }
    
    /**
     * Method for easier retrieving the Date for a given time since Epoch in millis.
     * @param millis The amount of milliseconds since Epoch.
     * @return The date in XMLGregorianCalendar format.
     */
    public static XMLGregorianCalendar getFromMillis(long millis) {
        return getXmlGregorianCalendar(new Date(millis));
    }
    
    /**
     * Method for converting a date from the XML calendar type 'XMLGregorianCalendar' to the default java date.
     * 
     * @param xmlCal The XML calendar to convert from.
     * @return The date for the XML calendar converted into the default java date class.
     */
    public static Date convertFromXMLGregorianCalendar(XMLGregorianCalendar xmlCal) {
        ArgumentValidator.checkNotNull(xmlCal, "XMLGregorianCalendar xmlCal");
        
        return xmlCal.toGregorianCalendar().getTime();
    }
    
    /**
     * Create a date object representing the start of the day denoted by dateStr
     * @param dateStr The string representation of the date, in the form '02/26/2015'
     * @return Date A date object representing the start of the day
     */
    public static Date makeStartDateObject(String dateStr) {
        return makeCalendarObject(dateStr).getTime();
    }
    
    /**
     * Create a date object representing the end of the day denoted by dateStr
     * @param dateStr The string representation of the date, in the form '02/26/2015'
     * @return Date A date object representing the end of the day
     */
    public static Date makeEndDateObject(String dateStr) {
        Calendar cal = makeCalendarObject(dateStr);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.MILLISECOND, -1);
        return cal.getTime();
    } 
    
    /**
     * Parses the input string and returns a calendar representation of the day in UTC. 
     * @param dateStr The string representation of the date, in the form '02/26/2015' 
     * @return Calendar A calendar object representing the start of the date in UTC.
     */
    private static Calendar makeCalendarObject(String dateStr) {
        if(dateStr == null || dateStr.trim().isEmpty()) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            try {
                Date basedate = sdf.parse(dateStr);
                int utcOffset = TimeZone.getDefault().getOffset(basedate.getTime());
                Calendar time = Calendar.getInstance();
                time.setTime(basedate);
                time.add(Calendar.MILLISECOND, utcOffset);
                return time;
            } catch (ParseException e) {
                return null;
            } 
        }
    }
    
}
<commit_msg>remember to handle null-pointers in CalendarUtils<commit_after>/*
 * #%L
 * Bitrepository Modifying Client
 * 
 * $Id$
 * $HeadURL$
 * %%
 * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */
package org.bitrepository.common.utils;

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

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.bitrepository.common.ArgumentValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility class for calendar issues. 
 */
public final class CalendarUtils {
    /** The log.*/
    private static Logger log = LoggerFactory.getLogger(CalendarUtils.class);

    /**
     * Private constructor to prevent instantiation of utility class.
     */
    private CalendarUtils() { }
    
    /**
     * Turns a date into a XMLGregorianCalendar.
     * @param date The date. If the argument is null, then epoch is returned.
     * @return The XMLGregorianCalendar.
     */
    public static XMLGregorianCalendar getXmlGregorianCalendar(Date date) {
        if(date == null) {
            log.debug("Cannot convert the date '" + date + "'. Returning epoch instead.");
            date = new Date(0);
        }
        
        GregorianCalendar gc = new GregorianCalendar();
        try {
            gc.setTime(date);
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
        } catch (Exception e) {
            throw new IllegalStateException("Could not convert the date '" + date + "' into the xml format.", e);
        }
    }

    /**
     * Method for easier retrieving the current date in XML format.
     * @param gregorianCalendar the calendar
     * @return The current date in XML format
     */
    public static XMLGregorianCalendar getXmlGregorianCalendar(GregorianCalendar gregorianCalendar) {
        try {
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
        } catch (Exception e) {
            throw new IllegalStateException("Could not convert the date '" + gregorianCalendar + "' into the xml format.", e);
        }
    }

    /**
     * Method for easier retrieving the current date in XML format.
     * @return The current date in XML format
     */
    public static XMLGregorianCalendar getNow() {
        return getXmlGregorianCalendar(new Date());
    }
    
    /**
     * Method for easier retrieving the date for Epoch (January 1, 1970 00:00:00.000 GMT).
     * @return Epoch in XMLGregorianCalendar format.
     */
    public static XMLGregorianCalendar getEpoch() {
        return getXmlGregorianCalendar(new Date(0));
    }
    
    /**
     * Method for easier retrieving the Date for a given time since Epoch in millis.
     * @param millis The amount of milliseconds since Epoch.
     * @return The date in XMLGregorianCalendar format.
     */
    public static XMLGregorianCalendar getFromMillis(long millis) {
        return getXmlGregorianCalendar(new Date(millis));
    }
    
    /**
     * Method for converting a date from the XML calendar type 'XMLGregorianCalendar' to the default java date.
     * 
     * @param xmlCal The XML calendar to convert from.
     * @return The date for the XML calendar converted into the default java date class.
     */
    public static Date convertFromXMLGregorianCalendar(XMLGregorianCalendar xmlCal) {
        ArgumentValidator.checkNotNull(xmlCal, "XMLGregorianCalendar xmlCal");
        
        return xmlCal.toGregorianCalendar().getTime();
    }
    
    /**
     * Create a date object representing the start of the day denoted by dateStr
     * @param dateStr The string representation of the date, in the form '02/26/2015'
     * @return Date A date object representing the start of the day
     */
    public static Date makeStartDateObject(String dateStr) {
        Calendar cal = makeCalendarObject(dateStr);
        if(cal == null) {
            return null;
        } else {
            return cal.getTime();
        }
    }
    
    /**
     * Create a date object representing the end of the day denoted by dateStr
     * @param dateStr The string representation of the date, in the form '02/26/2015'
     * @return Date A date object representing the end of the day
     */
    public static Date makeEndDateObject(String dateStr) {
        Calendar cal = makeCalendarObject(dateStr);
        if(cal == null) {
            return null;
        } else {
            cal.add(Calendar.DAY_OF_MONTH, 1);
            cal.add(Calendar.MILLISECOND, -1);
            return cal.getTime();
        }
    } 
    
    /**
     * Parses the input string and returns a calendar representation of the day in UTC. 
     * @param dateStr The string representation of the date, in the form '02/26/2015' 
     * @return Calendar A calendar object representing the start of the date in UTC.
     */
    private static Calendar makeCalendarObject(String dateStr) {
        if(dateStr == null || dateStr.trim().isEmpty()) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            try {
                Date basedate = sdf.parse(dateStr);
                int utcOffset = TimeZone.getDefault().getOffset(basedate.getTime());
                Calendar time = Calendar.getInstance();
                time.setTime(basedate);
                time.add(Calendar.MILLISECOND, utcOffset);
                return time;
            } catch (ParseException e) {
                return null;
            } 
        }
    }
    
}

{'fraction_non_alphanumeric': 0.08275261324041812, 'fraction_numerical': 0.008869179600886918, 'mean_word_length': 2.5766071934296235, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 18}, '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': '5455236', 'n_tokens_mistral': 3516, 'n_tokens_neox': 3252, 'n_words': 1190}