diff --git a/jsf/pom.xml b/jsf/pom.xml index 98e90be..f791a38 100644 --- a/jsf/pom.xml +++ b/jsf/pom.xml @@ -1,26 +1,62 @@ - 4.0.0 + 4.0.0 - - org.ocpsoft.prettytime - prettytime-parent - 5.0.9-SNAPSHOT - ../pom.xml - + + org.ocpsoft.prettytime + prettytime-parent + 5.0.9-SNAPSHOT + ../pom.xml + - prettytime-integration-jsf + prettytime-integration-jsf - PrettyTime - Integration - JSF + PrettyTime - Integration - JSF - - - org.ocpsoft.prettytime - prettytime - - - javax.faces - jsf-api - provided - - + + + org.ocpsoft.prettytime + prettytime + + + javax.faces + jsf-api + provided + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.6.0 + + + jakarta + package + + shade + + + false + true + jakarta + false + + + ${project.groupId}:${project.artifactId} + + + + + javax.faces + jakarta.faces + + + + + + + + diff --git a/jsf/src/main/java/org/ocpsoft/prettytime/jsf/PrettyTimeConverter.java b/jsf/src/main/java/org/ocpsoft/prettytime/jsf/PrettyTimeConverter.java index 4d9dd27..4ff52d0 100644 --- a/jsf/src/main/java/org/ocpsoft/prettytime/jsf/PrettyTimeConverter.java +++ b/jsf/src/main/java/org/ocpsoft/prettytime/jsf/PrettyTimeConverter.java @@ -1,12 +1,12 @@ /* * Copyright 2012 Lincoln Baxter, III - * + * * 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. @@ -15,58 +15,81 @@ */ package org.ocpsoft.prettytime.jsf; - -import javax.faces.component.UIComponent; -import javax.faces.context.FacesContext; -import javax.faces.convert.Converter; -import javax.faces.convert.ConverterException; - +import org.ocpsoft.prettytime.Duration; import org.ocpsoft.prettytime.PrettyTime; import java.io.Serializable; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZonedDateTime; +import java.util.Calendar; import java.util.Date; import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; -public class PrettyTimeConverter implements Converter, Serializable -{ +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; +import javax.faces.convert.ConverterException; + +public class PrettyTimeConverter implements Converter, Serializable { + private static final long serialVersionUID = 7690470362440868260L; private static final int MAX_CACHE_SIZE = 20; // Cache PrettyTime per locale. LRU cache to prevent memory leak. - private static final Map PRETTY_TIME_LOCALE_MAP = new LinkedHashMap(MAX_CACHE_SIZE + 1, 1.1F, true) - { - private static final long serialVersionUID = 5093634937930600141L; + private static final Map PRETTY_TIME_LOCALE_MAP = new LinkedHashMap(MAX_CACHE_SIZE + 1, 1.1F, true) { + private static final long serialVersionUID = 5093634937930600141L; - @Override - protected boolean removeEldestEntry(Map.Entry eldest) - { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { return size() > MAX_CACHE_SIZE; } }; - - public Object getAsObject(final FacesContext context, final UIComponent comp, final String value) - { + public Object getAsObject(final FacesContext context, final UIComponent comp, final String value) { throw new ConverterException("Does not yet support converting String to Date"); } - public String getAsString(final FacesContext context, final UIComponent comp, final Object value) - { - if (value instanceof Date) - { - // Use locale of current viewer. - Locale locale = context.getViewRoot().getLocale(); - PrettyTime prettyTime; + public String getAsString(final FacesContext context, final UIComponent comp, final Object value) { + // Use locale of current viewer. + Locale locale = context.getViewRoot().getLocale(); + PrettyTime prettyTime; - synchronized (PRETTY_TIME_LOCALE_MAP) - { - prettyTime = PRETTY_TIME_LOCALE_MAP.computeIfAbsent(locale, PrettyTime::new); - } + synchronized (PRETTY_TIME_LOCALE_MAP) { + prettyTime = PRETTY_TIME_LOCALE_MAP.computeIfAbsent(locale, PrettyTime::new); + } + if (value instanceof Date) { return prettyTime.format((Date) value); } - throw new ConverterException("May only be used to convert java.util.Date objects. Got: " + (value != null ? value.getClass() : "null")); + if (value instanceof Calendar) { + return prettyTime.format((Calendar) value); + } + if (value instanceof Duration) { + return prettyTime.format((Duration) value); + } + if (value instanceof Instant) { + return prettyTime.format((Instant) value); + } + if (value instanceof ZonedDateTime) { + return prettyTime.format((ZonedDateTime) value); + } + if (value instanceof OffsetDateTime) { + return prettyTime.format((OffsetDateTime) value); + } + if (value instanceof LocalDateTime) { + return prettyTime.format((LocalDateTime) value); + } + if (value instanceof LocalDate) { + return prettyTime.format((LocalDate) value); + } + throw new ConverterException("May only be used to convert java.util.Date,java.util.Calendar," + + "org.ocpsoft.prettytime.Duration,java.time.Instant,java.time.ZonedDateTime," + + "java.time.OffsetDateTime,java.time.LocalDateTime,java.time.LocalDate objects. " + + "Got: " + (value != null ? value.getClass() : "null")); } }