Open In App

Java Program to Implement RoleList API

Last Updated : 11 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

A Role List represents a list of roles (Role objects). It is used as a parameter when creating a relation, and when attempting to set multiple roles in a relationship or via setRoles() method. It is returned as part of a RoleResult, to provide roles successfully obtain the role.

Syntax:

public class RoleList extends ArrayList<Object> ;

Methods for constructing a Role list

  1. Role List(): Constructs an empty Role List.
  2. Role List(int initial_capacity): Constructs an empty Role List with the initial capacity specified.
  3. Role List(List<Role> list): Constructs a Role List containing the elements of the List specified, in the order in which they are returned by the List’s iterator.

Implementation: All Implementable Interfaces are given below:

  1. Serializable
  2. Cloneable
  3. Iterable<Object>
  4. Collection<Object>
  5. List<Object>
  6. RandomAccess

Java Program to Implement RoleList API 

Java




// Java Program to Implement RoleList API
  
// Importing libraries
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.relation.Role;
import javax.management.relation.RoleList;
  
public class GFG {
  
    private RoleList rList;
  
    // Create an empty RoleList
    public GFG() { rList = new RoleList(); }
  
    // Create an empty RoleList
    // with the initial capacity
    public GFG(int inicapacity)
    {
        rList = new RoleList(inicapacity);
    }
  
    // Create a RoleList containing the elements of the List
    // specified in order in which they are returned by the
    // List's iterator
    public GFG(List<Role> list)
    {
        rList = new RoleList(list);
    }
  
    // Inserts element at the specified position in list
    public void add(int index, Object element)
    {
        rList.add(index, element);
    }
  
    // Inserts the role specified at the position
    //  specified as an element
    public void add(int index, Role role)
    {
        rList.add(index, role);
    }
  
    // Appends the specified element to the end of List
    public boolean add(Object o) 
    {
      return rList.add(o); 
    }
  
    // Adds the Role specified as the last element
    public void add(Role role) 
    
      rList.add(role); 
    }
  
    // Appends all elements in the specified collections
    // to the end of list, in the order that
    // they are returned by the specified collection's
    // Iterator
    public boolean addAll(Collection<?> c)
    {
        return rList.addAll(c);
    }
  
    // Inserts all elements in the specified collection into
    // list starting from the specified position.
    public boolean addAll(int index, Collection<?> c)
    {
        return rList.addAll(index, c);
    }
  
    // Inserts all elements in the RoleList specified into
    // list, starting from the specified position,in the
    // order in which they are returned by the Iterator
    public boolean addAll(int index, RoleList roleList)
    {
        return this.rList.addAll(index, rList);
    }
  
    // Append all elements in the RoleList specified to the
    // end, in the order in which they are returned.
    public boolean addAll(RoleList rList)
    {
        return rList.addAll(rList);
    }
  
    // Return a view of list as a List<Role>
    public List<Role> asList() 
    
      return rList.asList(); 
    }
  
    // Replace the element at the specified position in list
    // with the specified element
    public Object set(int index, Object element)
    {
        return rList.set(index, element);
    }
  
    // Set the element at the position specified
    // to be the role specified
    public void set(int index, Role role)
    {
        rList.set(index, role);
    }
  
    // Main driver method
    public static void main(String[] arg)
        throws MalformedObjectNameException
    {
        GFG rList = new GFG();
        List<ObjectName> rlist1
            = new LinkedList<ObjectName>();
        rlist1.add(
            new ObjectName("domain1_", "key1_", "value1_"));
        rlist1.add(
            new ObjectName("domain2_", "key2_", "value3_"));
        rList.add(0, new Role("1_rolename", rlist1));
  
        List<ObjectName> rList2
            = new LinkedList<ObjectName>();
        rList2.add(
            new ObjectName("domain3_", "key3_", "value3_"));
        rList2.add(
            new ObjectName("domain4_", "key4_", "value4_"));
        rList.add(1, new Role("2_rolename", rList2));
  
        List<Role> list = rList.asList();
        int index = 0;
        while (index < list.size()) {
            System.out.println(list.get(index++) + " ");
        }
        System.out.println();
    }
}


Output

role name: 1_rolename; role value: domain1_:key1_=value1_, domain2_:key2_=value3_ 
role name: 2_rolename; role value: domain3_:key3_=value3_, domain4_:key4_=value4_


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads