Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#pubctors) \| [Methods](https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#inhmethods)

# ConstrainedFieldPosition

*** ** * ** ***


`
public


class
ConstrainedFieldPosition
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.icu.text.ConstrainedFieldPosition |

<br />

*** ** * ** ***

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 ||
|---|---|
| ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#ConstrainedFieldPosition()() ` Initializes a CategoryFieldPosition. |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#constrainClass(java.lang.Class<?>)(https://developer.android.com/reference/java/lang/Class<?> classConstraint) ` Sets a constraint on the field class. |
| ` void` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#constrainField(java.text.Format.Field)(https://developer.android.com/reference/java/text/Format.Field field) ` Sets a constraint on the field. |
| ` https://developer.android.com/reference/java/text/Format.Field` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#getField()() ` Gets the field for the current position. |
| ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#getFieldValue()() ` Gets the value associated with the current field position. |
| ` long` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#getInt64IterationContext()() ` Gets an int64 that FormattedValue implementations may use for storage. |
| ` int` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#getLimit()() ` Gets the EXCLUSIVE end index stored for the current position. |
| ` int` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#getStart()() ` Gets the INCLUSIVE start index for the current position. |
| ` boolean` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#matchesField(java.text.Format.Field,%20java.lang.Object)(https://developer.android.com/reference/java/text/Format.Field field, https://developer.android.com/reference/java/lang/Object fieldValue) ` Determines whether a given field and value should be included given the constraints. |
| ` void` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#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` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#setInt64IterationContext(long)(long context) ` Sets an int64 that FormattedValue implementations may use for storage. |
| ` void` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#setState(java.text.Format.Field,%20java.lang.Object,%20int,%20int)(https://developer.android.com/reference/java/text/Format.Field field, https://developer.android.com/reference/java/lang/Object value, int start, int limit) ` Sets new values for the primary public getters. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#toString()() ` Returns a string representation of the object. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Public constructors

### ConstrainedFieldPosition

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ConstrainedFieldPosition ()
```

Initializes a CategoryFieldPosition.

By default, the CategoryFieldPosition has no iteration constraints.

<br />

## Public methods

### constrainClass

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void constrainClass (Class<?> classConstraint)
```

Sets a constraint on the field class.

When this instance of ConstrainedFieldPosition is passed to `https://developer.android.com/reference/android/icu/text/FormattedValue#nextPosition(android.icu.text.ConstrainedFieldPosition)`, 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
}
```

<br />

<br />

| Parameters ||
|---|---|
| `classConstraint` | `Class`: The field class to fix when iterating. <br /> |

### constrainField

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void constrainField (Format.Field field)
```

Sets a constraint on the field.

When this instance of ConstrainedFieldPosition is passed to `https://developer.android.com/reference/android/icu/text/FormattedValue#nextPosition(android.icu.text.ConstrainedFieldPosition)`, 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.

<br />

<br />

| Parameters ||
|---|---|
| `field` | `Format.Field`: The field to fix when iterating. <br /> |

### getField

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/text/Format.Field` | The field saved in the instance. See above for null conditions. <br /> |

### getFieldValue

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/Object` | The value for the current position. Might be null. <br /> |

### getInt64IterationContext

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Returns ||
|---|---|
| `long` | The current iteration context from `https://developer.android.com/reference/android/icu/text/ConstrainedFieldPosition#setInt64IterationContext(long)`. <br /> |

### getLimit

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Returns ||
|---|---|
| `int` | The end index saved in the instance. <br /> |

### getStart

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getStart ()
```

Gets the INCLUSIVE start index for the current position.

The return value is well-defined only after FormattedValue#nextPosition returns true.

<br />

| Returns ||
|---|---|
| `int` | The start index saved in the instance. <br /> |

### matchesField

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `field` | `Format.Field`: The field to test. <br /> |
| `fieldValue` | `Object`: The field value to test. Should be null if there is no value. <br /> |

| Returns ||
|---|---|
| `boolean` | Whether the field should be included given the constraints. <br /> |

### reset

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

### setInt64IterationContext

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setInt64IterationContext (long context)
```

Sets an int64 that FormattedValue implementations may use for storage.

Intended to be used by FormattedValue implementations.

<br />

| Parameters ||
|---|---|
| `context` | `long`: The new iteration context. <br /> |

### setState

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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!

<br />

| Parameters ||
|---|---|
| `field` | `Format.Field`: The new field. <br /> |
| `value` | `Object`: The new field value. Should be null if there is no value. <br /> |
| `start` | `int`: The new inclusive start index. <br /> |
| `limit` | `int`: The new exclusive end index. <br /> |

### toString

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

Returns a string representation of the object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of the object. <br /> |