1 11 package org.jboss.portlet.forums.helper; 12 13 import java.text.DateFormat ; 14 import java.text.SimpleDateFormat ; 15 import java.util.Date ; 16 17 24 public final class SimpleDateFormatPool 25 extends AbstractPool 26 { 27 30 private int version = 0; 31 32 35 private String pattern = "EEE MMM d, yyyy HH:mm aaa"; 36 37 43 public SimpleDateFormatPool(int maxSize, 44 int initialSize) 45 { 46 initialize(maxSize, initialSize); 47 } 48 49 54 public void setPattern(String format) 55 { 56 version++; 57 this.pattern = format; 58 } 59 60 65 public String getPattern() 66 { 67 return pattern; 68 } 69 70 75 protected Object _create() 76 { 77 return new SimpleDateFormatPool.VersionedSimpleDateFormat(pattern, version); 78 } 79 80 85 protected void _acquire(Object instance) 86 { 87 SimpleDateFormatPool.VersionedSimpleDateFormat format = 88 (SimpleDateFormatPool.VersionedSimpleDateFormat) instance; 89 if (format.version < version) 90 { 91 format.version = version; 92 format.applyPattern(pattern); 93 } 94 } 95 96 private static class VersionedSimpleDateFormat 97 extends SimpleDateFormat 98 { 99 private int version; 100 101 107 public VersionedSimpleDateFormat(String pattern, 108 int version) 109 { 110 super(pattern); 111 this.version = version; 112 } 113 } 114 115 122 public String formatDate(Date date) 123 { 124 DateFormat format = null; 125 try 126 { 127 format = (DateFormat ) acquire(); 128 return format.format(date); 129 } 130 finally 131 { 132 release(format); 133 } 134 } 135 } | Popular Tags |