src/java.naming/share/classes/javax/naming/directory/BasicAttributes.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 48587 f2e87b6383af
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48587
f2e87b6383af 8191142: More refactoring for naming deserialization cases
vtewari
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.naming.directory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
    31
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.NamingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.NamingEnumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  * This class provides a basic implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  * of the Attributes interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  * BasicAttributes is either case-sensitive or case-insensitive (case-ignore).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  * This property is determined at the time the BasicAttributes constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  * is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  * In a case-insensitive BasicAttributes, the case of its attribute identifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  * is ignored when searching for an attribute, or adding attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  * In a case-sensitive BasicAttributes, the case is significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  * When the BasicAttributes class needs to create an Attribute, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  * uses BasicAttribute. There is no other dependency on BasicAttribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  * Note that updates to BasicAttributes (such as adding or removing an attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  * does not affect the corresponding representation in the directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  * Updates to the directory can only be effected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  * using operations in the DirContext interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  * A BasicAttributes instance is not synchronized against concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  * multithreaded access. Multiple threads trying to access and modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  * a single BasicAttributes instance should lock the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  * @see DirContext#getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  * @see DirContext#modifyAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  * @see DirContext#bind
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  * @see DirContext#rebind
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
  * @see DirContext#createSubcontext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
  * @see DirContext#search
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public class BasicAttributes implements Attributes {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Indicates whether case of attribute ids is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private boolean ignoreCase = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // The 'key' in attrs is stored in the 'right case'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // If ignoreCase is true, key is aways lowercase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // If ignoreCase is false, key is stored as supplied by put().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // %%% Not declared "private" due to bug 4064984.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    82
    transient Hashtable<String,Attribute> attrs = new Hashtable<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
      * Constructs a new instance of Attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
      * The character case of attribute identifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
      * is significant when subsequently retrieving or adding attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public BasicAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
      * Constructs a new instance of Attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
      * If <code>ignoreCase</code> is true, the character case of attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
      * identifiers is ignored; otherwise the case is significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      * @param ignoreCase true means this attribute set will ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      *                   the case of its attribute identifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
      *                   when retrieving or adding attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      *                   false means case is respected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public BasicAttributes(boolean ignoreCase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.ignoreCase = ignoreCase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      * Constructs a new instance of Attributes with one attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
      * The attribute specified by attrID and val are added to the newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      * created attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
      * The character case of attribute identifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      * is significant when subsequently retrieving or adding attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
      * @param attrID   non-null The id of the attribute to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
      * @param val The value of the attribute to add. If null, a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
      *        value is added to the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public BasicAttributes(String attrID, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.put(new BasicAttribute(attrID, val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
      * Constructs a new instance of Attributes with one attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
      * The attribute specified by attrID and val are added to the newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      * created attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
      * If <code>ignoreCase</code> is true, the character case of attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
      * identifiers is ignored; otherwise the case is significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
      * @param attrID   non-null The id of the attribute to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
      *           If this attribute set ignores the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
      *           case of its attribute ids, the case of attrID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
      *           is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
      * @param val The value of the attribute to add. If null, a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
      *        value is added to the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
      * @param ignoreCase true means this attribute set will ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
      *                   the case of its attribute identifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
      *                   when retrieving or adding attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
      *                   false means case is respected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public BasicAttributes(String attrID, Object val, boolean ignoreCase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this(ignoreCase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.put(new BasicAttribute(attrID, val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   142
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        BasicAttributes attrset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            attrset = (BasicAttributes)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            attrset = new BasicAttributes(ignoreCase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   150
        attrset.attrs = (Hashtable<String,Attribute>)attrs.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return attrset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public boolean isCaseIgnored() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return ignoreCase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return attrs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public Attribute get(String attrID) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   163
        Attribute attr = attrs.get(
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   164
                ignoreCase ? attrID.toLowerCase(Locale.ENGLISH) : attrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return (attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public NamingEnumeration<Attribute> getAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return new AttrEnumImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public NamingEnumeration<String> getIDs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return new IDEnumImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public Attribute put(String attrID, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return this.put(new BasicAttribute(attrID, val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public Attribute put(Attribute attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        String id = attr.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (ignoreCase) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   183
            id = id.toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   185
        return attrs.put(id, attr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public Attribute remove(String attrID) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   189
        String id = (ignoreCase ? attrID.toLowerCase(Locale.ENGLISH) : attrID);
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   190
        return attrs.remove(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Generates the string representation of this attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * The string consists of each attribute identifier and the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * of each attribute. The contents of this string is useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * for debugging and is not meant to be interpreted programmatically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return A non-null string listing the contents of this attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (attrs.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return("No attributes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return attrs.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   210
     * Determines whether this {@code BasicAttributes} is equal to another
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   211
     * {@code Attributes}
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   212
     * Two {@code Attributes} are equal if they are both instances of
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   213
     * {@code Attributes},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * treat the case of attribute IDs the same way, and contain the
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   215
     * same attributes. Each {@code Attribute} in this {@code BasicAttributes}
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   216
     * is checked for equality using {@code Object.equals()}, which may have
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   217
     * be overridden by implementations of {@code Attribute}).
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   218
     * If a subclass overrides {@code equals()},
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   219
     * it should override {@code hashCode()}
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   220
     * as well so that two {@code Attributes} instances that are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * have the same hash code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param obj the possibly null object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @return true If obj is equal to this BasicAttributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @see #hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if ((obj != null) && (obj instanceof Attributes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            Attributes target = (Attributes)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // Check case first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (ignoreCase != target.isCaseIgnored()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (size() == target.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                Attribute their, mine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                try {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   239
                    NamingEnumeration<?> theirs = target.getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    while (theirs.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        their = (Attribute)theirs.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        mine = get(their.getID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        if (!their.equals(mine)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Calculates the hash code of this BasicAttributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * The hash code is computed by adding the hash code of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * the attributes of this object. If this BasicAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * ignores case of its attribute IDs, one is added to the hash code.
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   262
     * If a subclass overrides {@code hashCode()},
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   263
     * it should override {@code equals()}
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   264
     * as well so that two {@code Attributes} instances that are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * have the same hash code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return an int representing the hash code of this BasicAttributes instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @see #equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int hash = (ignoreCase ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        try {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   273
            NamingEnumeration<?> all = getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            while (all.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                hash += all.next().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } catch (NamingException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Overridden to avoid exposing implementation details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @serialData Default field (ignoreCase flag -- a boolean), followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * the number of attributes in the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * (an int), and then the individual Attribute objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        s.defaultWriteObject(); // write out the ignoreCase flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        s.writeInt(attrs.size());
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   291
        Enumeration<Attribute> attrEnum = attrs.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        while (attrEnum.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            s.writeObject(attrEnum.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Overridden to avoid exposing implementation details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        s.defaultReadObject();  // read in the ignoreCase flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int n = s.readInt();    // number of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        attrs = (n >= 1)
48587
f2e87b6383af 8191142: More refactoring for naming deserialization cases
vtewari
parents: 47216
diff changeset
   305
                ? new Hashtable<>(1 + (int) (Math.min(768, n) / .75f))
f2e87b6383af 8191142: More refactoring for naming deserialization cases
vtewari
parents: 47216
diff changeset
   306
                : new Hashtable<>(2); // can't have initial size of 0 (grrr...)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        while (--n >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            put((Attribute)s.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
class AttrEnumImpl implements NamingEnumeration<Attribute> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    Enumeration<Attribute> elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public AttrEnumImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        this.elements = attrs.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return elements.hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public Attribute nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return elements.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public boolean hasMore() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public Attribute next() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public void close() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        elements = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
class IDEnumImpl implements NamingEnumeration<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    Enumeration<Attribute> elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public IDEnumImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // Walking through the elements, rather than the keys, gives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // us attribute IDs that have not been converted to lowercase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        this.elements = attrs.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return elements.hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public String nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        Attribute attr = elements.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return attr.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public boolean hasMore() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public String next() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public void close() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        elements = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Use serialVersionUID from JNDI 1.1.1 for interoperability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    private static final long serialVersionUID = 4980164073184639448L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
}