01: /*
02: * @(#)toLowerCase.java 1.2 05/01/19
03: *
04: * Copyright (c) 2005 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.Context;
12: import pnuts.lang.PnutsFunction;
13:
14: public class toLowerCase extends PnutsFunction {
15:
16: public toLowerCase() {
17: super ("toLowerCase");
18: }
19:
20: public boolean defined(int narg) {
21: return narg == 1;
22: }
23:
24: protected Object exec(Object[] args, Context context) {
25: if (args.length != 1) {
26: undefined(args, context);
27: return null;
28: }
29: Object arg = args[0];
30: if (arg instanceof String) {
31: return ((String) arg).toLowerCase();
32: } else if (arg instanceof Character) {
33: return new Character(Character
34: .toLowerCase(((Character) arg).charValue()));
35: } else if (arg instanceof CharSequence) {
36: return new LowerCaseCharSequence((CharSequence) arg);
37: } else if (arg == null) {
38: return null;
39: } else {
40: throw new IllegalArgumentException(String.valueOf(arg));
41: }
42: }
43:
44: public String toString() {
45: return "function toLowerCase(char|String|CharSequence)";
46: }
47:
48: static class LowerCaseCharSequence implements CharSequence {
49: private CharSequence base;
50:
51: LowerCaseCharSequence(CharSequence base) {
52: this .base = base;
53: }
54:
55: public int length() {
56: return base.length();
57: }
58:
59: public char charAt(int index) {
60: return Character.toLowerCase(base.charAt(index));
61: }
62:
63: public CharSequence subSequence(int start, int end) {
64: return new LowerCaseCharSequence(base.subSequence(start,
65: end));
66: }
67:
68: public String toString() {
69: return base.toString().toLowerCase();
70: }
71: }
72: }
|