All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.bval.constraints.PastOrPresentValidator Maven / Gradle / Ivy

There is a newer version: 10.0.0-M3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.bval.constraints;

import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.chrono.ChronoLocalDate;
import java.time.chrono.ChronoLocalDateTime;
import java.time.chrono.ChronoZonedDateTime;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.function.Function;
import java.util.function.IntPredicate;

import javax.validation.ConstraintValidator;
import javax.validation.constraints.PastOrPresent;

/**
 * Defines built-in {@link ConstraintValidator} implementations for {@link PastOrPresent}.
 *
 * @param 
 *            validated type
 */
public abstract class PastOrPresentValidator> extends TimeValidator {

    public static class ForDate extends PastOrPresentValidator {

        public ForDate() {
            super(clock -> Date.from(clock.instant()));
        }
    }

    public static class ForCalendar extends PastOrPresentValidator {

        public ForCalendar() {
            super(clock -> GregorianCalendar.from(clock.instant().atZone(clock.getZone())));
        }
    }

    public static class ForInstant extends PastOrPresentValidator {

        public ForInstant() {
            super(Instant::now);
        }
    }

    public static class ForChronoLocalDate extends PastOrPresentValidator {

        public ForChronoLocalDate() {
            super(LocalDate::now, CHRONO_LOCAL_DATE_COMPARATOR);
        }
    }

    public static class ForChronoLocalDateTime extends PastOrPresentValidator> {

        public ForChronoLocalDateTime() {
            super(LocalDateTime::now, CHRONO_LOCAL_DATE_TIME_COMPARATOR);
        }
    }

    public static class ForLocalTime extends PastOrPresentValidator {

        public ForLocalTime() {
            super(LocalTime::now);
        }
    }

    public static class ForOffsetDateTime extends PastOrPresentValidator {

        public ForOffsetDateTime() {
            super(OffsetDateTime::now);
        }
    }

    public static class ForOffsetTime extends PastOrPresentValidator {

        public ForOffsetTime() {
            super(OffsetTime::now);
        }
    }

    public static class ForChronoZonedDateTime extends PastOrPresentValidator> {

        public ForChronoZonedDateTime() {
            super(ZonedDateTime::now, CHRONO_ZONED_DATE_TIME_COMPARATOR);
        }
    }

    public static class ForMonthDay extends PastOrPresentValidator {

        public ForMonthDay() {
            super(MonthDay::now);
        }
    }

    public static class ForYear extends PastOrPresentValidator {

        public ForYear() {
            super(Year::now);
        }
    }

    public static class ForYearMonth extends PastOrPresentValidator {

        public ForYearMonth() {
            super(YearMonth::now);
        }
    }

    private static final IntPredicate TEST = n -> n <= 0;

    protected PastOrPresentValidator(Function now) {
        super(now, TEST);
    }

    protected PastOrPresentValidator(Function now, Comparator cmp) {
        super(now, cmp, TEST);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy