Java Program to Implement Attribute API

This Java program is to Implement Attributes API.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.

Here is the source code of the Java program to Implement Attributes API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5. import java.util.Set;
  6. import java.util.jar.Attributes;
  7.  
  8. public class AttributesImpl
  9. { 
  10.     private Attributes attributes;
  11.  
  12.     /** Constructs a new, empty Attributes object with default size. **/
  13.     public AttributesImpl()
  14.     {
  15.         attributes = new Attributes();
  16.     }
  17.  
  18.     /**
  19.      * Constructs a new Attributes object with the same attribute name-value
  20.      * mappings as in the specified Attributes.
  21.      **/
  22.     public AttributesImpl(Attributes attr)
  23.     {
  24.         attributes = new Attributes(attr);
  25.     }
  26.  
  27.     /**
  28.      * Constructs a new, empty Attributes object with the specified initial
  29.      * size.
  30.      **/
  31.     public AttributesImpl(int size)
  32.     {
  33.         attributes = new Attributes(size);
  34.     }
  35.  
  36.     /** Removes all attributes from this Map. **/
  37.     public void clear()
  38.     {
  39.         attributes.clear();
  40.     }
  41.  
  42.     /** Returns a copy of the Attributes **/
  43.     public Object clone()
  44.     {
  45.         return attributes.clone();
  46.     }
  47.  
  48.     /** Returns true if this Map contains the specified attribute name (key). **/
  49.     public boolean containsKey(Object key)
  50.     {
  51.         return attributes.containsKey(key);
  52.     }
  53.  
  54.     /**
  55.      * Returns true if this Map maps one or more attribute names (keys) to the
  56.      * specified value.
  57.      **/
  58.     public boolean containsValue(Object value)
  59.     {
  60.         return attributes.containsValue(value);
  61.     }
  62.  
  63.     /**
  64.      * Returns a Collection view of the attribute name-value mappings contained
  65.      * in this Map.
  66.      **/
  67.     public Set<Map.Entry<Object, Object>> entrySet()
  68.     {
  69.         return attributes.entrySet();
  70.     }
  71.  
  72.     /**
  73.      * Returns the value of the specified attribute name, or null if the
  74.      * attribute name was not found.
  75.      **/
  76.     public Object get(Object key)
  77.     {
  78.         return attributes.get(key);
  79.     }
  80.  
  81.     /**
  82.      * Returns the value of the specified Attributes.Name, or null if the
  83.      * attribute was not found.
  84.      **/
  85.     public String getValue(Attributes.Name name)
  86.     {
  87.         return attributes.getValue(name);
  88.     }
  89.  
  90.     /**
  91.      * Returns the value of the specified attribute name, specified as a string,
  92.      * or null if the attribute was not found.
  93.      **/
  94.     public String getValue(String name)
  95.     {
  96.         return attributes.getValue(name);
  97.     }
  98.  
  99.     /** Returns true if this Map contains no attributes. **/
  100.     public boolean isEmpty()
  101.     {
  102.         return attributes.isEmpty();
  103.     }
  104.  
  105.     /** Returns a Set view of the attribute names (keys) contained in this Map. **/
  106.     public Set<Object> keySet()
  107.     {
  108.         return attributes.keySet();
  109.     }
  110.  
  111.     /**
  112.      * Associates the specified value with the specified attribute name (key) in
  113.      * this Map.
  114.      **/
  115.     public Object put(Object key, Object value)
  116.     {
  117.         return attributes.put(key, value);
  118.     }
  119.  
  120.     /**
  121.      * Copies all of the attribute name-value mappings from the specified
  122.      * Attributes to this Map.
  123.      **/
  124.     public void putAll(Map<?, ?> m)
  125.     {
  126.         attributes.putAll(m);
  127.     }
  128.  
  129.     /**
  130.      * Associates the specified value with the specified attribute name,
  131.      * specified as a String.
  132.      **/
  133.     public String putValue(String name, String value)
  134.     {
  135.         return attributes.putValue(name, value);
  136.     }
  137.  
  138.     /** Removes the attribute with the specified name (key) from this Map **/
  139.     public Object remove(Object key)
  140.     {
  141.         return attributes.remove(key);
  142.     }
  143.  
  144.     /** Returns the number of attributes in this Map. **/
  145.     public int size()
  146.     {
  147.         return attributes.size();
  148.     }
  149.  
  150.     /** Returns a Collection view of the attribute values contained in this Map. **/
  151.     public Collection<Object> values()
  152.     {
  153.         return attributes.values();
  154.     }
  155.  
  156.     public static void main(String... arg)
  157.     {
  158.         Attributes.Name CLASS_PATH = new Attributes.Name("CLASS_PATH");
  159.         Attributes.Name CONTENT_TYPE = new Attributes.Name("CONTENT_TYPE");
  160.         Attributes.Name MANIFEST_VERSION = new Attributes.Name("MANIFEST_VERSION");
  161.  
  162.         AttributesImpl attribute = new AttributesImpl();
  163.         attribute.put(CLASS_PATH, "root/sub_dir/class_path");
  164.         attribute.put(CONTENT_TYPE, "UTF-8");
  165.         attribute.put(MANIFEST_VERSION, "2");
  166.  
  167.         attribute.putValue("MAIN_CLASS", "TESTMAIN.java");
  168.         System.out.println("the key set of the Attributes is ");
  169.         Set<Object> keySet = attribute.keySet();
  170.         Iterator<Object> itr = keySet.iterator();
  171.         while (itr.hasNext())
  172.         {
  173.             System.out.print(itr.next() + "\t");
  174.         }
  175.         System.out.println();
  176.  
  177.         System.out.println("the values of the Attributes is ");
  178.         Collection<Object> collectionValues = attribute.values();
  179.         itr = collectionValues.iterator();
  180.         while (itr.hasNext())
  181.         {
  182.             System.out.print(itr.next() + "\t");
  183.         }
  184.         System.out.println();
  185.         System.out.println("the entry set of the Attributes is ");
  186.         Iterator<Entry<Object, Object>> eitr;
  187.         Set<Entry<Object, Object>> entrySet = attribute.entrySet();
  188.         eitr = entrySet.iterator();
  189.         while (eitr.hasNext())
  190.         {
  191.             System.out.println(eitr.next() + "\t");
  192.         }
  193.         System.out.println("the Attributes contains Key CLASS_PATH:" + attribute.containsKey(CLASS_PATH));
  194.         System.out.println("the Attributes contains Value TESTMAIN.java :"
  195.             + attribute.containsValue("TESTMAIN.java"));
  196.         System.out.println("the size of the Attributes is " + attribute.size());
  197.         attribute.clear();
  198.         if (attribute.isEmpty())
  199.             System.out.println("the Attributes is empty");
  200.         else
  201.             System.out.println("the Attributes is not empty");
  202.     }
  203. }

$ javac  AttributesImpl.java
$ java AttributesImpl
the key set of the Attributes is 
MAIN_CLASS	MANIFEST_VERSION	CONTENT_TYPE	CLASS_PATH	
the values of the Attributes is 
TESTMAIN.java	2	UTF-8	root/sub_dir/class_path	
the entry set of the Attributes is 
MAIN_CLASS=TESTMAIN.java	
MANIFEST_VERSION=2	
CONTENT_TYPE=UTF-8	
CLASS_PATH=root/sub_dir/class_path	
the Attributes contains Key CLASS_PATH:true
the Attributes contains Value TESTMAIN.java :true
the size of the Attributes is 4
the Attributes is empty

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.