alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (Formats.java)

This example Play Framework source code file (Formats.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

data, date, dateformatter, datetime, field, format, locale, nonempty, play, play framework, retention, simpledateformat, string, target

The Formats.java Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.data.format;

import java.text.*;
import java.util.*;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;

import java.lang.annotation.*;

/**
 * Defines several default formatters.
 */
public class Formats {
    
    // -- DATE
    
    /**
     * Formatter for <code>java.util.Date</code> values.
     */
    public static class DateFormatter extends Formatters.SimpleFormatter<Date> {
        
        private final String pattern;
        
        /**
         * Creates a date formatter.
         *
         * @param pattern date pattern, as specified for {@link SimpleDateFormat}.
         */
        public DateFormatter(String pattern) {
            this.pattern = pattern;
        }
        
        /**
         * Binds the field - constructs a concrete value from submitted data.
         *
         * @param text the field text
         * @param locale the current <code>Locale</code>
         * @return a new value
         */
        public Date parse(String text, Locale locale) throws java.text.ParseException {
            if(text == null || text.trim().isEmpty()) {
                return null;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
            sdf.setLenient(false);  
            return sdf.parse(text);
        }
        
        /**
         * Unbinds this fields - converts a concrete value to a plain string.
         *
         * @param value the value to unbind
         * @param locale the current <code>Locale</code>
         * @return printable version of the value
         */
        public String print(Date value, Locale locale) {
            if(value == null) {
                return "";
            }
            return new SimpleDateFormat(pattern, locale).format(value);
        }
        
    }
    
    /**
     * Defines the format for a <code>Date</code> field.
     */
    @Target({FIELD})
    @Retention(RUNTIME)
    @play.data.Form.Display(name="format.date", attributes={"pattern"})
    public static @interface DateTime {
        
        /**
         * Date pattern, as specified for {@link SimpleDateFormat}.
         */
        String pattern();
    }
    
    /**
     * Annotation formatter, triggered by the <code>@DateTime</code> annotation.
     */
    public static class AnnotationDateFormatter extends Formatters.AnnotationFormatter<DateTime,Date> {
        
        /**
         * Binds the field - constructs a concrete value from submitted data.
         *
         * @param annotation the annotation that trigerred this formatter
         * @param text the field text
         * @param locale the current <code>Locale</code>
         * @return a new value
         */
        public Date parse(DateTime annotation, String text, Locale locale) throws java.text.ParseException {
            if(text == null || text.trim().isEmpty()) {
                return null;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(annotation.pattern(), locale);
            sdf.setLenient(false);  
            return sdf.parse(text);
        }
        
        /**
         * Unbinds this field - converts a concrete value to plain string
         *
         * @param annotation the annotation that trigerred this formatter
         * @param value the value to unbind
         * @param locale the current <code>Locale</code>
         * @return printable version of the value
         */
        public String print(DateTime annotation, Date value, Locale locale) {
            if(value == null) {
                return "";
            }
            return new SimpleDateFormat(annotation.pattern(), locale).format(value);
        }
        
    }
    
    // -- STRING
    
    /**
     * Defines the format for a <code>String</code> field that cannot be empty.
     */
    @Target({FIELD})
    @Retention(RUNTIME)
    public static @interface NonEmpty {}
    
    /**
     * Annotation formatter, triggered by the <code>@NonEmpty</code> annotation.
     */
    public static class AnnotationNonEmptyFormatter extends Formatters.AnnotationFormatter<NonEmpty,String> {
        
        /**
         * Binds the field - constructs a concrete value from submitted data.
         *
         * @param annotation the annotation that trigerred this formatter
         * @param text the field text
         * @param locale the current <code>Locale</code>
         * @return a new value
         */
        public String parse(NonEmpty annotation, String text, Locale locale) throws java.text.ParseException {
            if(text == null || text.trim().isEmpty()) {
                return null;
            }
            return text;
        }
        
        /**
         * Unbinds this field - converts a concrete value to plain string
         *
         * @param annotation the annotation that trigerred this formatter
         * @param value the value to unbind
         * @param locale the current <code>Locale</code>
         * @return printable version of the value
         */
        public String print(NonEmpty annotation, String value, Locale locale) {
            if(value == null) {
                return "";
            }
            return value;
        }
        
    }
    
    
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework Formats.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.