This example Java source code file (Attributes.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.util.jar;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Collection;
import java.util.AbstractSet;
import java.util.Iterator;
import sun.util.logging.PlatformLogger;
import java.util.Comparator;
import sun.misc.ASCIICaseInsensitiveComparator;
/**
* The Attributes class maps Manifest attribute names to associated string
* values. Valid attribute names are case-insensitive, are restricted to
* the ASCII characters in the set [0-9a-zA-Z_-], and cannot exceed 70
* characters in length. Attribute values can contain any characters and
* will be UTF8-encoded when written to the output stream. See the
* <a href="../../../../technotes/guides/jar/jar.html">JAR File Specification
* for more information about valid attribute names and values.
*
* @author David Connelly
* @see Manifest
* @since 1.2
*/
public class Attributes implements Map<Object,Object>, Cloneable {
/**
* The attribute name-value mappings.
*/
protected Map<Object,Object> map;
/**
* Constructs a new, empty Attributes object with default size.
*/
public Attributes() {
this(11);
}
/**
* Constructs a new, empty Attributes object with the specified
* initial size.
*
* @param size the initial number of attributes
*/
public Attributes(int size) {
map = new HashMap<>(size);
}
/**
* Constructs a new Attributes object with the same attribute name-value
* mappings as in the specified Attributes.
*
* @param attr the specified Attributes
*/
public Attributes(Attributes attr) {
map = new HashMap<>(attr);
}
/**
* Returns the value of the specified attribute name, or null if the
* attribute name was not found.
*
* @param name the attribute name
* @return the value of the specified attribute name, or null if
* not found.
*/
public Object get(Object name) {
return map.get(name);
}
/**
* Returns the value of the specified attribute name, specified as
* a string, or null if the attribute was not found. The attribute
* name is case-insensitive.
* <p>
* This method is defined as:
* <pre>
* return (String)get(new Attributes.Name((String)name));
* </pre>
*
* @param name the attribute name as a string
* @return the String value of the specified attribute name, or null if
* not found.
* @throws IllegalArgumentException if the attribute name is invalid
*/
public String getValue(String name) {
return (String)get(new Attributes.Name(name));
}
/**
* Returns the value of the specified Attributes.Name, or null if the
* attribute was not found.
* <p>
* This method is defined as:
* <pre>
* return (String)get(name);
* </pre>
*
* @param name the Attributes.Name object
* @return the String value of the specified Attribute.Name, or null if
* not found.
*/
public String getValue(Name name) {
return (String)get(name);
}
/**
* Associates the specified value with the specified attribute name
* (key) in this Map. If the Map previously contained a mapping for
* the attribute name, the old value is replaced.
*
* @param name the attribute name
* @param value the attribute value
* @return the previous value of the attribute, or null if none
* @exception ClassCastException if the name is not a Attributes.Name
* or the value is not a String
*/
public Object put(Object name, Object value) {
return map.put((Attributes.Name)name, (String)value);
}
/**
* Associates the specified value with the specified attribute name,
* specified as a String. The attributes name is case-insensitive.
* If the Map previously contained a mapping for the attribute name,
* the old value is replaced.
* <p>
* This method is defined as:
* <pre>
* return (String)put(new Attributes.Name(name), value);
* </pre>
*
* @param name the attribute name as a string
* @param value the attribute value
* @return the previous value of the attribute, or null if none
* @exception IllegalArgumentException if the attribute name is invalid
*/
public String putValue(String name, String value) {
return (String)put(new Name(name), value);
}
/**
* Removes the attribute with the specified name (key) from this Map.
* Returns the previous attribute value, or null if none.
*
* @param name attribute name
* @return the previous value of the attribute, or null if none
*/
public Object remove(Object name) {
return map.remove(name);
}
/**
* Returns true if this Map maps one or more attribute names (keys)
* to the specified value.
*
* @param value the attribute value
* @return true if this Map maps one or more attribute names to
* the specified value
*/
public boolean containsValue(Object value) {
return map.containsValue(value);
}
/**
* Returns true if this Map contains the specified attribute name (key).
*
* @param name the attribute name
* @return true if this Map contains the specified attribute name
*/
public boolean containsKey(Object name) {
return map.containsKey(name);
}
/**
* Copies all of the attribute name-value mappings from the specified
* Attributes to this Map. Duplicate mappings will be replaced.
*
* @param attr the Attributes to be stored in this map
* @exception ClassCastException if attr is not an Attributes
*/
public void putAll(Map<?,?> attr) {
// ## javac bug?
if (!Attributes.class.isInstance(attr))
throw new ClassCastException();
for (Map.Entry<?,?> me : (attr).entrySet())
put(me.getKey(), me.getValue());
}
/**
* Removes all attributes from this Map.
*/
public void clear() {
map.clear();
}
/**
* Returns the number of attributes in this Map.
*/
public int size() {
return map.size();
}
/**
* Returns true if this Map contains no attributes.
*/
public boolean isEmpty() {
return map.isEmpty();
}
/**
* Returns a Set view of the attribute names (keys) contained in this Map.
*/
public Set<Object> keySet() {
return map.keySet();
}
/**
* Returns a Collection view of the attribute values contained in this Map.
*/
public Collection<Object> values() {
return map.values();
}
/**
* Returns a Collection view of the attribute name-value mappings
* contained in this Map.
*/
public Set<Map.Entry
Other Java examples (source code examples)
Here is a short list of links related to this Java Attributes.java source code file: