实现 RoleList API 的Java程序
角色列表表示角色列表(角色对象)。在创建关系以及尝试在关系中或通过setRoles()方法设置多个角色时,它用作参数。它作为 RoleResult 的一部分返回,以提供成功获取角色的角色。
- java.lang.Object
- java.util.AbstractCollection
- java.util.AbstractList
- java.util.ArrayList
- javax.management.relation.RoleList
句法:
public class RoleList extends ArrayList
构建角色列表的方法
- Role List() :构造一个空的角色列表。
- Role List(int initial_capacity) :构造一个具有指定初始容量的空角色列表。
- Role List(List
list) :构造一个包含指定 List 元素的 Role List,按照 List 的迭代器返回它们的顺序。
实现:下面给出了所有可实现的接口:
- 可序列化
- 可克隆
- 可迭代<对象>
- 集合<对象>
- 列表<对象>
- 随机访问
实现 RoleList API 的Java程序
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 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
public List 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 rlist1
= new LinkedList();
rlist1.add(
new ObjectName("domain1_", "key1_", "value1_"));
rlist1.add(
new ObjectName("domain2_", "key2_", "value3_"));
rList.add(0, new Role("1_rolename", rlist1));
List rList2
= new LinkedList();
rList2.add(
new ObjectName("domain3_", "key3_", "value3_"));
rList2.add(
new ObjectName("domain4_", "key4_", "value4_"));
rList.add(1, new Role("2_rolename", rList2));
List list = rList.asList();
int index = 0;
while (index < list.size()) {
System.out.println(list.get(index++) + " ");
}
System.out.println();
}
}
输出
role name: 1_rolename; role value: domain1_:key1_=value1_, domain2_:key2_=value3_
role name: 2_rolename; role value: domain3_:key3_=value3_, domain4_:key4_=value4_