1. /*
  2. * @(#)TextSyntax.java 1.7 04/01/07
  3. *
  4. * Copyright 2004 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. private static final long serialVersionUID = -8130648736378144102L;
  23. /**
  24. * String value of this text attribute.
  25. * @serial
  26. */
  27. private String value;
  28. /**
  29. * Locale of this text attribute.
  30. * @serial
  31. */
  32. private Locale locale;
  33. /**
  34. * Constructs a TextAttribute with the specified string and locale.
  35. *
  36. * @param value Text string.
  37. * @param locale Natural language of the text string. null
  38. * is interpreted to mean the default locale for as returned
  39. * by <code>Locale.getDefault()</code>
  40. *
  41. * @exception NullPointerException
  42. * (unchecked exception) Thrown if <CODE>value</CODE> is null.
  43. */
  44. protected TextSyntax(String value, Locale locale) {
  45. this.value = verify (value);
  46. this.locale = verify (locale);
  47. }
  48. private static String verify(String value) {
  49. if (value == null) {
  50. throw new NullPointerException(" value is null");
  51. }
  52. return value;
  53. }
  54. private static Locale verify(Locale locale) {
  55. if (locale == null) {
  56. return Locale.getDefault();
  57. }
  58. return locale;
  59. }
  60. /**
  61. * Returns this text attribute's text string.
  62. * @return the text string.
  63. */
  64. public String getValue() {
  65. return value;
  66. }
  67. /**
  68. * Returns this text attribute's text string's natural language (locale).
  69. * @return the locale
  70. */
  71. public Locale getLocale() {
  72. return locale;
  73. }
  74. /**
  75. * Returns a hashcode for this text attribute.
  76. *
  77. * @return A hashcode value for this object.
  78. */
  79. public int hashCode() {
  80. return value.hashCode() ^ locale.hashCode();
  81. }
  82. /**
  83. * Returns whether this text attribute is equivalent to the passed in
  84. * object. To be equivalent, all of the following conditions must be true:
  85. * <OL TYPE=1>
  86. * <LI>
  87. * <CODE>object</CODE> is not null.
  88. * <LI>
  89. * <CODE>object</CODE> is an instance of class TextSyntax.
  90. * <LI>
  91. * This text attribute's underlying string and <CODE>object</CODE>'s
  92. * underlying string are equal.
  93. * <LI>
  94. * This text attribute's locale and <CODE>object</CODE>'s locale are
  95. * equal.
  96. * </OL>
  97. *
  98. * @param object Object to compare to.
  99. *
  100. * @return True if <CODE>object</CODE> is equivalent to this text
  101. * attribute, false otherwise.
  102. */
  103. public boolean equals(Object object) {
  104. return(object != null &&
  105. object instanceof TextSyntax &&
  106. this.value.equals (((TextSyntax) object).value) &&
  107. this.locale.equals (((TextSyntax) object).locale));
  108. }
  109. /**
  110. * Returns a String identifying this text attribute. The String is
  111. * the attribute's underlying text string.
  112. *
  113. * @return A String identifying this object.
  114. */
  115. public String toString(){
  116. return value;
  117. }
  118. }