📜  实现 AttributeList API 的Java程序

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

实现 AttributeList API 的Java程序

AttributeList可用于在单个调用中获取和设置多个 MBean 属性。它是一个只能包含属性的 ArrayList。 AttributeLists 必须在外部同步。

宣言:

public class AttributeList extends ArrayList
{
  // code
}

AttributeList API 的实现类:

  • 可序列化
  • 可克隆
  • 可迭代的
  • 收藏
  • 列表
  • 随机访问

构造函数:

  1. AttributeList() –构造一个新的空属性列表。
  2. AttributeList(AttributeList list) –从另一个属性列表构造一个新属性。
  3. AttributeList(int initialCapacity) –构造一个具有指定初始容量的新空属性列表。

方法:

  1. add(Attribute object) –将属性附加到列表的方法。
  2. void add(int index, Attribute object) –将新属性插入列表中指定索引处的方法。
  3. boolean addAll(AttributeList list) –将属性附加到列表的方法。
  4. boolean addAll(int index, AttributeList list) –在指定索引处插入传递列表中所有属性的方法。
  5. void set(int index, Attribute object) –在指定索引处更改属性的方法。
Java
// Java program to implement AttributeList API
  
import java.util.Collection;
import java.util.List;
import javax.management.Attribute;
import javax.management.AttributeList;
  
public class AttributeListImpl {
    private AttributeList attributeList;
  
    // constructor for creating an empty AttributeList
    public AttributeListImpl()
    {
        attributeList = new AttributeList();
    }
  
    // constructor for creating an AttributeList containing
    // the elements of the AttributeList specified, in the
    // order in which they are returned by the
    // AttributeList's iterator.
  
    public AttributeListImpl(AttributeList list)
    {
        attributeList = new AttributeList();
    }
  
    // constructor for creating an AttributeList with the
    // specified initial capacity
    public AttributeListImpl(int initialCapacity)
    {
        attributeList = new AttributeList(initialCapacity);
    }
  
    // constructor for creating an AttributeList
    // containing the elements of the List
    // specified.
  
    public AttributeListImpl(List list)
    {
        attributeList = new AttributeList();
    }
  
    // this method appends an Attribute to the list
    public void add(Attribute object)
    {
        attributeList.add(object);
    }
  
    // this method inserts an attribute at a specified
    // position
    public void add(int index, Attribute object)
    {
        attributeList.add(index, object);
    }
  
    // this method inserts an specified element at
    // a specified position
    public void add(int index, Object element)
    {
        attributeList.add(index, element);
    }
  
    // this method appends a specified element to the list
    public boolean add(Object element)
    {
        return attributeList.add(element);
    }
  
    // this method appends all the elements of a
    // given list in the AttributeList.
    public boolean addAll(AttributeList list)
    {
        return attributeList.addAll(list);
    }
  
    // this method will appends all of the
    // elements of the given Collection
    // in the AttributeList.
    public boolean addAll(Collection c)
    {
        return attributeList.addAll(c);
    }
  
    // Inserts all of the elements in the AttributeList
    // specified into this list, starting at the specified
    // position, in the order in which they are returned by
    // the Iterator of the AttributeList specified.
    public boolean addAll(int index, AttributeList list)
    {
        return attributeList.addAll(index, list);
    }
  
    // this method will add all of the elements of the
    // given collection at a the specified index
    public boolean addAll(int index, Collection c)
    {
        return attributeList.addAll(index, c);
    }
  
    // this method Return a view of this list as a
    // List
    public List asList()
    {
        return attributeList.asList();
    }
  
    // this method will set the element at a given position
    public void set(int index, Attribute element)
    {
        attributeList.set(index, element);
    }
  
    // this method will replace the element at the specified
    // position in this list with the given element
    public Object set(int index, Object element)
    {
        return attributeList.set(index, element);
    }
  
    public static void main(String[] arg)
    {
        // creating object for AttributeListImpl object
        AttributeListImpl attributeList
            = new AttributeListImpl();
  
        attributeList.add(new Attribute("rose", 1));
        attributeList.add(new Attribute("sun-flower", 2));
        attributeList.add(new Attribute("tulip", 3));
        attributeList.add(new Attribute("orchid", 4));
        attributeList.add(new Attribute("marie-gold", 5));
        attributeList.add(new Attribute("hibiscus", 0));
  
        System.out.println(
            "The elements of the attributelist are");
  
        List flowerlist = attributeList.asList();
  
        int ind = 0;
  
        // iterating through the attribute List and
        // printing elements.
        while (ind < flowerlist.size()) {
            System.out.print(flowerlist.get(ind++) + " ");
        }
  
        System.out.println();
  
        // setting value = 6 at key = "hibiscus"
        attributeList.set(5, new Attribute("hibiscus", 6));
  
        System.out.println();
        System.out.println("after setting index 5");
        System.out.println(
            "the elements of the attributelist are");
  
        // iterating through the attribute List and
        // printing elements.
        flowerlist = attributeList.asList();
  
        ind = 0;
        while (ind < flowerlist.size()) {
            System.out.print(flowerlist.get(ind++) + " ");
        }
    }
}


输出
The elements of the attributelist are
rose = 1 sun-flower = 2 tulip = 3 orchid = 4 marie-gold = 5 hibiscus = 0 

after setting index 5
the elements of the attributelist are
rose = 1 sun-flower = 2 tulip = 3 orchid = 4 marie-gold = 5 hibiscus = 6