001: /*
002: * @(#)contains.java 1.3 05/01/14
003: *
004: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.lib;
010:
011: import pnuts.lang.*;
012: import pnuts.lang.Runtime;
013: import java.util.*;
014: import java.lang.reflect.Array;
015:
016: public class contains extends PnutsFunction {
017:
018: public contains() {
019: super ("contains");
020: }
021:
022: public boolean defined(int nargs) {
023: return nargs == 2;
024: }
025:
026: protected Object exec(Object[] args, Context context) {
027: if (args.length != 2) {
028: undefined(args, context);
029: }
030: Object target = args[0];
031: final Object key = args[1];
032: if (target instanceof Collection) {
033: Collection col = (Collection) target;
034: return col.contains(args[1]) ? Boolean.TRUE : Boolean.FALSE;
035: } else if (target instanceof String) {
036: String str = (String) target;
037: if (key instanceof String) {
038: if (str.indexOf((String) key) >= 0) {
039: return Boolean.TRUE;
040: } else {
041: return Boolean.FALSE;
042: }
043: } else if (key instanceof Character) {
044: if (str.indexOf(((Character) key).charValue()) >= 0) {
045: return Boolean.TRUE;
046: } else {
047: return Boolean.FALSE;
048: }
049: }
050: } else if (target instanceof CharSequence) {
051: if (key instanceof CharSequence) {
052: if (check((CharSequence) target, (CharSequence) key)) {
053: return Boolean.TRUE;
054: } else {
055: return Boolean.FALSE;
056: }
057: } else if (key instanceof Character) {
058: if (check((CharSequence) target, ((Character) key)
059: .charValue())) {
060: return Boolean.TRUE;
061: } else {
062: return Boolean.FALSE;
063: }
064: }
065: } else if (target instanceof Object[]) {
066: Object[] array = (Object[]) target;
067: if (key != null) {
068: for (int i = 0; i < array.length; i++) {
069: if (key.equals(array[i])) {
070: return Boolean.TRUE;
071: }
072: }
073: return Boolean.FALSE;
074: } else {
075: for (int i = 0; i < array.length; i++) {
076: if (array[i] == null) {
077: return Boolean.TRUE;
078: }
079: }
080: return Boolean.FALSE;
081: }
082: } else if (Runtime.isArray(target)) {
083: int len = Array.getLength(target);
084: if (key != null) {
085: for (int i = 0; i < len; i++) {
086: if (key.equals(Array.get(target, i))) {
087: return Boolean.TRUE;
088: }
089: }
090: return Boolean.FALSE;
091: } else {
092: for (int i = 0; i < len; i++) {
093: if (Array.get(target, i) == null) {
094: return Boolean.TRUE;
095: }
096: }
097: return Boolean.FALSE;
098: }
099: }
100: throw new IllegalArgumentException(String.valueOf(target));
101:
102: }
103:
104: static boolean check(CharSequence target, CharSequence key) {
105: int i = 0;
106: int target_len = target.length();
107: int key_len = key.length();
108: int max = target_len - key_len;
109: if (key_len < 1) {
110: return true;
111: }
112: char first = key.charAt(0);
113:
114: startSearchForFirstChar: while (true) {
115: while (i <= max && target.charAt(i) != first) {
116: i++;
117: }
118: if (i > max) {
119: return false;
120: }
121: int j = i + 1;
122: int end = j + key_len - 1;
123: int k = 1;
124: while (j < end) {
125: if (target.charAt(j++) != key.charAt(k++)) {
126: i++;
127: continue startSearchForFirstChar;
128: }
129: }
130: return true;
131: }
132: }
133:
134: static boolean check(CharSequence target, char key) {
135: int len = target.length();
136: for (int i = 0; i < len; i++) {
137: if (target.charAt(i) == key) {
138: return true;
139: }
140: }
141: return false;
142: }
143:
144: public String toString() {
145: return "function contains(collection|array|string, elem)";
146: }
147: }
|