# Class TemplateVariable

java.lang.Object
org.eclipse.jface.text.templates.TemplateVariable

public class TemplateVariable extends Object
A `TemplateVariable` represents a set of positions into a `TemplateBuffer` with identical content each. `TemplateVariableResolver`s can be used to resolve a template variable to a symbol available from the `TemplateContext`. A resolved variable may have one or more possible `values` which may be presented to the user as choices. If there is no user interaction the `default value` is chosen as textual representation of the variable.

Clients may instantiate and extend this class.

Since:
  3.0
See Also:
  - `TemplateVariableResolver`
  - `TemplateBuffer`

## Constructor Summary

Constructors
Constructor
Description
`TemplateVariable(String type, String[] values, int[] offsets)`
Creates a template variable with multiple possible values.
`TemplateVariable(String type, String defaultValue, int[] offsets)`
Creates a template variable.
`TemplateVariable(String type, String name, String[] values, int[] offsets)`
Creates a template variable with multiple possible values.
`TemplateVariable(String type, String name, String[] values, int[] offsets, int length)`
Creates a template variable with multiple possible values.
`TemplateVariable(String type, String name, String defaultValue, int[] offsets)`
Creates a template variable.
`TemplateVariable(TemplateVariableType type, String name, String defaultValue, int[] offsets)`
Creates a template variable.

## Method Summary

Modifier and Type
Method
Description
`String`
`getDefaultValue()`
Returns the default value of the variable.
`final int`
`getInitialLength()`
Returns the initial length of the variable.
`int`
`getLength()`
Returns the length of the variable's default value.
`String`
`getName()`
Returns the name of the variable.
`int[]`
`getOffsets()`
Returns the offsets of the variable.
`String`
`getType()`
Returns the type name of the variable.
`String[]`
`getValues()`
Returns the possible values for this variable.
`TemplateVariableType`
`getVariableType()`
Returns the type of the variable.
`boolean`
`isResolved()`
Returns `true` if the variable has been resolved, `false` otherwise.
`boolean`
`isUnambiguous()`
Returns `true` if the variable is unambiguously resolved, `false` otherwise.
`void`
`setOffsets(int[] offsets)`
Sets the offsets of the variable.
`void`
`setResolved(boolean resolved)`
Sets the resolved flag of the variable.
`void`
`setUnambiguous(boolean unambiguous)`
Sets the isUnambiguous flag of the variable.
`final void`
`setValue(String value)`
Resolves the variable to a single value.
`void`
`setValues(String[] values)`
Resolves the variable to several possible values for this variable, with the first being the default value.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### TemplateVariable

public TemplateVariable(String type, String defaultValue, int[] offsets)
Creates a template variable. The type is used as the name of the variable.

Parameters:
  `type` - the type of the variable
  `defaultValue` - the default value of the variable
  `offsets` - the array of offsets of the variable

### TemplateVariable

public TemplateVariable(String type, String name, String defaultValue, int[] offsets)
Creates a template variable.

Parameters:
  `type` - the type of the variable
  `name` - the name of the variable
  `defaultValue` - the default value of the variable
  `offsets` - the array of offsets of the variable

### TemplateVariable

public TemplateVariable(TemplateVariableType type, String name, String defaultValue, int[] offsets)
Creates a template variable.

Parameters:
  `type` - the type of the variable
  `name` - the name of the variable
  `defaultValue` - the default value of the variable
  `offsets` - the array of offsets of the variable
Since:
  3.3

### TemplateVariable

public TemplateVariable(String type, String[] values, int[] offsets)
Creates a template variable with multiple possible values. The type is used as the name of the template.

Parameters:
  `type` - the type of the template variable
  `values` - the values available at this variable, non-empty
  `offsets` - the array of offsets of the variable

### TemplateVariable

public TemplateVariable(String type, String name, String[] values, int[] offsets)
Creates a template variable with multiple possible values.

Parameters:
  `type` - the type of the variable
  `name` - the name of the variable
  `values` - the values available at this variable, non-empty
  `offsets` - the array of offsets of the variable

### TemplateVariable

public TemplateVariable(String type, String name, String[] values, int[] offsets, int length)
Creates a template variable with multiple possible values.

Parameters:
  `type` - the type of the variable
  `name` - the name of the variable
  `values` - the values available at this variable, non-empty
  `offsets` - the array of offsets of the variable
  `length` - the length of the variable in the template at offset positions
Since:
  3.11

## Method Details

### getType

public String getType()
Returns the type name of the variable.

Returns:
  the type name of the variable

### getVariableType

public TemplateVariableType getVariableType()
Returns the type of the variable.

Returns:
  the type of the variable
Since:
  3.3

### getName

public String getName()
Returns the name of the variable.

Returns:
  the name of the variable

### getDefaultValue

public String getDefaultValue()
Returns the default value of the variable. Typically, this is the first of the possible values (see `getValues()`.

Returns:
  the default value of the variable

### getValues

public String[] getValues()
Returns the possible values for this variable. The returned array is owned by this variable and must not be modified. The array is not empty.

Returns:
  the possible values for this variable

### getLength

public int getLength()
Returns the length of the variable's default value.

Returns:
  the length of the variable

### getInitialLength

public final int getInitialLength()
Returns the initial length of the variable. The initial length is the lenght as it occurred in the template pattern and is used when resolving a template to update the pattern with the resolved values of the variable.

Returns:
  the initial length of the variable
Since:
  3.11

### setOffsets

public void setOffsets(int[] offsets)
Sets the offsets of the variable.

Parameters:
  `offsets` - the new offsets of the variable

### getOffsets

public int[] getOffsets()
Returns the offsets of the variable. The returned array is owned by this variable and must not be modified.

Returns:
  the length of the variable

### setValue

public final void setValue(String value)
Resolves the variable to a single value. This is a shortcut for `setValues(new String[] { value })`.

Parameters:
  `value` - the new default value

### setValues

public void setValues(String[] values)
Resolves the variable to several possible values for this variable, with the first being the default value.

Parameters:
  `values` - a non-empty array of values

### setUnambiguous

public void setUnambiguous(boolean unambiguous)
Sets the isUnambiguous flag of the variable.

Parameters:
  `unambiguous` - the new unambiguous state of the variable

### isUnambiguous

public boolean isUnambiguous()
Returns `true` if the variable is unambiguously resolved, `false` otherwise.

Returns:
  `true` if the variable is unambiguously resolved, `false` otherwise

### setResolved

public void setResolved(boolean resolved)
Sets the resolved flag of the variable.

Parameters:
  `resolved` - the new resolved state
Since:
  3.3

### isResolved

public boolean isResolved()
Returns `true` if the variable has been resolved, `false` otherwise.

Returns:
  `true` if the variable has been resolved, `false` otherwise
Since:
  3.3