1 /*
   2  * Copyright (c) 2000, 2017, 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 package javax.print.attribute;
  27 
  28 import java.io.Serializable;
  29 import java.util.Locale;
  30 
  31 /**
  32  * Class {@code TextSyntax} is an abstract base class providing the common
  33  * implementation of all attributes whose value is a string. The text attribute
  34  * includes a locale to indicate the natural language. Thus, a text attribute
  35  * always represents a localized string. Once constructed, a text attribute's
  36  * value is immutable.
  37  *
  38  * @author David Mendenhall
  39  * @author Alan Kaminsky
  40  */
  41 public abstract class TextSyntax implements Serializable, Cloneable {
  42 
  43     /**
  44      * Use serialVersionUID from JDK 1.4 for interoperability.
  45      */
  46     private static final long serialVersionUID = -8130648736378144102L;
  47 
  48     /**
  49      * String value of this text attribute.
  50      *
  51      * @serial
  52      */
  53     private String value;
  54 
  55     /**
  56      * Locale of this text attribute.
  57      *
  58      * @serial
  59      */
  60     private Locale locale;
  61 
  62     /**
  63      * Constructs a {@code TextAttribute} with the specified string and locale.
  64      *
  65      * @param  value text string
  66      * @param  locale natural language of the text string. {@code null} is
  67      *         interpreted to mean the default locale for as returned by
  68      *         {@code Locale.getDefault()}
  69      * @throws NullPointerException if {@code value} is {@code null}
  70      */
  71     protected TextSyntax(String value, Locale locale) {
  72         this.value = verify (value);
  73         this.locale = verify (locale);
  74     }
  75 
  76     private static String verify(String value) {
  77         if (value == null) {
  78             throw new NullPointerException(" value is null");
  79         }
  80         return value;
  81     }
  82 
  83     private static Locale verify(Locale locale) {
  84         if (locale == null) {
  85             return Locale.getDefault();
  86         }
  87         return locale;
  88     }
  89 
  90     /**
  91      * Returns this text attribute's text string.
  92      *
  93      * @return the text string
  94      */
  95     public String getValue() {
  96         return value;
  97     }
  98 
  99     /**
 100      * Returns this text attribute's text string's natural language (locale).
 101      *
 102      * @return the locale
 103      */
 104     public Locale getLocale() {
 105         return locale;
 106     }
 107 
 108     /**
 109      * Returns a hashcode for this text attribute.
 110      *
 111      * @return a hashcode value for this object
 112      */
 113     public int hashCode() {
 114         return value.hashCode() ^ locale.hashCode();
 115     }
 116 
 117     /**
 118      * Returns whether this text attribute is equivalent to the passed in
 119      * object. To be equivalent, all of the following conditions must be true:
 120      * <ol type=1>
 121      *   <li>{@code object} is not {@code null}.
 122      *   <li>{@code object} is an instance of class {@code TextSyntax}.
 123      *   <li>This text attribute's underlying string and {@code object}'s
 124      *   underlying string are equal.
 125      *   <li>This text attribute's locale and {@code object}'s locale are equal.
 126      * </ol>
 127      *
 128      * @param  object {@code Object} to compare to
 129      * @return {@code true} if {@code object} is equivalent to this text
 130      *         attribute, {@code false} otherwise
 131      */
 132     public boolean equals(Object object) {
 133         return(object != null &&
 134                object instanceof TextSyntax &&
 135                this.value.equals (((TextSyntax) object).value) &&
 136                this.locale.equals (((TextSyntax) object).locale));
 137     }
 138 
 139     /**
 140      * Returns a {@code String} identifying this text attribute. The
 141      * {@code String} is the attribute's underlying text string.
 142      *
 143      * @return a {@code String} identifying this object
 144      */
 145     public String toString(){
 146         return value;
 147     }
 148 }