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
FrameworkBenchmarks - Source code for the framework benchmarking project... 2017-02-22
buildbot - Python-based continuous integration testing framew... 2017-02-22
rufus - The Reliable USB Formatting Utility ... 2017-02-22
uiGradients - Beautiful colour gradients for design and code ... 2017-02-22
Haxl - A Haskell library that simplifies access to remote... 2017-02-21
duktape - Duktape - embeddable Javascript engine with a focu... 2017-02-21
scala-best-practices - A collection of Scala best practice 2017-02-21
hammerspoon - Staggeringly powerful OS X desktop automation with... 2017-02-21
DGElasticPullToRefresh - Elastic pull to refresh for iOS developed in Swift 2017-02-21
evercookie - evercookie is a javascript API that produces extre... 2017-02-21

 Back to top