<commit_before>//
// Copyright 2012-2014 Paytronix Systems, Inc.
//
// Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package com.paytronix.utils.validation

import scala.language.implicitConversions

import java.util.TimeZone
import org.joda.time.{DateTime, DateTimeZone, LocalDate, LocalDateTime, LocalTime, Period, ReadableInstant}
import org.joda.time.format.{DateTimeFormat, DateTimeFormatter, ISODateTimeFormat, PeriodFormatter}
import scalaz.NonEmptyList

import base.{Validated, ValidationError, anyE, failure, predicateE, success}
import NonEmptyList.nels

object date {
    val invalidDateFormatError = ValidationError("invalid_date", "invalid date") // FIXME? explain what is expected?
    val invalidTimeFormatError = ValidationError("invalid_time", "invalid time") // FIXME? explain what is expected?
    val invalidPeriodFormatError = ValidationError("invalid_period", "invalid period") // FIXME? explain what is expected?

    /**
     * Validation function which maps a Joda `ReadableInstant` (e.g. `DateTime`) to a `java.util.Date`
     * Example: `isoDateTime and javaUtilDate`
     */
    val javaUtilDate: ReadableInstant => Validated[java.util.Date] =
        in => success(new java.util.Date(in.getMillis))

    /**
     * Validation function which maps a Joda `LocalDateTime` to a `java.util.Date` in the default time zone
     * Example: `isoishLocalDateTime and localJavaUtilDate`
     */
    val localJavaUtilDate: LocalDateTime => Validated[java.util.Date] =
        in => success(in.toDate(TimeZone.getDefault))

    /**
     * Validation function which maps a Joda `LocalDate` to a `java.sql.Date` in the default time zone
     * Example: `isoDate and javaSqlDate`
     */
    val javaSqlDate: LocalDate => Validated[java.sql.Date] =
        in => success(new java.sql.Date(in.toDateTimeAtStartOfDay().getMillis))

    /**
     * Validation function which maps a Joda `LocalTime` to a `java.sql.Time`
     * Example: `isoTime and javaSqlTime`
     */
    val javaSqlTime: LocalTime => Validated[java.sql.Time] =<commit_msg>[issue48518] Fixed validation function conversion from joda LocalDate to java SQL date, similar to recent Interchange fix<commit_after>//
// Copyright 2012-2014 Paytronix Systems, Inc.
//
// Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package com.paytronix.utils.validation

import scala.language.implicitConversions

import java.util.TimeZone
import org.joda.time.{DateTime, DateTimeZone, LocalDate, LocalDateTime, LocalTime, Period, ReadableInstant}
import org.joda.time.format.{DateTimeFormat, DateTimeFormatter, ISODateTimeFormat, PeriodFormatter}
import scalaz.NonEmptyList

import base.{Validated, ValidationError, anyE, failure, predicateE, success}
import NonEmptyList.nels

object date {
    val invalidDateFormatError = ValidationError("invalid_date", "invalid date") // FIXME? explain what is expected?
    val invalidTimeFormatError = ValidationError("invalid_time", "invalid time") // FIXME? explain what is expected?
    val invalidPeriodFormatError = ValidationError("invalid_period", "invalid period") // FIXME? explain what is expected?

    /**
     * Validation function which maps a Joda `ReadableInstant` (e.g. `DateTime`) to a `java.util.Date`
     * Example: `isoDateTime and javaUtilDate`
     */
    val javaUtilDate: ReadableInstant => Validated[java.util.Date] =
        in => success(new java.util.Date(in.getMillis))

    /**
     * Validation function which maps a Joda `LocalDateTime` to a `java.util.Date` in the default time zone
     * Example: `isoishLocalDateTime and localJavaUtilDate`
     */
    val localJavaUtilDate: LocalDateTime => Validated[java.util.Date] =
        in => success(in.toDate(TimeZone.getDefault))

    /**
     * Validation function which maps a Joda `LocalDate` to a `java.sql.Date` in the default time zone
     * Example: `isoDate and javaSqlDate`
     */
    val javaSqlDate: LocalDate => Validated[java.sql.Date] =
        in => success(new java.sql.Date(in.toDate().getTime))

    /**
     * Validation function which maps a Joda `LocalDate` to a `java.sql.Date` in the default time zone
     * Example: `isoDate and javaSqlDate`
     */
    val javaSqlDate: LocalDate => Validated[java.sql.Date] =
        in => success(new java.sql.Date(in.toDate().getTime))

    /**
     * Validation function which maps a Joda `LocalTime` to a `java.sql.Time`
     * Example: `isoTime and javaSqlTime`
     */
    val javaSqlTime: LocalTime => Validated[java.sql.Time] =
{'fraction_non_alphanumeric': 0.10351100600327451, 'fraction_numerical': 0.005275604875386575, 'mean_word_length': 3.789198606271777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '982278', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1524, 'n_words': 518}