1 /*
   2  * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package javax.print.attribute;
  28 
  29 import java.io.Serializable;
  30 import java.util.Locale;
  31 
  32 /**
  33  * Class TextSyntax is an abstract base class providing the common
  34  * implementation of all attributes whose value is a string. The text attribute
  35  * includes a locale to indicate the natural language. Thus, a text attribute
  36  * always represents a localized string. Once constructed, a text attribute's
  37  * value is immutable.
  38  *
  39  * @author  David Mendenhall
  40  * @author  Alan Kaminsky
  41  */
  42 public abstract class TextSyntax implements Serializable, Cloneable {
  43 
  44     private static final long serialVersionUID = -8130648736378144102L;
  45 
  46     /**
  47      * String value of this text attribute.
  48      * @serial
  49      */
  50     private String value;
  51 
  52     /**
  53      * Locale of this text attribute.
  54      * @serial
  55      */
  56     private Locale locale;
  57 
  58     /**
  59      * Constructs a TextAttribute with the specified string and locale.
  60      *
  61      * @param  value   Text string.
  62      * @param  locale  Natural language of the text string. null
  63      * is interpreted to mean the default locale for as returned
  64      * by <code>Locale.getDefault()</code>
  65      *
  66      * @exception  NullPointerException
  67      *     (unchecked exception) Thrown if <CODE>value</CODE> is null.
  68      */
  69     protected TextSyntax(String value, Locale locale) {
  70         this.value = verify (value);
  71         this.locale = verify (locale);
  72     }
  73 
  74     private static String verify(String value) {
  75         if (value == null) {
  76             throw new NullPointerException(" value is null");
  77         }
  78         return value;
  79     }
  80 
  81     private static Locale verify(Locale locale) {
  82         if (locale == null) {
  83             return Locale.getDefault();
  84         }
  85         return locale;
  86     }
  87 
  88     /**
  89      * Returns this text attribute's text string.
  90      * @return the text string.
  91      */
  92     public String getValue() {
  93         return value;
  94     }
  95 
  96     /**
  97      * Returns this text attribute's text string's natural language (locale).
  98      * @return the locale
  99      */
 100     public Locale getLocale() {
 101         return locale;
 102     }
 103 
 104     /**
 105      * Returns a hashcode for this text attribute.
 106      *
 107      * @return  A hashcode value for this object.
 108      */
 109     public int hashCode() {
 110         return value.hashCode() ^ locale.hashCode();
 111     }
 112 
 113     /**
 114      * Returns whether this text attribute is equivalent to the passed in
 115      * object. To be equivalent, all of the following conditions must be true:
 116      * <OL TYPE=1>
 117      * <LI>
 118      * <CODE>object</CODE> is not null.
 119      * <LI>
 120      * <CODE>object</CODE> is an instance of class TextSyntax.
 121      * <LI>
 122      * This text attribute's underlying string and <CODE>object</CODE>'s
 123      * underlying string are equal.
 124      * <LI>
 125      * This text attribute's locale and <CODE>object</CODE>'s locale are
 126      * equal.
 127      * </OL>
 128      *
 129      * @param  object  Object to compare to.
 130      *
 131      * @return  True if <CODE>object</CODE> is equivalent to this text
 132      *          attribute, false otherwise.
 133      */
 134     public boolean equals(Object object) {
 135         return(object != null &&
 136                object instanceof TextSyntax &&
 137                this.value.equals (((TextSyntax) object).value) &&
 138                this.locale.equals (((TextSyntax) object).locale));
 139     }
 140 
 141     /**
 142      * Returns a String identifying this text attribute. The String is
 143      * the attribute's underlying text string.
 144      *
 145      * @return  A String identifying this object.
 146      */
 147     public String toString(){
 148         return value;
 149     }
 150 
 151 }