SimpleAttributeSet.java 12 KB
Newer Older
Tom Tromey committed
1
/* SimpleAttributeSet.java --
2
   Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
Tom Tromey committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package javax.swing.text;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;

45 46 47
/**
 * A set of attributes.
 */
Tom Tromey committed
48 49 50
public class SimpleAttributeSet
  implements MutableAttributeSet, Serializable, Cloneable
{
51 52 53
  /** The serialization UID (compatible with JDK1.5). */
  private static final long serialVersionUID = 8267656273837665219L;

54 55 56 57
  /**
   * An empty attribute set.
   */
  public static final AttributeSet EMPTY = new EmptyAttributeSet();
Tom Tromey committed
58

59
  /** Storage for the attributes. */
Tom Tromey committed
60 61
  Hashtable tab;

62 63 64
  /**
   * Creates a new attribute set that is initially empty.
   */
Tom Tromey committed
65 66
  public SimpleAttributeSet()
  {
67
    tab = new Hashtable();
Tom Tromey committed
68
  }
69

70 71 72
  /**
   * Creates a new <code>SimpleAttributeSet</code> with the same attributes
   * and resolve parent as the specified set.
73
   *
74
   * @param a  the attributes (<code>null</code> not permitted).
75
   *
76 77
   * @throws NullPointerException if <code>a</code> is <code>null</code>.
   */
Tom Tromey committed
78 79 80
  public SimpleAttributeSet(AttributeSet a)
  {
    tab = new Hashtable();
81
    addAttributes(a);
Tom Tromey committed
82 83
  }

84 85 86 87
  /**
   * Adds an attribute with the given <code>name</code> and <code>value</code>
   * to the set.  If the set already contains an attribute with the given
   * <code>name</code>, the attribute value is updated.
88
   *
89 90
   * @param name  the attribute name (<code>null</code> not permitted).
   * @param value  the value (<code>null</code> not permitted).
91
   *
92 93
   * @throws NullPointerException if either argument is <code>null</code>.
   */
Tom Tromey committed
94 95 96 97 98
  public void addAttribute(Object name, Object value)
  {
    tab.put(name, value);
  }

99 100
  /**
   * Adds all the attributes from <code>attributes</code> to this set.
101
   *
102 103
   * @param attributes  the set of attributes to add (<code>null</code> not
   *                    permitted).
104 105
   *
   * @throws NullPointerException if <code>attributes</code> is
106 107
   *         <code>null</code>.
   */
Tom Tromey committed
108 109 110 111 112 113 114 115 116 117 118
  public void addAttributes(AttributeSet attributes)
  {
    Enumeration e = attributes.getAttributeNames();
    while (e.hasMoreElements())
      {
        Object name = e.nextElement();
        Object val = attributes.getAttribute(name);
        tab.put(name, val);
      }
  }

119 120
  /**
   * Returns a clone of the attribute set.
121
   *
122 123
   * @return A clone of the attribute set.
   */
Tom Tromey committed
124 125
  public Object clone()
  {
126 127 128 129 130 131 132 133 134 135 136
    SimpleAttributeSet attr = null;
    try
      {
        attr = (SimpleAttributeSet) super.clone();
        attr.tab = (Hashtable) tab.clone();
      }
    catch (CloneNotSupportedException ex)
      {
        assert false;
      }
    return attr;
Tom Tromey committed
137 138
  }

139 140 141 142 143 144 145 146
  /**
   * Returns true if the given name and value represent an attribute
   * found either in this AttributeSet or in its resolve parent hierarchy.
   * @param name the key for the attribute
   * @param value the value for the attribute
   * @return true if the attribute is found here or in this set's resolve
   * parent hierarchy
   */
Tom Tromey committed
147 148
  public boolean containsAttribute(Object name, Object value)
  {
149 150 151 152 153 154 155 156 157 158 159 160
    if (value == null)
      throw new NullPointerException("Null 'value' argument.");
    if (tab.containsKey(name))
      return tab.get(name).equals(value);
    else
      {
        AttributeSet p = getResolveParent();
        if (p != null)
          return p.containsAttribute(name, value);
        else
          return false;
      }
161
  }
162

163 164 165 166 167 168 169 170 171
  /**
   * Returns true if the given name and value are found in this AttributeSet.
   * Does not check the resolve parent.
   * @param name the key for the attribute
   * @param value the value for the attribute
   * @return true if the attribute is found in this AttributeSet
   */
  boolean containsAttributeLocally(Object name, Object value)
  {
172
    return tab.containsKey(name)
Tom Tromey committed
173 174
      && tab.get(name).equals(value);
  }
175

176 177 178 179 180 181 182 183 184
  /**
   * Returns <code>true</code> of this <code>AttributeSet</code> contains all
   * of the specified <code>attributes</code>.
   *
   * @param attributes the requested attributes
   *
   * @return <code>true</code> of this <code>AttributeSet</code> contains all
   *         of the specified <code>attributes</code>
   */
Tom Tromey committed
185 186 187 188 189 190 191 192
  public boolean containsAttributes(AttributeSet attributes)
  {
    Enumeration e = attributes.getAttributeNames();
    while (e.hasMoreElements())
      {
        Object name = e.nextElement();
        Object val = attributes.getAttribute(name);
        if (! containsAttribute(name, val))
193
          return false;
Tom Tromey committed
194 195 196 197
      }
    return true;
  }

198 199 200 201 202
  /**
   * Creates and returns a copy of this <code>AttributeSet</code>.
   *
   * @return a copy of this <code>AttributeSet</code>
   */
Tom Tromey committed
203 204 205 206 207
  public AttributeSet copyAttributes()
  {
    return (AttributeSet) clone();
  }

208 209
  /**
   * Checks this set for equality with an arbitrary object.
210
   *
211
   * @param obj  the object (<code>null</code> permitted).
212
   *
213
   * @return <code>true</code> if this set is equal to <code>obj</code>, and
214
   *         <code>false</code> otherwise.
215
   */
Tom Tromey committed
216 217
  public boolean equals(Object obj)
  {
218
    return
219 220
      (obj instanceof AttributeSet)
      && this.isEqual((AttributeSet) obj);
Tom Tromey committed
221 222
  }

223
  /**
224
   * Returns the value of the specified attribute, or <code>null</code> if
225 226 227
   * there is no attribute with that name.  If the attribute is not defined
   * directly in this set, the parent hierarchy (if there is one) will be
   * used.
228
   *
229
   * @param name  the attribute (<code>null</code> not permitted).
230
   *
231 232
   * @throws NullPointerException if <code>name</code> is <code>null</code>.
   */
Tom Tromey committed
233 234 235
  public Object getAttribute(Object name)
  {
    Object val = tab.get(name);
236
    if (val != null)
Tom Tromey committed
237 238
      return val;

239 240 241
    AttributeSet p = getResolveParent();
    if (p != null)
      return p.getAttribute(name);
Tom Tromey committed
242 243 244 245

    return null;
  }

246 247
  /**
   * Returns the number of attributes stored in this set, plus 1 if a parent
248
   * has been specified (the reference to the parent is stored as a special
249 250
   * attribute).  The attributes stored in the parent do NOT contribute
   * to the count.
251
   *
252 253
   * @return The attribute count.
   */
Tom Tromey committed
254 255 256 257 258
  public int getAttributeCount()
  {
    return tab.size();
  }

259 260
  /**
   * Returns an enumeration of the attribute names.
261
   *
262 263
   * @return An enumeration of the attribute names.
   */
264
  public Enumeration<?> getAttributeNames()
Tom Tromey committed
265 266 267 268
  {
    return tab.keys();
  }

269 270
  /**
   * Returns the resolving parent.
271
   *
272
   * @return The resolving parent (possibly <code>null</code>).
273
   *
274 275
   * @see #setResolveParent(AttributeSet)
   */
Tom Tromey committed
276 277 278 279 280
  public AttributeSet getResolveParent()
  {
    return (AttributeSet) tab.get(ResolveAttribute);
  }

281 282
  /**
   * Returns a hash code for this instance.
283
   *
284 285
   * @return A hash code.
   */
Tom Tromey committed
286 287 288 289 290
  public int hashCode()
  {
    return tab.hashCode();
  }

291 292 293 294
  /**
   * Returns <code>true</code> if the given attribute is defined in this set,
   * and <code>false</code> otherwise.  The parent attribute set is not
   * checked.
295
   *
296 297
   * @param attrName  the attribute name (<code>null</code> not permitted).
   */
Tom Tromey committed
298 299 300 301 302
  public boolean isDefined(Object attrName)
  {
    return tab.containsKey(attrName);
  }

303
  /**
304 305 306
   * Returns <code>true</code> if the set contains no attributes, and
   * <code>false</code> otherwise.  Note that the resolving parent is
   * stored as an attribute, so this method will return <code>false</code> if
307
   * a resolving parent is set.
308 309
   *
   * @return <code>true</code> if the set contains no attributes, and
310 311
   * <code>false</code> otherwise.
   */
Tom Tromey committed
312 313
  public boolean isEmpty()
  {
314
    return tab.isEmpty();
Tom Tromey committed
315
  }
316 317 318 319

  /**
   * Returns true if the given set has the same number of attributes
   * as this set and <code>containsAttributes(attr)</code> returns
320
   * <code>true</code>.
321
   *
322
   * @param attr  the attribute set (<code>null</code> not permitted).
323
   *
324
   * @return A boolean.
325
   *
326
   * @throws NullPointerException if <code>attr</code> is <code>null</code>.
327
   */
Tom Tromey committed
328 329
  public boolean isEqual(AttributeSet attr)
  {
330 331
    return getAttributeCount() == attr.getAttributeCount()
      && this.containsAttributes(attr);
Tom Tromey committed
332
  }
333

334
  /**
335
   * Removes the attribute with the specified <code>name</code>, if this
336 337
   * attribute is defined.  This method will only remove an attribute from
   * this set, not from the resolving parent.
338
   *
339
   * @param name  the attribute name (<code>null</code> not permitted).
340
   *
341 342
   * @throws NullPointerException if <code>name</code> is <code>null</code>.
   */
Tom Tromey committed
343 344 345 346 347
  public void removeAttribute(Object name)
  {
    tab.remove(name);
  }

348
  /**
349
   * Removes attributes from this set if they are found in the
350
   * given set.  Only attributes whose key AND value are removed.
351 352
   * Removes attributes only from this set, not from the resolving parent.
   * Since the resolving parent is stored as an attribute, if
353 354
   * <code>attributes</code> has the same resolving parent as this set, the
   * parent will be removed from this set.
355
   *
356
   * @param attributes  the attributes (<code>null</code> not permitted).
357
   */
Tom Tromey committed
358 359
  public void removeAttributes(AttributeSet attributes)
  {
360 361 362 363 364 365
    Enumeration e = attributes.getAttributeNames();
    while (e.hasMoreElements())
      {
        Object name = e.nextElement();
        Object val = attributes.getAttribute(name);
        if (containsAttributeLocally(name, val))
366
          removeAttribute(name);
367
      }
Tom Tromey committed
368 369
  }

370 371
  /**
   * Removes the attributes listed in <code>names</code>.
372
   *
373
   * @param names  the attribute names (<code>null</code> not permitted).
374 375
   *
   * @throws NullPointerException if <code>names</code> is <code>null</code>
376 377
   *         or contains any <code>null</code> values.
   */
378
  public void removeAttributes(Enumeration<?> names)
Tom Tromey committed
379 380 381 382
  {
    while (names.hasMoreElements())
      {
        removeAttribute(names.nextElement());
383
      }
Tom Tromey committed
384 385
  }

386 387 388 389 390
  /**
   * Sets the reolving parent for this set.  When looking up an attribute, if
   * it is not found in this set, then the resolving parent is also used for
   * the lookup.
   * <p>
391 392 393
   * Note that the parent is stored as an attribute, and will contribute 1 to
   * the count returned by {@link #getAttributeCount()}.
   *
394
   * @param parent  the parent attribute set (<code>null</code> not permitted).
395
   *
396
   * @throws NullPointerException if <code>parent</code> is <code>null</code>.
397
   *
398 399
   * @see #setResolveParent(AttributeSet)
   */
Tom Tromey committed
400 401 402 403
  public void setResolveParent(AttributeSet parent)
  {
    addAttribute(ResolveAttribute, parent);
  }
404

405 406 407
  /**
   * Returns a string representation of this instance, typically used for
   * debugging purposes.
408
   *
409 410
   * @return A string representation of this instance.
   */
Tom Tromey committed
411 412 413
  public String toString()
  {
    return tab.toString();
414
  }
Tom Tromey committed
415
}