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.strategy;
  17. import java.io.Serializable;
  18. import org.apache.commons.betwixt.expression.Context;
  19. /**
  20. * <p>Strategy class for string <-> object conversions.
  21. * Implementations of this interface are used by Betwixt to perform
  22. * string <-> object conversions.
  23. * This performs only the most basic conversions.
  24. * Most applications will use a subclass.
  25. * </p>
  26. * <p>It is strongly recommended that (in order to support round tripping)
  27. * that <code>objectToString</code> and <code>stringToObject</code>
  28. * are inverse functions.
  29. * In other words, given the same flavour, context and type the applying
  30. * objectToString to the result of stringToObject should be equal to the
  31. * original input.
  32. * </p>
  33. * @author Robert Burrell Donkin
  34. * @since 0.5
  35. */
  36. public class ObjectStringConverter implements Serializable {
  37. /**
  38. * Converts an object to a string representation.
  39. * This basic implementation returns object.toString()
  40. * or an empty string if the given object is null.
  41. *
  42. * @param object the object to be converted, possibly null
  43. * @param type the property class of the object, not null
  44. * @param flavour a string allow symantic differences in formatting to be communicated
  45. * @param context the context, not null
  46. * @return a String representation, not null
  47. */
  48. public String objectToString(Object object, Class type, String flavour, Context context) {
  49. if ( object != null ) {
  50. return object.toString();
  51. }
  52. return "";
  53. }
  54. /**
  55. * Converts a string representation to an object.
  56. * It is acceptable for an implementation to return the string if it cannot convert
  57. * the string to the given class type.
  58. * This basic implementation just returns a string.
  59. *
  60. * @param value the String to be converted
  61. * @param type the property class to be returned (if possible), not null
  62. * @param flavour a string allow symantic differences in formatting to be communicated
  63. * @param context the context, not null
  64. * @return an Object converted from the String, not null
  65. */
  66. public Object stringToObject(String value, Class type, String flavour, Context context) {
  67. return value;
  68. }
  69. }