ImmutableDescriptor (Java SE 15 & JDK 15) (original) (raw)
All Implemented Interfaces:
[Serializable](../../../java.base/java/io/Serializable.html "interface in java.io")
, [Cloneable](../../../java.base/java/lang/Cloneable.html "interface in java.lang")
, [Descriptor](Descriptor.html "interface in javax.management")
public class ImmutableDescriptor extends Object implements Descriptor
An immutable descriptor.
Since:
1.6
See Also:
Field Summary
Constructor Summary
Constructors
Constructor | Description |
---|---|
ImmutableDescriptor(String... fields) | Construct a descriptor containing the given fields. |
ImmutableDescriptor(String[] fieldNames,Object[] fieldValues) | Construct a descriptor containing the given fields and values. |
ImmutableDescriptor(Map<String,?> fields) | Construct a descriptor where the names and values of the fields are the keys and values of the given Map. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
Descriptor | clone() | Returns a descriptor which is equal to this descriptor. |
boolean | equals(Object o) | Compares this descriptor to the given object. |
int | hashCode() | Returns the hash code value for this descriptor. |
boolean | isValid() | Returns true if all of the fields have legal values given their names. |
void | removeField(String fieldName) | Removes a field from the descriptor. |
void | setField(String fieldName,Object fieldValue) | This operation is unsupported since this class is immutable. |
void | setFields(String[] fieldNames,Object[] fieldValues) | This operation is unsupported since this class is immutable. |
static ImmutableDescriptor | union(Descriptor... descriptors) | Return an ImmutableDescriptor whose contents are the union of the given descriptors. |
Field Details
EMPTY_DESCRIPTOR
An empty descriptor.
Constructor Details
ImmutableDescriptor
public ImmutableDescriptor(String[] fieldNames,Object[] fieldValues)
Construct a descriptor containing the given fields and values.
Parameters:
fieldNames
- the field names
fieldValues
- the field values
Throws:
[IllegalArgumentException](../../../java.base/java/lang/IllegalArgumentException.html "class in java.lang")
- if either array is null, or if the arrays have different sizes, or if a field name is null or empty, or if the same field name appears more than once.ImmutableDescriptor
public ImmutableDescriptor(String... fields)
Construct a descriptor containing the given fields. Each String must be of the formfieldName=fieldValue
. The field name ends at the first=
character; for example if the String isa=b=c
then the field name isa
and its value isb=c
.
Parameters:
fields
- the field names
Throws:
[IllegalArgumentException](../../../java.base/java/lang/IllegalArgumentException.html "class in java.lang")
- if the parameter is null, or if a field name is empty, or if the same field name appears more than once, or if one of the strings does not contain an=
character.ImmutableDescriptor
public ImmutableDescriptor(Map<String,?> fields)
Construct a descriptor where the names and values of the fields are the keys and values of the given Map.
Parameters:
fields
- the field names and values
Throws:
[IllegalArgumentException](../../../java.base/java/lang/IllegalArgumentException.html "class in java.lang")
- if the parameter is null, or if a field name is null or empty, or if the same field name appears more than once (which can happen because field names are not case sensitive).Method Details
union
Return an
ImmutableDescriptor
whose contents are the union of the given descriptors. Every field name that appears in any of the descriptors will appear in the result with the value that it has when the method is called. Subsequent changes to any of the descriptors do not affect the ImmutableDescriptor returned here.
In the simplest case, there is only one descriptor and the returnedImmutableDescriptor
is a copy of its fields at the time this method is called:
Descriptor d = something();
ImmutableDescriptor copy = ImmutableDescriptor.union(d);Parameters:
descriptors
- the descriptors to be combined. Any of the descriptors can be null, in which case it is skipped.
Returns:
anImmutableDescriptor
that is the union of the given descriptors. The returned object may be identical to one of the input descriptors if it is an ImmutableDescriptor that contains all of the required fields.
Throws:
[IllegalArgumentException](../../../java.base/java/lang/IllegalArgumentException.html "class in java.lang")
- if two Descriptors contain the same field name with different associated values. Primitive array values are considered the same if they are of the same type with the same elements. Object array values are considered the same ifArrays.deepEquals(Object[],Object[]) returns true.equals
public boolean equals(Object o)
Compares this descriptor to the given object. The objects are equal if the given object is also a Descriptor, and if the two Descriptors have the same field names (possibly differing in case) and the same associated values. The respective values for a field in the two Descriptors are equal if the following conditions hold:
* If one value is null then the other must be too.
* If one value is a primitive array then the other must be a primitive array of the same type with the same elements.
* If one value is an object array then the other must be too andArrays.deepEquals(Object[],Object[]) must return true.
* Otherwise Object.equals(Object) must return true.
Specified by:
[equals](Descriptor.html#equals%28java.lang.Object%29)
in interface[Descriptor](Descriptor.html "interface in javax.management")
Overrides:
[equals](../../../java.base/java/lang/Object.html#equals%28java.lang.Object%29)
in class[Object](../../../java.base/java/lang/Object.html "class in java.lang")
Parameters:
o
- the object to compare with.
Returns:
true
if the objects are the same;false
otherwise.
See Also:
Object.hashCode(), HashMaphashCode
public int hashCode()
Returns the hash code value for this descriptor. The hash code is computed as the sum of the hash codes for each field in the descriptor. The hash code of a field with namen
and valuev
isn.toLowerCase().hashCode() ^ h
. Hereh
is the hash code ofv
, computed as follows:
* Ifv
is null thenh
is 0.
* Ifv
is a primitive array thenh
is computed using the appropriate overloading ofjava.util.Arrays.hashCode
.
* Ifv
is an object array thenh
is computed usingArrays.deepHashCode(Object[]).
* Otherwiseh
isv.hashCode()
.
Specified by:
[hashCode](Descriptor.html#hashCode%28%29)
in interface[Descriptor](Descriptor.html "interface in javax.management")
Overrides:
[hashCode](../../../java.base/java/lang/Object.html#hashCode%28%29)
in class[Object](../../../java.base/java/lang/Object.html "class in java.lang")
Returns:
A hash code value for this object.
See Also:
Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object)isValid
public boolean isValid()
Returns true if all of the fields have legal values given their names. This method always returns true, but a subclass can override it to return false when appropriate.
Specified by:
[isValid](Descriptor.html#isValid%28%29)
in interface[Descriptor](Descriptor.html "interface in javax.management")
Returns:
true if the values are legal.
Throws:
[RuntimeOperationsException](RuntimeOperationsException.html "class in javax.management")
- if the validity checking fails. The method returns false if the descriptor is not valid, but throws this exception if the attempt to determine validity fails.clone
Returns a descriptor which is equal to this descriptor. Changes to the returned descriptor will have no effect on this descriptor, and vice versa.
This method returns the object on which it is called. A subclass can override it to return another object provided the contract is respected.
Specified by:
[clone](Descriptor.html#clone%28%29)
in interface[Descriptor](Descriptor.html "interface in javax.management")
Overrides:
[clone](../../../java.base/java/lang/Object.html#clone%28%29)
in class[Object](../../../java.base/java/lang/Object.html "class in java.lang")
Returns:
a clone of this instance.
Throws:
[RuntimeOperationsException](RuntimeOperationsException.html "class in javax.management")
- for illegal value for field Names or field Values. If the descriptor construction fails for any reason, this exception will be thrown.
See Also:
CloneablesetFields
This operation is unsupported since this class is immutable. If this call would change a mutable descriptor with the same contents, then a RuntimeOperationsException wrapping anUnsupportedOperationException is thrown. Otherwise, the behavior is the same as it would be for a mutable descriptor: either an exception is thrown because of illegal parameters, or there is no effect.
Specified by:
[setFields](Descriptor.html#setFields%28java.lang.String%5B%5D,java.lang.Object%5B%5D%29)
in interface[Descriptor](Descriptor.html "interface in javax.management")
Parameters:
fieldNames
- String array of field names. The array and array elements cannot be null.
fieldValues
- Object array of the corresponding field values. The array cannot be null. Elements of the array can be null.
Throws:
[RuntimeOperationsException](RuntimeOperationsException.html "class in javax.management")
- if the change fails for any reason. Wrapped exception is IllegalArgumentException iffieldNames
orfieldValues
is null, or if the arrays are of different lengths, or if there is an illegal value in one of them. Wrapped exception is UnsupportedOperationException if the descriptor is immutable, and the call would change its contents.
See Also:
Descriptor.getFields()setField
This operation is unsupported since this class is immutable. If this call would change a mutable descriptor with the same contents, then a RuntimeOperationsException wrapping anUnsupportedOperationException is thrown. Otherwise, the behavior is the same as it would be for a mutable descriptor: either an exception is thrown because of illegal parameters, or there is no effect.
Specified by:
[setField](Descriptor.html#setField%28java.lang.String,java.lang.Object%29)
in interface[Descriptor](Descriptor.html "interface in javax.management")
Parameters:
fieldName
- The field name to be set. Cannot be null or empty.
fieldValue
- The field value to be set for the field name. Can be null if that is a valid value for the field.
Throws:
[RuntimeOperationsException](RuntimeOperationsException.html "class in javax.management")
- if the field name or field value is illegal (wrapped exception is IllegalArgumentException); or if the descriptor is immutable (wrapped exception isUnsupportedOperationException).removeField
public final void removeField(String fieldName)
Removes a field from the descriptor.
Specified by:
[removeField](Descriptor.html#removeField%28java.lang.String%29)
in interface[Descriptor](Descriptor.html "interface in javax.management")
Parameters:
fieldName
- String name of the field to be removed. If the field name is illegal or the field is not found, no exception is thrown.
Throws:
[RuntimeOperationsException](RuntimeOperationsException.html "class in javax.management")
- if a field of the given name exists and the descriptor is immutable. The wrapped exception will be an UnsupportedOperationException.