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

com.vaadin.flow.data.renderer.LocalDateRenderer Maven / Gradle / Ivy

There is a newer version: 24.5.5
Show newest version
/*
 * Copyright 2000-2024 Vaadin Ltd.
 *
 * 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.vaadin.flow.data.renderer;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

import com.vaadin.flow.function.SerializableSupplier;
import com.vaadin.flow.function.ValueProvider;

/**
 * A template renderer for presenting date values.
 *
 * @author Vaadin Ltd
 *
 * @param 
 *            the type of the input item, from which the {@link LocalDate} is
 *            extracted
 */
public class LocalDateRenderer
        extends BasicRenderer {

    private SerializableSupplier formatter;
    private String nullRepresentation;

    /**
     * Creates a new LocalDateRenderer.
     * 

* The renderer is configured with the format style {@code FormatStyle.LONG} * and an empty string as its null representation. * * @param valueProvider * the callback to provide a {@link LocalDate} to the renderer, * not null * * @see * FormatStyle.LONG */ public LocalDateRenderer(ValueProvider valueProvider) { this(valueProvider, () -> DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG), ""); } /** * Creates a new LocalDateRenderer. *

* The renderer is configured to render with the given string format, with * an empty string as its null representation. * * @param valueProvider * the callback to provide a {@link LocalDate} to the renderer, * not null * * @param formatPattern * the format pattern to format the date with, not * null * * @see * Format Pattern Syntax */ public LocalDateRenderer(ValueProvider valueProvider, String formatPattern) { this(valueProvider, formatPattern, Locale.getDefault()); } /** * Creates a new LocalDateRenderer. *

* The renderer is configured to render with the given string format, as * displayed in the given locale, with an empty string as its null * representation. * * @param valueProvider * the callback to provide a {@link LocalDate} to the renderer, * not null * @param formatPattern * the format pattern to format the date with, not * null * @param locale * the locale to use, not null * * @see * Format Pattern Syntax */ public LocalDateRenderer(ValueProvider valueProvider, String formatPattern, Locale locale) { this(valueProvider, formatPattern, locale, ""); } /** * Creates a new LocalDateRenderer. *

* The renderer is configured to render with the given string format, as * displayed in the given locale. * * @param valueProvider * the callback to provide a {@link LocalDate} to the renderer, * not null * @param formatPattern * the format pattern to format the date with, not * null * @param locale * the locale to use, not null * @param nullRepresentation * the textual representation of the null value * * @see * Format Pattern Syntax */ public LocalDateRenderer(ValueProvider valueProvider, String formatPattern, Locale locale, String nullRepresentation) { super(valueProvider); if (formatPattern == null) { throw new IllegalArgumentException( "format pattern may not be null"); } if (locale == null) { throw new IllegalArgumentException("locale may not be null"); } formatter = () -> DateTimeFormatter.ofPattern(formatPattern, locale); this.nullRepresentation = nullRepresentation; } /** * Creates a new LocalDateRenderer. *

* The renderer is configured to render with the given formatter, with an * empty string as its null representation. * * @param valueProvider * the callback to provide a {@link LocalDate} to the renderer, * not null * @param formatter * the formatter to use, not null * @deprecated Via this constructor renderer is not serializable, use * {@link LocalDateRenderer(ValueProvider, * SerializableSupplier)} instead. */ @Deprecated public LocalDateRenderer(ValueProvider valueProvider, DateTimeFormatter formatter) { this(valueProvider, () -> formatter, ""); } /** * Creates a new LocalDateRenderer. *

* The renderer is configured to render with the given formatter, with an * empty string as its null representation. * * @param valueProvider * the callback to provide a {@link LocalDate} to the renderer, * not null * @param formatter * the formatter to use, not null */ public LocalDateRenderer(ValueProvider valueProvider, SerializableSupplier formatter) { this(valueProvider, formatter, ""); } /** * Creates a new LocalDateRenderer. *

* The renderer is configured to render with the given formatter. * * @param valueProvider * the callback to provide a {@link LocalDate} to the renderer, * not null * @param formatter * the formatter to use, not null * @param nullRepresentation * the textual representation of the null value * @deprecated Via this constructor renderer is not serializable, use * {@link LocalDateRenderer(ValueProvider, SerializableSupplier, * String)} instead. * */ @Deprecated public LocalDateRenderer(ValueProvider valueProvider, DateTimeFormatter formatter, String nullRepresentation) { this(valueProvider, () -> formatter, nullRepresentation); } /** * Creates a new LocalDateRenderer. *

* The renderer is configured to render with the given formatter. * * @param valueProvider * the callback to provide a {@link LocalDate} to the renderer, * not null * @param formatter * the formatter to use, not null * @param nullRepresentation * the textual representation of the null value * */ public LocalDateRenderer(ValueProvider valueProvider, SerializableSupplier formatter, String nullRepresentation) { super(valueProvider); if (formatter == null) { throw new IllegalArgumentException("formatter may not be null"); } this.formatter = formatter; this.nullRepresentation = nullRepresentation; } @Override protected String getFormattedValue(LocalDate date) { try { return date == null ? nullRepresentation : formatter.get().format(date); } catch (Exception e) { throw new IllegalStateException("Could not format input date '" + date + "' using formatter '" + formatter + "'", e); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy