1. /*
  2. * @(#)TextSyntax.java 1.5 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.print.attribute;
  8. import java.io.Serializable;
  9. import java.util.Locale;
  10. /**
  11. * Class TextSyntax is an abstract base class providing the common
  12. * implementation of all attributes whose value is a string. The text attribute
  13. * includes a locale to indicate the natural language. Thus, a text attribute
  14. * always represents a localized string. Once constructed, a text attribute's
  15. * value is immutable.
  16. * <P>
  17. *
  18. * @author David Mendenhall
  19. * @author Alan Kaminsky
  20. */
  21. public abstract class TextSyntax implements Serializable, Cloneable {
  22. /**
  23. * String value of this text attribute.
  24. * @serial
  25. */
  26. private String value;
  27. /**
  28. * Locale of this text attribute.
  29. * @serial
  30. */
  31. private Locale locale;
  32. /**
  33. * Constructs a TextAttribute with the specified string and locale.
  34. *
  35. * @param value Text string.
  36. * @param locale Natural language of the text string. null
  37. * is interpreted to mean the default locale for as returned
  38. * by <code>Locale.getDefault()</code>
  39. *
  40. * @exception NullPointerException
  41. * (unchecked exception) Thrown if <CODE>value</CODE> is null.
  42. */
  43. protected TextSyntax(String value, Locale locale) {
  44. this.value = verify (value);
  45. this.locale = verify (locale);
  46. }
  47. private static String verify(String value) {
  48. if (value == null) {
  49. throw new NullPointerException(" value is null");
  50. }
  51. return value;
  52. }
  53. private static Locale verify(Locale locale) {
  54. if (locale == null) {
  55. return Locale.getDefault();
  56. }
  57. return locale;
  58. }
  59. /**
  60. * Returns this text attribute's text string.
  61. * @return the text string.
  62. */
  63. public String getValue() {
  64. return value;
  65. }
  66. /**
  67. * Returns this text attribute's text string's natural language (locale).
  68. * @return the locale
  69. */
  70. public Locale getLocale() {
  71. return locale;
  72. }
  73. /**
  74. * Returns a hashcode for this text attribute.
  75. *
  76. * @return A hashcode value for this object.
  77. */
  78. public int hashCode() {
  79. return value.hashCode() ^ locale.hashCode();
  80. }
  81. /**
  82. * Returns whether this text attribute is equivalent to the passed in
  83. * object. To be equivalent, all of the following conditions must be true:
  84. * <OL TYPE=1>
  85. * <LI>
  86. * <CODE>object</CODE> is not null.
  87. * <LI>
  88. * <CODE>object</CODE> is an instance of class TextSyntax.
  89. * <LI>
  90. * This text attribute's underlying string and <CODE>object</CODE>'s
  91. * underlying string are equal.
  92. * <LI>
  93. * This text attribute's locale and <CODE>object</CODE>'s locale are
  94. * equal.
  95. * </OL>
  96. *
  97. * @param object Object to compare to.
  98. *
  99. * @return True if <CODE>object</CODE> is equivalent to this text
  100. * attribute, false otherwise.
  101. */
  102. public boolean equals(Object object) {
  103. return(object != null &&
  104. object instanceof TextSyntax &&
  105. this.value.equals (((TextSyntax) object).value) &&
  106. this.locale.equals (((TextSyntax) object).locale));
  107. }
  108. /**
  109. * Returns a String identifying this text attribute. The String is
  110. * the attribute's underlying text string.
  111. *
  112. * @return A String identifying this object.
  113. */
  114. public String toString(){
  115. return value;
  116. }
  117. }