Se puede utilizar una AttributeList para obtener y establecer varios atributos de MBean en una sola invocación. Es una ArrayList que solo puede contener Atributos. Las listas de atributos deben sincronizarse externamente.
Declaración:
public class AttributeList extends ArrayList { // code }
Clases de implementación de la API AttributeList:
- Serializable
- Clonable
- Iterable
- Recopilación
- Lista
- Acceso aleatorio
Constructor:
- AttributeList(): construye una nueva lista de atributos vacía.
- AttributeList (lista de AttributeList): construye un nuevo atributo a partir de otra lista de atributos.
- AttributeList(int initialCapacity): construye una nueva lista de atributos vacía con la capacidad inicial especificada.
Métodos:
- add(Objeto de atributo): método para agregar un atributo a la lista.
- void add(int index, Attribute object) – Método para insertar un nuevo atributo en la lista en el índice especificado.
- boolean addAll(AttributeList list) – Método para añadir atributos a la lista.
- boolean addAll(int index, AttributeList list) – Método para insertar todos los atributos en la lista pasada en el índice especificado.
- conjunto vacío (índice int, objeto de atributo): método para cambiar un atributo en un índice especificado.
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<Attribute> 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<Attribute> public List<Attribute> 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<Attribute> 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++) + " "); } } }
Producción
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
Publicación traducida automáticamente
Artículo escrito por Gunjanpaul y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA