01: /*
02: * @(#)isEmpty.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 pnuts.lang.*;
12: import pnuts.lang.Runtime;
13: import pnuts.lang.Package;
14: import java.lang.reflect.Array;
15: import java.util.*;
16:
17: public class isEmpty extends PnutsFunction {
18:
19: public isEmpty() {
20: super ("isEmpty");
21: }
22:
23: public boolean defined(int nargs) {
24: return nargs == 1;
25: }
26:
27: protected Object exec(Object[] args, Context context) {
28: if (args.length != 1) {
29: undefined(args, context);
30: }
31: Object arg0 = args[0];
32: if (arg0 instanceof Collection) {
33: return ((Collection) arg0).isEmpty() ? Boolean.TRUE
34: : Boolean.FALSE;
35: } else if (arg0 instanceof Map) {
36: return ((Map) arg0).isEmpty() ? Boolean.TRUE
37: : Boolean.FALSE;
38: } else if (arg0 instanceof Object[]) {
39: return ((Object[]) arg0).length == 0 ? Boolean.TRUE
40: : Boolean.FALSE;
41: } else if (Runtime.isArray(arg0)) {
42: return Array.getLength(arg0) == 0 ? Boolean.TRUE
43: : Boolean.FALSE;
44: } else if (arg0 instanceof Package) {
45: return ((Package) arg0).size() == 0 ? Boolean.TRUE
46: : Boolean.FALSE;
47: } else {
48: throw new IllegalArgumentException(String.valueOf(arg0));
49: }
50: }
51:
52: public String toString() {
53: return "function isEmpty(collection)";
54: }
55: }
|