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

org.thymeleaf.extras.java8time.util.TemporalSetUtils Maven / Gradle / Ivy

There is a newer version: 3.0.4.RELEASE
Show newest version
/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2014, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   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 org.thymeleaf.extras.java8time.util;

import java.time.ZoneId;
import java.time.temporal.Temporal;
import java.util.Locale;
import java.util.Set;
import java.util.function.Function;
import static java.util.stream.Collectors.toSet;
import org.thymeleaf.util.Validate;


/**
 * Formatting utilities for Java 8 Time object sets.
 *
 * @author José Miguel Samper
 *
 * @since 2.1.0
 */
public final class TemporalSetUtils {

    private final TemporalFormattingUtils temporalFormattingUtils;
    
    public TemporalSetUtils(final Locale locale, final ZoneId defaultZoneId) {
        super();
        Validate.notNull(locale, "Locale cannot be null");
        Validate.notNull(defaultZoneId, "ZoneId cannot be null");
        temporalFormattingUtils = new TemporalFormattingUtils(locale, defaultZoneId);
    }

    public Set setFormat(final Set target) {
        return setFormat(target, temporalFormattingUtils::format);
    }

    public  Set setFormat(final Set target, final Locale locale) {
        return setFormat(target, time -> temporalFormattingUtils.format(time, locale));
    }

    public  Set setFormat(final Set target, final String pattern) {
        return setFormat(target, time -> temporalFormattingUtils.format(time, pattern));
    }

    public  Set setFormat(final Set target, final String pattern, final Locale locale) {
        return setFormat(target, time -> temporalFormattingUtils.format(time, pattern, locale));
    }

    public Set setDay(final Set target) {
        return setFormat(target, temporalFormattingUtils::day);
    }

    public Set setMonth(final Set target) {
        return setFormat(target, temporalFormattingUtils::month);
    }

    public Set setMonthName(final Set target) {
        return setFormat(target, temporalFormattingUtils::monthName);
    }

    public Set setMonthNameShort(final Set target) {
        return setFormat(target, temporalFormattingUtils::monthNameShort);
    }

    public Set setYear(final Set target) {
        return setFormat(target, temporalFormattingUtils::year);
    }
    
    public Set setDayOfWeek(final Set target) {
        return setFormat(target, temporalFormattingUtils::dayOfWeek);
    }

    public Set setDayOfWeekName(final Set target) {
        return setFormat(target, temporalFormattingUtils::dayOfWeekName);
    }
    
    public Set setDayOfWeekNameShort(final Set target) {
        return setFormat(target, temporalFormattingUtils::dayOfWeekNameShort);
    }
    
    public Set setHour(final Set target) {
        return setFormat(target, temporalFormattingUtils::hour);
    }
    
    public Set setMinute(final Set target) {
        return setFormat(target, temporalFormattingUtils::minute);
    }
    
    public Set setSecond(final Set target) {
        return setFormat(target, temporalFormattingUtils::second);
    }
    
    public Set setNanosecond(final Set target) {
        return setFormat(target, temporalFormattingUtils::nanosecond);
    }

    public Set setFormatISO(final Set target) {
        return setFormat(target, temporalFormattingUtils::formatISO);
    }

    private  Set setFormat(
            final Set target, final Function mapFunction) {
        Validate.notNull(target, "Target cannot be null");
        return target.stream()
            .map(time -> mapFunction.apply(time))
            .collect(toSet());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy