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