01: /*
02: * Created on Jul 8, 2005
03: */
04: package uk.org.ponder.timeutil;
05:
06: import java.util.Calendar;
07: import java.util.Date;
08:
09: /**
10: * @author Antranig Basman (antranig@caret.cam.ac.uk)
11: * Java Calendar objects are baroque and relatively expensive. This provides
12: * a Thread-local store of them, which is dipped into for various
13: * straightforward operations.
14: */
15:
16: public class CalendarStore {
17: // A "default" CalendarStore for those happy with the default.
18: // The behaviour of using this is identical to Calendar.instance()
19: // (unless you are living in Thailand) but it is now easy to
20: // provide an application-wide different default with only a little
21: // effort.
22: public static CalendarStore instance = new CalendarStore();
23:
24: public Calendar get() {
25: return (Calendar) store.get();
26: }
27:
28: public int getHour(Date toget) {
29: Calendar got = get();
30: got.setTime(toget);
31: return got.get(Calendar.HOUR_OF_DAY);
32: }
33:
34: private CalendarFactory factory = DefaultGregorianFactory.instance;
35: private ThreadLocal store = new ThreadLocal() {
36: public Object initialValue() {
37: return factory.getCalendar();
38: }
39: };
40:
41: public void setCalendarFactory(CalendarFactory factory) {
42: this.factory = factory;
43: }
44: }
|