01: /*
02: * @(#)DateElementAdd.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import java.util.Calendar;
12: import java.util.Date;
13: import pnuts.lang.PnutsFunction;
14: import pnuts.lang.Context;
15:
16: class DateElementAdd extends PnutsFunction {
17: private int element;
18:
19: DateElementAdd(int element, String name) {
20: super (name);
21: this .element = element;
22: }
23:
24: public boolean defined(int nargs) {
25: return nargs == 2;
26: }
27:
28: protected Object exec(Object[] args, Context context) {
29: if (args.length != 2) {
30: undefined(args, context);
31: return null;
32: }
33: Date d = (Date) args[0];
34: int n = ((Number) args[1]).intValue();
35: Calendar c = date.getCalendar(context);
36: synchronized (c) {
37: c.setTime(d);
38: c.add(element, n);
39: return c.getTime();
40: }
41: }
42:
43: public String toString() {
44: return "function " + name + "(Date, int)";
45: }
46: }
|