1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.json;
18
19 import java.io.Serializable;
20
21 import net.sf.json.util.JSONUtils;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.commons.lang.builder.EqualsBuilder;
25 import org.apache.commons.lang.builder.HashCodeBuilder;
26
27
28
29
30
31
32 public class JSONFunction implements Serializable {
33
34 private static final String[] EMPTY_PARAM_ARRAY = new String[0];
35
36
37
38
39
40 public static JSONFunction parse( String str ) {
41 if( !JSONUtils.isFunction( str ) ) {
42 throw new JSONException( "String is not a function. " + str );
43 } else {
44 String params = JSONUtils.getFunctionParams( str );
45 String text = JSONUtils.getFunctionBody( str );
46 return new JSONFunction( (params != null) ? StringUtils.split( params, "," ) : null, text != null ? text : "" );
47 }
48 }
49
50
51 private String[] params;
52
53
54 private String text;
55
56
57
58
59
60
61 public JSONFunction( String text ) {
62 this( null, text );
63 }
64
65
66
67
68
69
70
71 public JSONFunction( String[] params, String text ) {
72 this.text = (text != null) ? text.trim() : "";
73 if( params != null ){
74 if( params.length == 1 && params[0].trim()
75 .equals( "" ) ){
76 this.params = EMPTY_PARAM_ARRAY;
77 }else{
78 this.params = new String[params.length];
79 System.arraycopy( params, 0, this.params, 0, params.length );
80
81 for( int i = 0; i < params.length; i++ ){
82 this.params[i] = this.params[i].trim();
83 }
84 }
85 }else{
86 this.params = EMPTY_PARAM_ARRAY;
87 }
88 }
89
90 public boolean equals( Object obj ) {
91 if( this == obj ){
92 return true;
93 }
94 if( obj == null ){
95 return false;
96 }
97
98 if( obj instanceof String ){
99 try{
100 JSONFunction other = parse( (String) obj );
101 return equals( other );
102 }catch( JSONException e ){
103 return false;
104 }
105 }
106
107 if( !(obj instanceof JSONFunction) ){
108 return false;
109 }
110
111 JSONFunction other = (JSONFunction) obj;
112 if( params.length != other.params.length ){
113 return false;
114 }
115 EqualsBuilder builder = new EqualsBuilder();
116 for( int i = 0; i < params.length; i++ ){
117 builder.append( params[i], other.params[i] );
118 }
119 builder.append( text, other.text );
120 return builder.isEquals();
121 }
122
123
124
125
126 public String[] getParams() {
127 return params;
128 }
129
130
131
132
133 public String getText() {
134 return text;
135 }
136
137 public int hashCode() {
138 HashCodeBuilder builder = new HashCodeBuilder();
139 for( int i = 0; i < params.length; i++ ){
140 builder.append( params[i] );
141 }
142 builder.append( text );
143 return builder.toHashCode();
144 }
145
146
147
148
149 public String toString() {
150 StringBuffer b = new StringBuffer( "function(" );
151 if( params.length > 0 ){
152 for( int i = 0; i < params.length - 1; i++ ){
153 b.append( params[i] )
154 .append( ',' );
155 }
156 b.append( params[params.length - 1] );
157 }
158 b.append( "){" );
159 if( text.length() > 0 ){
160 b.append( ' ' )
161 .append( text )
162 .append( ' ' );
163 }
164 b.append( '}' );
165 return b.toString();
166 }
167 }