Java Program to Implement RoleList API

«
»
This Java program Implements RoleList API.A RoleList represents a list of roles (Role objects). It is used as parameter when creating a relation, and when trying to set several roles in a relation (via ‘setRoles()’ method). It is returned as part of a RoleResult, to provide roles successfully retrieved.

Here is the source code of the Java Program to Implement RoleList 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.LinkedList;
  3. import java.util.List;
  4. import javax.management.MalformedObjectNameException;
  5. import javax.management.ObjectName;
  6. import javax.management.relation.Role;
  7. import javax.management.relation.RoleList;
  8.  
  9. public class RoleListImpl
  10. {
  11.     private RoleList roleList;
  12.  
  13.     /** Constructs an empty RoleList. **/  
  14.     public RoleListImpl()
  15.     {
  16.         roleList = new RoleList();
  17.     }
  18.  
  19.     /** Constructs an empty RoleList with the initial capacity specified. **/
  20.     public RoleListImpl(int initialCapacity)
  21.     {
  22.         roleList = new RoleList(initialCapacity);
  23.     }
  24.  
  25.     /**
  26.      * Constructs a RoleList containing the elements of the List specified, in
  27.      * the order in which they are returned by the List's iterator.
  28.     **/
  29.     public RoleListImpl(List<Role> list)
  30.     {
  31.         roleList = new RoleList(list);
  32.     }
  33.  
  34.     /** Inserts the specified element at the specified position in this list. **/
  35.     public void add(int index, Object element)
  36.     {
  37.         roleList.add(index, element);
  38.     }
  39.  
  40.     /** Inserts the role specified as an element at the position specified. **/
  41.     public void add(int index, Role role)
  42.     {
  43.         roleList.add(index, role);
  44.     }
  45.  
  46.     /** Appends the specified element to the end of this list. **/
  47.     public boolean add(Object o)
  48.     {
  49.         return roleList.add(o);
  50.     }
  51.  
  52.     /** Adds the Role specified as the last element of the list. **/
  53.     public void add(Role role)
  54.     {
  55.         roleList.add(role);
  56.     }
  57.  
  58.     /**
  59.      * Appends all of the elements in the specified collection to the end of
  60.      * this list, in the order that they are returned by the specified
  61.      * collection's Iterator.
  62.     **/
  63.     public boolean addAll(Collection<?> c)
  64.     {
  65.         return roleList.addAll(c);
  66.     }
  67.  
  68.     /**
  69.      * Inserts all of the elements in the specified collection into this list,
  70.      * starting at the specified position.
  71.     **/
  72.     public boolean addAll(int index, Collection<?> c)
  73.     {
  74.         return roleList.addAll(index, c);
  75.     }
  76.  
  77.     /**
  78.      * Inserts all of the elements in the RoleList specified into this list,
  79.      * starting at the specified position, in the order in which they are
  80.      * returned by the Iterator of the RoleList specified.
  81.     **/
  82.     public boolean addAll(int index, RoleList roleList)
  83.     {
  84.         return this.roleList.addAll(index, roleList);
  85.     }
  86.  
  87.     /**
  88.      * Appends all the elements in the RoleList specified to the end of the
  89.      * list, in the order in which they are returned by the Iterator of the
  90.      * RoleList specified.
  91.     **/
  92.     public boolean addAll(RoleList roleList)
  93.     {
  94.         return roleList.addAll(roleList);
  95.     }
  96.  
  97.     /** vReturn a view of this list as a List<Role>. **/
  98.     public List<Role> asList()
  99.     {
  100.         return roleList.asList();
  101.     }
  102.  
  103.     /**
  104.      * Replaces the element at the specified position in this list with the
  105.      * specified element.
  106.     **/
  107.     public Object set(int index, Object element)
  108.     {
  109.         return roleList.set(index, element);
  110.     }
  111.  
  112.     /** Sets the element at the position specified to be the role specified. **/
  113.     public void set(int index, Role role)
  114.     {
  115.         roleList.set(index, role);
  116.     }
  117.  
  118.     public static void main(String... arg) throws MalformedObjectNameException
  119.     {
  120.         RoleListImpl roleList = new RoleListImpl();
  121.         List<ObjectName> rolelist1 = new LinkedList<ObjectName>();
  122.         rolelist1.add(new ObjectName("domain1","key1","value1"));
  123.         rolelist1.add(new ObjectName("domain2","key2","value2"));
  124.         roleList.add(0, new Role("rolename1", rolelist1));
  125.  
  126.         List<ObjectName> roleList2 = new LinkedList<ObjectName>();
  127.         roleList2.add(new ObjectName("domain3","key3","value3"));
  128.         roleList2.add(new ObjectName("domain4","key4","value4"));
  129.         roleList.add(1, new Role("rolename2", roleList2));
  130.  
  131.         List<Role> list = roleList.asList();
  132.         int index = 0;
  133.         while (index < list.size())
  134.         {
  135.             System.out.println(list.get(index++) + "\t");
  136.         }
  137.         System.out.println();
  138.     }
  139. }

$ javac RoleListImpl.java
$ java RoleListImpl
role name: rolename1; role value: domain1:key1=value1, domain2:key2=value2	
role name: rolename2; role value: domain3:key3=value3, domain4:key4=value4

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
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.