attributes.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 626 行 · 第 1/2 页

JAVA
626
字号
     * Another object is equal to this Name object if it is an instance of
     * Name and the (lowercase) string representation of the name is equal.
     */
    public boolean equals(Object o)
    {
      // Quick and dirty check
      if (name == o)
	return true;

      try
	{
	  // Note that the constructor already converts the strings to
	  // lowercase.
	  String otherName = ((Name) o).name;
	  return name.equals(otherName);
	}
      catch (ClassCastException cce)
	{
	  return false;
	}
      catch (NullPointerException npe)
	{
	  return false;
	}
    }

    /**
     * Returns the string representation of this Name as given to the
     * constructor (not neccesarily the lower case representation).
     */
    public String toString()
    {
      return origName;
    }
  }

  // Constructors

  /**
   * Creates an empty Attributes map.
   */
  public Attributes()
  {
    map = new Hashtable();
  }

  /**
   * Creates an empty Attributes map with the given initial size.
   * @param size the initial size of the underlying map
   */
  public Attributes(int size)
  {
    map = new Hashtable(size);
  }

  /**
   * Creates an Attributes map with the initial values taken from another
   * Attributes map.
   * @param attr Attributes map to take the initial values from
   */
  public Attributes(Attributes attr)
  {
    map = new Hashtable(attr.map);
  }

  // Methods

  /**
   * Gets the value of an attribute name given as a String.
   *
   * @param name a String describing the Name to look for
   * @return the value gotten from the map of null when not found
   */
  public String getValue(String name)
  {
    return (String) get(new Name(name));
  }

  /**
   * Gets the value of the given attribute name.
   *
   * @param name the Name to look for
   * @return the value gotten from the map of null when not found
   */
  public String getValue(Name name)
  {
    return (String) get(name);
  }

  /**
   * Stores an attribute name (represented by a String) and value in this
   * Attributes map.
   * When the (case insensitive string) name already exists the value is
   * replaced and the old value is returned.
   *
   * @param name a (case insensitive) String representation of the attribite
   * name to add/replace
   * @param value the (new) value of the attribute name
   * @returns the old value of the attribute name or null if it didn't exist
   * yet
   */
  public String putValue(String name, String value)
  {
    return putValue(new Name(name), value);
  }

  /**
   * Stores an attribute name (represented by a String) and value in this
   * Attributes map.
   * When the name already exists the value is replaced and the old value
   * is returned.
   * <p>
   * I don't know why there is no public method with this signature. I think
   * there should be one.
   *
   * @param name the attribite name to add/replace
   * @param value the (new) value of the attribute name
   * @returns the old value of the attribute name or null if it didn't exist
   * yet
   */
  String putValue(Name name, String value)
  {
    return (String) put(name, value);
  }

  // Methods from Cloneable interface

  /**
   * Return a clone of this attribute map.
   */
  public Object clone()
  {
    return new Attributes(this);
  }

  // Methods from Map interface

  /**
   * Removes all attributes.
   */
  public void clear()
  {
    map.clear();
  }

  /**
   * Checks to see if there is an attribute with the specified name.
   * XXX - what if the object is a String?
   *
   * @param attrName the name of the attribute to check
   * @return true if there is an attribute with the specified name, false
   * otherwise
   */
  public boolean containsKey(Object attrName)
  {
    return map.containsKey(attrName);
  }

  /**
   * Checks to see if there is an attribute name with the specified value.
   *
   * @param attrValue the value of a attribute to check
   * @return true if there is an attribute name with the specified value,
   * false otherwise
   */
  public boolean containsValue(Object attrValue)
  {
    return map.containsValue(attrValue);
  }

  /**
   * Gives a Set of attribute name and values pairs as MapEntries.
   * @see java.util.Map.Entry
   * @see java.util.Map#entrySet()
   *
   * @return a set of attribute name value pairs
   */
  public Set entrySet()
  {
    return map.entrySet();
  }

  /**
   * Checks to see if two Attributes are equal. The supplied object must be
   * a real instance of Attributes and contain the same attribute name/value
   * pairs.
   *
   * @param o another Attribute object which should be checked for equality
   * @return true if the object is an instance of Attributes and contains the
   * same name/value pairs, false otherwise
   */
  public boolean equals(Object o)
  {
    // quick and dirty check
    if (this == o)
      return true;

    try
      {
	return map.equals(((Attributes) o).map);
      }
    catch (ClassCastException cce)
      {
	return false;
      }
    catch (NullPointerException npe)
      {
	return false;
      }
  }

  /**
   * Gets the value of a specified attribute name.
   * XXX - what if the object is a String?
   *
   * @param attrName the name of the attribute we want the value of
   * @return the value of the specified attribute name or null when there is
   * no such attribute name
   */
  public Object get(Object attrName)
  {
    return map.get(attrName);
  }

  /**
   * Returns the hashcode of the attribute name/value map.
   */
  public int hashCode()
  {
    return map.hashCode();
  }

  /**
   * Returns true if there are no attributes set, false otherwise.
   */
  public boolean isEmpty()
  {
    return map.isEmpty();
  }

  /**
   * Gives a Set of all the values of defined attribute names.
   */
  public Set keySet()
  {
    return map.keySet();
  }

  /**
   * Adds or replaces a attribute name/value pair.
   * XXX - What if the name is a string? What if the name is neither a Name
   * nor a String? What if the value is not a string?
   *
   * @param name the name of the attribute
   * @param value the (new) value of the attribute
   * @return the old value of the attribute or null when there was no old
   * attribute with this name
   */
  public Object put(Object name, Object value)
  {
    return map.put(name, value);
  }

  /**
   * Adds or replaces all attribute name/value pairs from another
   * Attributes object to this one. The supplied Map must be an instance of
   * Attributes.
   *
   * @param attr the Attributes object to merge with this one
   * @exception ClassCastException if the supplied map is not an instance of
   * Attributes
   */
  public void putAll(Map attr)
  {
    if (!(attr instanceof Attributes))
      {
	throw new
	  ClassCastException("Supplied Map is not an instance of Attributes");
      }
    map.putAll(attr);
  }

  /**
   * Remove a attribute name/value pair.
   * XXX - What if the name is a String?
   *
   * @param name the name of the attribute name/value pair to remove
   * @return the old value of the attribute or null if the attribute didn't
   * exist
   */
  public Object remove(Object name)
  {
    return map.remove(name);
  }

  /**
   * Returns the number of defined attribute name/value pairs.
   */
  public int size()
  {
    return map.size();
  }

  /**
   * Returns all the values of the defined attribute name/value pairs as a
   * Collection.
   */
  public Collection values()
  {
    return map.values();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?