View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  package org.apache.commons.betwixt.expression;
18  
19  /** <p><code>StringExpression</code> returns the current context object as a string.</p>
20    *
21    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
22    * @version $Revision: 438373 $
23    */
24  public class StringExpression implements Expression {
25      
26      /** We only need only <code>StringExpression</code> */
27      private static final StringExpression singleton = new StringExpression();
28      
29      /** 
30       * Gets the singleton 
31       * @return the singleton <code>StringExpression</code> instance
32       */
33      public static StringExpression getInstance() {
34          return singleton;
35      }
36      
37      /** Base constructor. Should this be private? */
38      public StringExpression() {
39      }
40      
41      /** Return the context bean as a string
42        *
43        * @param context evaluate expression against this context
44        * @return the <code>toString()</code> representation of the context bean
45        */
46      public Object evaluate(Context context) {
47          Object value = context.getBean();
48          if ( value != null ) {
49              return value.toString();
50          }
51          return null;
52      }
53      
54      /**
55       * Do nothing 
56       * @see org.apache.commons.betwixt.expression.Expression
57       */
58      public void update(Context context, String newValue) {
59          // do nothing
60      }
61      
62      /**
63       * Returns something useful for logging.
64       * @return the (short) class name
65       */
66      public String toString() {
67          return "StringExpression";
68      }
69  
70  }