BVB Source Codes

spring-mvc-showcase Show MaskFormatAnnotationFormatterFactory.java Source code

Return Download spring-mvc-showcase: download MaskFormatAnnotationFormatterFactory.java Source code - Download spring-mvc-showcase Source code - Type:.java
  1. package org.springframework.samples.mvc.convert;
  2.  
  3. import java.text.ParseException;
  4. import java.util.HashSet;
  5. import java.util.Locale;
  6. import java.util.Set;
  7.  
  8. import org.springframework.format.AnnotationFormatterFactory;
  9. import org.springframework.format.Formatter;
  10. import org.springframework.format.Parser;
  11. import org.springframework.format.Printer;
  12.  
  13. public class MaskFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<MaskFormat> {
  14.  
  15.         public Set<Class<?>> getFieldTypes() {
  16.                 Set<Class<?>> fieldTypes = new HashSet<Class<?>>(1, 1);
  17.                 fieldTypes.add(String.class);
  18.                 return fieldTypes;
  19.         }
  20.  
  21.         public Parser<?> getParser(MaskFormat annotation, Class<?> fieldType) {
  22.                 return new MaskFormatter(annotation.value());
  23.         }
  24.  
  25.         public Printer<?> getPrinter(MaskFormat annotation, Class<?> fieldType) {
  26.                 return new MaskFormatter(annotation.value());
  27.         }
  28.        
  29.         private static class MaskFormatter implements Formatter<String> {
  30.  
  31.                 private javax.swing.text.MaskFormatter delegate;
  32.  
  33.                 public MaskFormatter(String mask) {
  34.                         try {
  35.                                 this.delegate = new javax.swing.text.MaskFormatter(mask);
  36.                                 this.delegate.setValueContainsLiteralCharacters(false);
  37.                         } catch (ParseException e) {
  38.                                 throw new IllegalStateException("Mask could not be parsed " + mask, e);
  39.                         }
  40.                 }
  41.  
  42.                 public String print(String object, Locale locale) {
  43.                         try {
  44.                                 return delegate.valueToString(object);
  45.                         } catch (ParseException e) {
  46.                                 throw new IllegalArgumentException("Unable to print using mask " + delegate.getMask(), e);
  47.                         }
  48.                 }
  49.  
  50.                 public String parse(String text, Locale locale) throws ParseException {
  51.                         return (String) delegate.stringToValue(text);
  52.                 }
  53.  
  54.         }
  55.  
  56. }
  57.  
downloadMaskFormatAnnotationFormatterFactory.java Source code - Download spring-mvc-showcase Source code
Related Source Codes/Software:
tushare - TuShare is a utility for crawling historical data ... 2017-01-12
raml-spec - RAML Specification http://raml.... 2017-01-12
android-stackblur - Android StackBlur is a library that can perform a ... 2017-01-12
sound-redux - A Soundcloud client built with React / Redux ... 2017-01-12
httpstat - curl statistics made simple 2017-01-12
discover-flask - Full Stack Web Development with Flask. ... 2017-01-12
micro - Async HTTP microservices made easy ... 2017-01-12
gobot - Golang framework for robotics, drones, and the Int... 2017-01-11
mas - 2017-01-13
Zappa - Serverless Python Web Services ... 2017-01-12
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top