ConstrainedFieldPosition

public class ConstrainedFieldPosition
extends Object

java.lang.Object
   ↳ android.icu.text.ConstrainedFieldPosition


Represents a span of a string containing a given field. This class differs from FieldPosition in the following ways: 1. It has information on the field category. 2. It allows you to set constraints to use when iterating over field positions. 3. It is used for the newer FormattedValue APIs.

Summary

Public constructors

ConstrainedFieldPosition()

Initializes a CategoryFieldPosition.

Public methods

void constrainClass(Class<?> classConstraint)

Sets a constraint on the field class.

void constrainField(Format.Field field)

Sets a constraint on the field.

Format.Field getField()

Gets the field for the current position.

Object getFieldValue()

Gets the value associated with the current field position.

long getInt64IterationContext()

Gets an int64 that FormattedValue implementations may use for storage.

int getLimit()

Gets the EXCLUSIVE end index stored for the current position.

int getStart()

Gets the INCLUSIVE start index for the current position.

boolean matchesField(Format.Field field, Object fieldValue)

Determines whether a given field and value should be included given the constraints.

void reset()

Resets this ConstrainedFieldPosition to its initial state, as if it were newly created: - Removes any constraints that may have been set on the instance.

void setInt64IterationContext(long context)

Sets an int64 that FormattedValue implementations may use for storage.

void setState(Format.Field field, Object value, int start, int limit)

Sets new values for the primary public getters.

String toString()

Returns a string representation of the object.

Inherited methods

Public constructors

ConstrainedFieldPosition

Added in API level 31
public ConstrainedFieldPosition ()

Initializes a CategoryFieldPosition. By default, the CategoryFieldPosition has no iteration constraints.

Public methods

constrainClass

Added in API level 31
public void constrainClass (Class<?> classConstraint)

Sets a constraint on the field class. When this instance of ConstrainedFieldPosition is passed to FormattedValue#nextPosition, positions are skipped unless the field is an instance of the class constraint, including subclasses. Any previously set constraints are cleared. For example, to loop over only the number-related fields:

 ConstrainedFieldPosition cfpos;
 cfpos.constrainClass(NumberFormat.Field.class);
 while (fmtval.nextPosition(cfpos)) {
   // handle the number-related field position
 }
 

Parameters
classConstraint Class: The field class to fix when iterating.

constrainField

Added in API level 31
public void constrainField (Format.Field field)

Sets a constraint on the field. When this instance of ConstrainedFieldPosition is passed to FormattedValue#nextPosition, positions are skipped unless they have the given field. Any previously set constraints are cleared. For example, to loop over all grouping separators:

 ConstrainedFieldPosition cfpos;
 cfpos.constrainField(NumberFormat.Field.GROUPING_SEPARATOR);
 while (fmtval.nextPosition(cfpos)) {
   // handle the grouping separator position
 }
 
Changing the constraint while in the middle of iterating over a FormattedValue does not generally have well-defined behavior.

Parameters
field Format.Field: The field to fix when iterating.

getField

Added in API level 31
public Format.Field getField ()

Gets the field for the current position. The return value is well-defined and non-null only after FormattedValue#nextPosition returns true.

Returns
Format.Field The field saved in the instance. See above for null conditions.

getFieldValue

Added in API level 31
public Object getFieldValue ()

Gets the value associated with the current field position. The field value is often not set. The return value is well-defined only after FormattedValue#nextPosition returns true.

Returns
Object The value for the current position. Might be null.

getInt64IterationContext

Added in API level 31
public long getInt64IterationContext ()

Gets an int64 that FormattedValue implementations may use for storage. The initial value is zero. Users of FormattedValue should not need to call this method.

Returns
long The current iteration context from setInt64IterationContext(long).

getLimit

Added in API level 31
public int getLimit ()

Gets the EXCLUSIVE end index stored for the current position. The return value is well-defined only after FormattedValue#nextPosition returns true.

Returns
int The end index saved in the instance.

getStart

Added in API level 31
public int getStart ()

Gets the INCLUSIVE start index for the current position. The return value is well-defined only after FormattedValue#nextPosition returns true.

Returns
int The start index saved in the instance.

matchesField

Added in API level 31
public boolean matchesField (Format.Field field, 
                Object fieldValue)

Determines whether a given field and value should be included given the constraints. Intended to be used by FormattedValue implementations.

Parameters
field Format.Field: The field to test.

fieldValue Object: The field value to test. Should be null if there is no value.

Returns
boolean Whether the field should be included given the constraints.

reset

Added in API level 31
public void reset ()

Resets this ConstrainedFieldPosition to its initial state, as if it were newly created: - Removes any constraints that may have been set on the instance. - Resets the iteration position.

setInt64IterationContext

Added in API level 31
public void setInt64IterationContext (long context)

Sets an int64 that FormattedValue implementations may use for storage. Intended to be used by FormattedValue implementations.

Parameters
context long: The new iteration context.

setState

Added in API level 31
public void setState (Format.Field field, 
                Object value, 
                int start, 
                int limit)

Sets new values for the primary public getters. Intended to be used by FormattedValue implementations. It is up to the implementation to ensure that the user-requested constraints are satisfied. This method does not check!

Parameters
field Format.Field: The new field.

value Object: The new field value. Should be null if there is no value.

start int: The new inclusive start index.

limit int: The new exclusive end index.

toString

Added in API level 31
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.