001/*
002 * Copyright 2023 the original author or authors.
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * https://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.cuioss.test.generator.impl;
017
018import static de.cuioss.test.generator.Generators.integers;
019
020import java.time.ZonedDateTime;
021
022import de.cuioss.test.generator.Generators;
023import de.cuioss.test.generator.TypedGenerator;
024
025/**
026 * Provide any value for {@linkplain ZonedDateTime}
027 *
028 * @author Eugen Fischer
029 */
030public class ZonedDateTimeGenerator implements TypedGenerator<ZonedDateTime> {
031
032    private static final TypedGenerator<Integer> SOME_INT = integers(0, 10);
033
034    @Override
035    public ZonedDateTime next() {
036        return ZonedDateTime.ofInstant(Generators.dates().next().toInstant(), Generators.zoneIds().next());
037    }
038
039    @Override
040    public Class<ZonedDateTime> getType() {
041        return ZonedDateTime.class;
042    }
043
044    /**
045     * @return an arbitrary ZonedDateTime
046     */
047    public static ZonedDateTime any() {
048        return new ZonedDateTimeGenerator().next();
049    }
050
051    /**
052     * @return value of ZonedDateTime for now
053     */
054    public static ZonedDateTime now() {
055        return ZonedDateTime.now();
056    }
057
058    /**
059     * @return value of ZonedDateTime one hour ago
060     */
061    public static ZonedDateTime someMinutesAgo() {
062        return now().minusMinutes(SOME_INT.next());
063    }
064
065    /**
066     * @return value of ZonedDateTime one hour ago
067     */
068    public static ZonedDateTime someHoursAgo() {
069        return now().minusHours(SOME_INT.next());
070    }
071
072    /**
073     * @return value of ZonedDateTime one day ago
074     */
075    public static ZonedDateTime someDaysAgo() {
076        return now().minusDays(SOME_INT.next());
077    }
078
079    /**
080     * @return value of ZonedDateTime one week ago
081     */
082    public static ZonedDateTime someWeeksAgo() {
083        return now().minusWeeks(SOME_INT.next());
084    }
085
086    /**
087     * @return value of ZonedDateTime one month ago
088     */
089    public static ZonedDateTime someMonthsAgo() {
090        return now().minusMonths(SOME_INT.next());
091    }
092
093    /**
094     * @return value of ZonedDateTime one year ago
095     */
096    public static ZonedDateTime someYearsAgo() {
097        return now().minusYears(SOME_INT.next());
098    }
099
100    /**
101     * @return value of ZonedDateTime with date somewhere 10 years ago
102     */
103    public static ZonedDateTime lastTenYearsAgo() {
104        return now().minusYears(SOME_INT.next()).minusMonths(SOME_INT.next()).minusDays(SOME_INT.next());
105    }
106
107    /**
108     * @return value of ZonedDateTime with date somewhere lastMonth
109     */
110    public static ZonedDateTime lastMonthAgo() {
111        return now().minusMinutes(SOME_INT.next()).minusHours(SOME_INT.next()).minusDays(SOME_INT.next());
112    }
113}