KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > print > attribute > TextSyntax


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
8
9 package javax.print.attribute;
10
11 import java.io.Serializable JavaDoc;
12 import java.util.Locale JavaDoc;
13
14 /**
15  * Class TextSyntax is an abstract base class providing the common
16  * implementation of all attributes whose value is a string. The text attribute
17  * includes a locale to indicate the natural language. Thus, a text attribute
18  * always represents a localized string. Once constructed, a text attribute's
19  * value is immutable.
20  * <P>
21  *
22  * @author David Mendenhall
23  * @author Alan Kaminsky
24  */

25 public abstract class TextSyntax implements Serializable JavaDoc, Cloneable JavaDoc {
26
27     private static final long serialVersionUID = -8130648736378144102L;
28
29     /**
30      * String value of this text attribute.
31      * @serial
32      */

33     private String JavaDoc value;
34
35     /**
36      * Locale of this text attribute.
37      * @serial
38      */

39     private Locale JavaDoc locale;
40
41     /**
42      * Constructs a TextAttribute with the specified string and locale.
43      *
44      * @param value Text string.
45      * @param locale Natural language of the text string. null
46      * is interpreted to mean the default locale for as returned
47      * by <code>Locale.getDefault()</code>
48      *
49      * @exception NullPointerException
50      * (unchecked exception) Thrown if <CODE>value</CODE> is null.
51      */

52     protected TextSyntax(String JavaDoc value, Locale JavaDoc locale) {
53     this.value = verify (value);
54     this.locale = verify (locale);
55     }
56
57     private static String JavaDoc verify(String JavaDoc value) {
58     if (value == null) {
59         throw new NullPointerException JavaDoc(" value is null");
60     }
61     return value;
62     }
63
64     private static Locale JavaDoc verify(Locale JavaDoc locale) {
65     if (locale == null) {
66         return Locale.getDefault();
67     }
68     return locale;
69     }
70
71     /**
72      * Returns this text attribute's text string.
73      * @return the text string.
74      */

75     public String JavaDoc getValue() {
76     return value;
77     }
78
79     /**
80      * Returns this text attribute's text string's natural language (locale).
81      * @return the locale
82      */

83     public Locale JavaDoc getLocale() {
84     return locale;
85     }
86
87     /**
88      * Returns a hashcode for this text attribute.
89      *
90      * @return A hashcode value for this object.
91      */

92     public int hashCode() {
93     return value.hashCode() ^ locale.hashCode();
94     }
95
96     /**
97      * Returns whether this text attribute is equivalent to the passed in
98      * object. To be equivalent, all of the following conditions must be true:
99      * <OL TYPE=1>
100      * <LI>
101      * <CODE>object</CODE> is not null.
102      * <LI>
103      * <CODE>object</CODE> is an instance of class TextSyntax.
104      * <LI>
105      * This text attribute's underlying string and <CODE>object</CODE>'s
106      * underlying string are equal.
107      * <LI>
108      * This text attribute's locale and <CODE>object</CODE>'s locale are
109      * equal.
110      * </OL>
111      *
112      * @param object Object to compare to.
113      *
114      * @return True if <CODE>object</CODE> is equivalent to this text
115      * attribute, false otherwise.
116      */

117     public boolean equals(Object JavaDoc object) {
118     return(object != null &&
119            object instanceof TextSyntax JavaDoc &&
120            this.value.equals (((TextSyntax JavaDoc) object).value) &&
121            this.locale.equals (((TextSyntax JavaDoc) object).locale));
122     }
123
124     /**
125      * Returns a String identifying this text attribute. The String is
126      * the attribute's underlying text string.
127      *
128      * @return A String identifying this object.
129      */

130     public String JavaDoc toString(){
131     return value;
132     }
133     
134 }
135
Popular Tags