org.thymeleaf.extras.java8time.util.TemporalSetUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thymeleaf-extras-java8time Show documentation
Show all versions of thymeleaf-extras-java8time Show documentation
Modern server-side Java template engine for both web and standalone environments
/*
* =============================================================================
*
* 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 extends Temporal> 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 extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::day);
}
public Set setMonth(final Set extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::month);
}
public Set setMonthName(final Set extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::monthName);
}
public Set setMonthNameShort(final Set extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::monthNameShort);
}
public Set setYear(final Set extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::year);
}
public Set setDayOfWeek(final Set extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::dayOfWeek);
}
public Set setDayOfWeekName(final Set extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::dayOfWeekName);
}
public Set setDayOfWeekNameShort(final Set extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::dayOfWeekNameShort);
}
public Set setHour(final Set extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::hour);
}
public Set setMinute(final Set extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::minute);
}
public Set setSecond(final Set extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::second);
}
public Set setNanosecond(final Set extends Temporal> target) {
return setFormat(target, temporalFormattingUtils::nanosecond);
}
public Set setFormatISO(final Set extends Temporal> 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());
}
}