📜  用Java实现 RoleUnresolvedList API

📅  最后修改于: 2022-05-13 01:55:36.805000             🧑  作者: Mango

用Java实现 RoleUnresolvedList API

API 是 Application Programming Interface 的首字母缩写,它是允许两个应用程序相互通信的软件。 RoleUnresolvedList 表示 RoleUnresolved Object 的列表,RoleUnresolved Object 表示由于尝试访问读取或写入角色时遇到的某些问题而未从关系中检索到的角色。因此,RoleUnresolvedList API 是一个与内部文件通信以检索对象含义并表示由于某些问题而未从关系中检索到的角色的程序

先决条件:

(A) Packages:在Java的RoleUnresolvedList API程序中,为了检索程序中使用的对象的含义和用途,使用了各种Package。下图显示了程序中导入的包:

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.relation.RoleUnresolved;
import javax.management.relation.RoleUnresolvedList;

(B) 构造函数的使用

  • RoleUnresolvedList():用于构造空的RoleUnresolvedList。
  • RoleUnresolvedList(int initialCapacity):用于构造一个指定Initial Capacity的空RoleUnresolvedList。
  • RoleUnresolvedListImpl(List list) :用于构造包含指定列表元素的 RoleUnresolvedList。它是按照列表迭代器返回的顺序构造的。

(C) 方法

  • add(int index, Object element) 此方法用于在特定位置插入特定元素。
  • add(int index, RoleUnresolved role) 此方法在指定位置插入指定为元素的 Unresolved 角色。
  • add(Object o) :它在列表的末尾附加指定的元素。
  • add(RoleUnresolved role)它将添加在列表的最后一个元素指定的 RoleUnresolved。
  • addAll(Collection c) 将指定列表中的元素追加到最后。
  • addAll(int index, Collection c) 它将在指定位置插入元素。
  • addAll(int index, RoleUnresolvedList roleList):将指定的 RoleUnresolvedList 中的所有元素插入到此列表中,从指定位置开始,按照指定的 RoleUnresolvedList 的 Iterator 返回的顺序。
Java
// Java Program to Implement RoleUnresolvedList API
  
// Packages are imported whose methods
// will be used withinProgram
// Importing classes drom java.util package
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
// Importing javax.management package
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.relation.RoleUnresolved;
import javax.management.relation.RoleUnresolvedList;
  
// Class
public class GFG
  
{
    private RoleUnresolvedList roleUnresolvedList;
  
    // Constructor 1
    // It Constructs an empty RoleUnresolvedList
    public GFG()
    {
        roleUnresolvedList = new RoleUnresolvedList();
    }
  
    // Constructor 2
    // Constructs an empty RoleUnresolvedList
    // with the initial capacity specified
    public GFG(int initialCapacity)
  
    {
        roleUnresolvedList
            = new RoleUnresolvedList(initialCapacity);
    }
  
    // Constructor 3
    // It Constructs a RoleUnresolvedList containing
    // the elements of the List specified
    public GFG(List list)
  
    {
        roleUnresolvedList = new RoleUnresolvedList(list);
    }
  
    // Method 1
    // Inserts the specified element at the specified
    // position in this list
    public void add(int index, Object element)
  
    {
        roleUnresolvedList.add(index, element);
    }
  
    // Method 2
    // Inserts the unresolved role specified as an element
    // at the position specified.
    public void add(int index, RoleUnresolved role)
    {
        roleUnresolvedList.add(index, role);
    }
  
    // Method 3
    // Appends the specified element to the end of this list
    public boolean add(Object o)
    {
        return roleUnresolvedList.add(o);
    }
  
    // Method 4
    // Adds the RoleUnresolved specified as the last element
    // of the list
    public void add(RoleUnresolved role)
    {
        roleUnresolvedList.add(role);
    }
  
    // Method 5
    // Appends all of the elements in the specified
    // collection to the end of list, in the order that they
    // are returned by the specified collection's Iterator.
    public boolean addAll(Collection c)
    {
        return roleUnresolvedList.addAll(c);
    }
  
    // Method 6
    // Inserts all of the elements in the specified
    // collection into this list, starting at the specified
    // position
    public boolean addAll(int index, Collection c)
    {
        return roleUnresolvedList.addAll(index, c);
    }
  
    // Method 7
    // Inserts all of the elements in the RoleUnresolvedList
    // specified into list, starting at the specified
    // position, in the order in which they are returned by
    // the Iterator of the RoleUnresolvedList specified.
    public boolean addAll(int index,
                          RoleUnresolvedList roleList)
    {
        return this.roleUnresolvedList.addAll(index,
                                              roleList);
    }
  
    // Method 8
    // Appends all the elements in the RoleUnresolvedList
    // specified to the end of the list, in the order in
    // which they are returned by the Iterator o the
    // RoleUnresolvedList specified.
    public boolean addAll(RoleUnresolvedList roleList)
    {
        return roleList.addAll(roleList);
    }
  
    // Method 9
    // Return a view of this list as a List
    public List asList()
    {
        return roleUnresolvedList.asList();
    }
  
    // Method 10
    // Replaces the element at the specified position in
    // this list with the specified element
    public Object set(int index, Object element)
    {
        return roleUnresolvedList.set(index, element);
    }
  
    // Method 11
    // Sets the element at the position
    // specified to be the unresolved role specified.
    public void set(int index, RoleUnresolved role)
    {
        roleUnresolvedList.set(index, role);
    }
  
    // Method 12
    // Main driver method
    public static void main(String[] arg)
        throws MalformedObjectNameException
    {
        // Creating object of GFG class
        GFG roleUnresolvedList = new GFG();
  
        // Creating a List object
        // Declaring List of type ObjectName
        List rolelist1
            = new LinkedList();
  
        // Adding elements to above object
        // Custom inputs
        rolelist1.add(
            new ObjectName("domain1", "key1", "value1"));
        rolelist1.add(
            new ObjectName("domain2", "key2", "value2"));
        roleUnresolvedList.add(
            0,
            new RoleUnresolved("rolename1", rolelist1, 1));
  
        // Creating another List object
        List roleList2
            = new LinkedList();
  
        // Adding elements to above object
        // Custom inputs
        roleList2.add(
            new ObjectName("domain3", "key3", "value3"));
        roleList2.add(
            new ObjectName("domain4", "key4", "value4"));
        roleUnresolvedList.add(
            1,
            new RoleUnresolved("rolename2", roleList2, 2));
  
        // Creating(declaring) object of type RoleUnresolved
        List list
            = roleUnresolvedList.asList();
  
        // Initialising variable count
        int index = 0;
  
        // Condition check using size() method in List
        while (index < list.size())
  
        {
            // Print the elements
            System.out.println(list.get(index++) + "\t");
        }
  
        // As the condition fails
        // jump to new line
        System.out.println();
    }
}


输出
role name: rolename1; value: domain1:key1=value1, domain2:key2=value2; problem type: 1    
role name: rolename2; value: domain3:key3=value3, domain4:key4=value4; problem type: 2