001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.betwixt.expression;
018
019 /** <p><code>StringExpression</code> returns the current context object as a string.</p>
020 *
021 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
022 * @version $Revision: 438373 $
023 */
024 public class StringExpression implements Expression {
025
026 /** We only need only <code>StringExpression</code> */
027 private static final StringExpression singleton = new StringExpression();
028
029 /**
030 * Gets the singleton
031 * @return the singleton <code>StringExpression</code> instance
032 */
033 public static StringExpression getInstance() {
034 return singleton;
035 }
036
037 /** Base constructor. Should this be private? */
038 public StringExpression() {
039 }
040
041 /** Return the context bean as a string
042 *
043 * @param context evaluate expression against this context
044 * @return the <code>toString()</code> representation of the context bean
045 */
046 public Object evaluate(Context context) {
047 Object value = context.getBean();
048 if ( value != null ) {
049 return value.toString();
050 }
051 return null;
052 }
053
054 /**
055 * Do nothing
056 * @see org.apache.commons.betwixt.expression.Expression
057 */
058 public void update(Context context, String newValue) {
059 // do nothing
060 }
061
062 /**
063 * Returns something useful for logging.
064 * @return the (short) class name
065 */
066 public String toString() {
067 return "StringExpression";
068 }
069
070 }