1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.concepts.fn;
25
26 import java.util.*;
27 import zeus.util.*;
28 import zeus.concepts.*;
29
30 public class DateFn extends ValueFunction implements PrimitiveFn {
31 protected String arg = null;
32 protected Calendar calendar = null;
33
34 public DateFn(String arg) {
35 super(DATE,10);
36 this.arg = arg;
37
38 int index1 = arg.indexOf('-');
39 if ( index1 == -1 )
40 index1 = arg.indexOf('/');
41 if ( index1 == 1 )
42 this.arg = "0" + this.arg;
43 }
44 public ValueFunction mirror() {
45 return new DateFn(arg);
46 }
47 public String toString() {
48 return arg;
49 }
50 Object getArg(int position) {
51 if (position != 0) throw new ArrayIndexOutOfBoundsException(position);
52 return arg;
53 }
54 public boolean isDeterminate() {
55 return true;
56 }
57 public boolean equals(Object any) {
58 if ( !(any instanceof DateFn) ) return false;
59 DateFn fn = (DateFn)any;
60 Calendar c1 = getCalendar();
61 Calendar c2 = fn.getCalendar();
62 return c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH) &&
63 c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH) &&
64 c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR);
65 }
66 public boolean less(Object any) {
67 DateFn fn = (DateFn)any;
68 Calendar c1 = getCalendar();
69 Calendar c2 = fn.getCalendar();
70 return c1.before(c2);
71 }
72 ValueFunction unify(ValueFunction fn, Bindings b) {
73 return null;
74 }
75 public boolean references(ValueFunction var) {
76 return false;
77 }
78
79 Calendar getCalendar() {
80 if ( calendar != null ) return calendar;
81
82 StringTokenizer st = new StringTokenizer(arg,"-/");
83 int day = Integer.parseInt(st.nextToken());
84 int month = Integer.parseInt(st.nextToken());
85 int year = Integer.parseInt(st.nextToken());
86 calendar = new GregorianCalendar(year,month-1,day);
87 return calendar;
88 }
89
90
91
92
93
94 static final String[] METHOD_LIST = {
95 "day", "0",
96 "dayOfWeek", "0",
97 "month", "0",
98 "nameOfMonth", "0",
99 "year", "0",
100 "interval", "1"
101 };
102
103 static final int DAY = 0;
104 static final int DAY_OF_WEEK = 2;
105 static final int MONTH = 4;
106 static final int NAME_OF_MONTH = 6;
107 static final int YEAR = 8;
108 static final int INTERVAL = 10;
109
110 protected static final String[] DAY_LIST = {
111 "Sunday", "Monday", "Tuesday", "Wednesday",
112 "Thursday", "Friday", "Saturday"
113 };
114
115 protected static final String[] MONTH_LIST = {
116 "January", "February", "March", "April", "May", "June",
117 "July", "August", "September", "October", "November", "December"
118 };
119
120 ValueFunction invokeMethod(String method, Vector args) {
121 int position = Misc.whichPosition(method,METHOD_LIST);
122
123 if ( position == -1 )
124 return super.invokeMethod(method,args);
125
126 int arity = Integer.parseInt(METHOD_LIST[position+1]);
127 if ( args.size() != arity )
128 throw new IllegalArgumentException(
129 "Wrong number of arguments in method \'" + method + "/" +
130 arity + "\'.");
131
132 try {
133 getCalendar();
134 switch( position ) {
135 case DAY:
136 return new IntFn(calendar.get(Calendar.DAY_OF_MONTH));
137
138 case MONTH:
139 return new IntFn(calendar.get(Calendar.MONTH)+1);
140
141 case YEAR:
142 return new IntFn(calendar.get(Calendar.YEAR));
143
144 case DAY_OF_WEEK:
145 return new IdFn(DAY_LIST[calendar.get(Calendar.DAY_OF_WEEK)-1]);
146
147 case NAME_OF_MONTH:
148 return new IdFn(MONTH_LIST[calendar.get(Calendar.MONTH)]);
149
150 case INTERVAL:
151 DateFn date = (DateFn)args.elementAt(0);
152 Calendar cal = date.getCalendar();
153 int x = calendar.get(Calendar.DAY_OF_YEAR);
154 int y = cal.get(Calendar.DAY_OF_YEAR);
155 return new IntFn(Math.abs(x-y));
156 }
157 }
158 catch(ClassCastException e) {
159 throw new IllegalArgumentException(
160 "Type mismatch in method \'" + this + Fact.A_CHR + method +
161 "(...)\'");
162
163 }
164 Core.ERROR(null,1,this);
165 return null;
166 }
167
168 }