Java Program to Implement AttributeList API

«
»
This Java program Implements AttributeList API.Represents a list of values for attributes of an MBean.

Here is the source code of the Java Program to Implement AttributeList 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.List;
  3. import javax.management.Attribute;
  4. import javax.management.AttributeList;
  5.  
  6. public class AttributeListImpl
  7. {
  8.     private AttributeList attributeList;
  9.  
  10.     /** Constructs an empty AttributeList. **/
  11.     public AttributeListImpl()
  12.     {
  13.         attributeList = new AttributeList();
  14.     }
  15.  
  16.     /**
  17.      * Constructs an AttributeList containing the elements of the AttributeList
  18.      * specified, in the order in which they are returned by the AttributeList's
  19.      * iterator.
  20.     **/
  21.     public AttributeListImpl(AttributeList list)
  22.     {
  23.         attributeList = new AttributeList();
  24.     }
  25.  
  26.     /** Constructs an empty AttributeList with the initial capacity specified. **/
  27.     public AttributeListImpl(int initialCapacity)
  28.     {
  29.         attributeList = new AttributeList(initialCapacity);
  30.     }
  31.  
  32.     /**
  33.      * Constructs an AttributeList containing the elements of the List
  34.      * specified, in the order in which they are returned by the List's
  35.      * iterator.
  36.      **/
  37.     public AttributeListImpl(List<Attribute> list)
  38.     {
  39.         attributeList = new AttributeList();
  40.     }
  41.  
  42.     /** Adds the Attribute specified as the last element of the list. **/
  43.     public void add(Attribute object)
  44.     {
  45.         attributeList.add(object);
  46.     }
  47.  
  48.     /** Inserts the attribute specified as an element at the position specified. **/
  49.     public void add(int index, Attribute object)
  50.     {
  51.         attributeList.add(index, object);
  52.     }
  53.  
  54.     /** Inserts the specified element at the specified position in this list. **/
  55.     public void add(int index, Object element)
  56.     {
  57.         attributeList.add(index, element);
  58.     }
  59.  
  60.     /** Appends the specified element to the end of this list. **/
  61.     public boolean add(Object element)
  62.     {
  63.         return attributeList.add(element);
  64.     }
  65.  
  66.     /**
  67.      * Appends all the elements in the AttributeList specified to the end of the
  68.      * list, in the order in which they are returned by the Iterator of the
  69.      * AttributeList specified.
  70.     **/
  71.     public boolean addAll(AttributeList list)
  72.     {
  73.         return attributeList.addAll(list);
  74.     }
  75.  
  76.     /**
  77.      * Appends all of the elements in the specified collection to the end of
  78.      * this list, in the order that they are returned by the specified
  79.      * collection's Iterator.
  80.      **/
  81.     public boolean addAll(Collection<?> c)
  82.     {
  83.         return attributeList.addAll(c);
  84.     }
  85.  
  86.     /**
  87.      * Inserts all of the elements in the AttributeList specified into this
  88.      * list, starting at the specified position, in the order in which they are
  89.      * returned by the Iterator of the AttributeList specified.
  90.      **/
  91.     public boolean addAll(int index, AttributeList list)
  92.     {
  93.         return attributeList.addAll(index, list);
  94.     }
  95.  
  96.     /**
  97.      * Inserts all of the elements in the specified collection into this list,
  98.      * starting at the specified position.
  99.     **/
  100.     public boolean addAll(int index, Collection<?> c)
  101.     {
  102.         return attributeList.addAll(index, c);
  103.     }
  104.  
  105.     /** Return a view of this list as a List<Attribute>. **/
  106.     public List<Attribute> asList()
  107.     {
  108.         return attributeList.asList();
  109.     }
  110.  
  111.     /** Sets the element at the position specified to be the attribute specified. **/
  112.     public void set(int index, Attribute element)
  113.     {
  114.         attributeList.set(index, element);
  115.     }
  116.  
  117.     /**
  118.      * Replaces the element at the specified position in this list with the
  119.      * specified element.
  120.     **/
  121.     public Object set(int index, Object element)
  122.     {
  123.         return attributeList.set(index, element);
  124.     }
  125.  
  126.     public static void main(String... arg)
  127.     {
  128.         AttributeListImpl attributeList = new AttributeListImpl();
  129.         attributeList.add(new Attribute("one", 1));
  130.         attributeList.add(new Attribute("two", 2));
  131.         attributeList.add(new Attribute("three", 3));
  132.         attributeList.add(new Attribute("four", 4));
  133.         attributeList.add(new Attribute("five", 5));
  134.         attributeList.add(new Attribute("six", 7));
  135.         System.out.println("the elements of the attributelist are");
  136.         List<Attribute> list = attributeList.asList();
  137.         int index = 0;
  138.         while (index < list.size())
  139.         {
  140.             System.out.print(list.get(index++) + "\t");
  141.         }
  142.         System.out.println();
  143.         attributeList.set(5, new Attribute("six", 6));
  144.         System.out.println("after setting index 5");
  145.         System.out.println("the elements of the attributelist are");
  146.         list = attributeList.asList();
  147.         index = 0;
  148.         while (index < list.size())
  149.         {
  150.             System.out.print(list.get(index++) + "\t");
  151.         }
  152.     }
  153. }

$ javac AttributeListImpl.java
$ java AttributeListImpl
the elements of the attributelist are
one = 1	two = 2	three = 3	four = 4	five = 5	six = 7	
after setting index 5
the elements of the attributelist are
one = 1	two = 2	three = 3	four = 4	five = 5	six = 6

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

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

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest 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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.