1. /*
  2. * Copyright 1999-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.jxpath.ri.compiler;
  17. import org.apache.commons.jxpath.ri.EvalContext;
  18. /**
  19. * A compile tree element containing a constant number or string.
  20. *
  21. * @author Dmitri Plotnikov
  22. * @version $Revision: 1.8 $ $Date: 2004/02/29 14:17:39 $
  23. */
  24. public class Constant extends Expression {
  25. private Object value;
  26. public Constant(Number number) {
  27. this.value = number;
  28. }
  29. public Constant(String string) {
  30. this.value = string;
  31. }
  32. public Object compute(EvalContext context) {
  33. return value;
  34. }
  35. /**
  36. * Returns the value of the constant.
  37. */
  38. public Object computeValue(EvalContext context) {
  39. return value;
  40. }
  41. /**
  42. * Returns false
  43. */
  44. public boolean isContextDependent() {
  45. return false;
  46. }
  47. /**
  48. * Returns false
  49. */
  50. public boolean computeContextDependent() {
  51. return false;
  52. }
  53. public String toString() {
  54. if (value instanceof Number) {
  55. double doubleValue = ((Number) value).doubleValue();
  56. long longValue = ((Number) value).longValue();
  57. if (doubleValue == longValue) {
  58. return String.valueOf(longValue);
  59. }
  60. else {
  61. return String.valueOf(doubleValue);
  62. }
  63. }
  64. else {
  65. return "'" + value + "'";
  66. }
  67. }
  68. }