# Class MemoryFeatureSet

Object
AbstractResource
AbstractFeatureSet
MemoryFeatureSet

All Implemented Interfaces:
  `DataSet, FeatureSet, Resource`

public class MemoryFeatureSet extends AbstractFeatureSet
Set of feature instances stored in memory. Features are specified at construction time. Metadata can be specified by overriding the `AbstractFeatureSet​.createMetadata()` method.

## When to use

This class is useful for small sets of features, or for testing purposes, or when the features are in memory anyway (for example, a computation result). It should generally not be used for large data sets read from files or databases.

## Mutability

By default, the feature collection given at construction time is assumed stable. If the content of that collection is modified, then the `refresh()` method should be invoked for rebuilding the set of feature types.

## Thread-safety

This class is thread-safe if the collection given at construction time is thread-safe. Synchronizations use the lock returned by `AbstractResource​.getSynchronizationLock()`.

Since:
  1.6

- ## Nested Class Summary

  ### Nested classes/interfaces inherited from interface Resource

  `Resource​.FileSet`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected final Set<DefaultFeatureType>`
  `allTypes`
  The types, including sub-types, of all feature instances found in the collection.
  `protected final DefaultFeatureType`
  `baseType`
  The base type of all feature instances that the collection can contain.
  `protected final Collection<AbstractFeature>`
  `features`
  The features specified at construction time, potentially as a modifiable collection.

  ### Fields inherited from class AbstractResource

  `listeners`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `MemoryFeatureSet(Collection<AbstractFeature> features)`
  Creates a new set of feature instances stored in memory.
  `MemoryFeatureSet(Resource parent, DefaultFeatureType baseType, Collection<AbstractFeature> features)`
  Creates a new set of feature instances stored in memory with specified parent resource and base type.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `equals(Object obj)`
  Tests whether this memory feature set is wrapping the same feature instances as the given object.
  `Stream<AbstractFeature>`
  `features(boolean parallel)`
  Returns a stream of all features contained in this dataset.
  `OptionalLong`
  `getFeatureCount()`
  Returns the number of features in this set.
  `DefaultFeatureType`
  `getType()`
  Returns the type common to all feature instances in this set.
  `int`
  `hashCode()`
  Returns a hash code value for consistency with `equals(Object)`.
  `protected void`
  `prepareQueryOptimization(FeatureQuery query, Optimization optimizer)`
  Configures the optimization of a query with information about the expected types of all feature instances.
  `void`
  `refresh()`
  Notifies this `FeatureSet` that the elements in the collection of features have changed.

  ### Methods inherited from class AbstractFeatureSet

  `createMetadata, getIdentifier`

  ### Methods inherited from class AbstractResource

  `addListener, clearCache, getEnvelope, getMetadata, getSynchronizationLock, removeListener`

  ### Methods inherited from class Object

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

  ### Methods inherited from interface DataSet

  `getEnvelope`

  ### Methods inherited from interface Resource

  `addListener, getFileSet, getMetadata, removeListener`

- ## Field Details

  - ### baseType

    protected final DefaultFeatureType baseType
    The base type of all feature instances that the collection can contain. All elements of `allTypes` must be assignable to this base type.
    See Also:
      - `getType()`

  - ### allTypes

    protected final Set<DefaultFeatureType> allTypes
    The types, including sub-types, of all feature instances found in the collection. This is often a singleton containing only `baseType`, but it may also be a set without `baseType` if all features are instances of various subtypes.

    This set is modifiable and is updated when `refresh()` is invoked. This set should always contains at least one element.

    See Also:
      - `refresh()`
      - `prepareQueryOptimization(FeatureQuery, Optimization)`

  - ### features

    protected final Collection<AbstractFeature> features
    The features specified at construction time, potentially as a modifiable collection. For all feature instances in this collection, the value returned by `AbstractFeature​.getType()` shall be `baseType` or a subtype of `baseType`.
    See Also:
      - `features(boolean)`

- ## Constructor Details

  - ### MemoryFeatureSet

    public MemoryFeatureSet(Collection<AbstractFeature> features)
    Creates a new set of feature instances stored in memory. The base feature type is determined automatically from the given collection of features. The collection shall contain at least one element.
    Parameters:
      `features` - collection of stored features. This collection will not be copied.
    Throws:
      `IllegalArgumentException` - if the given collection is empty or does not contain feature instances having a common parent type.

  - ### MemoryFeatureSet

    public MemoryFeatureSet(Resource parent, DefaultFeatureType baseType, Collection<AbstractFeature> features)
    Creates a new set of feature instances stored in memory with specified parent resource and base type. This constructor verifies that all feature instances are assignable to a base type. That base type can be either specified explicitly or inferred automatically.
    Parameters:
      `parent` - the parent resource, or `null` if none.
      `baseType` - the base type of all features in the given collection, or `null` for automatic.
      `features` - collection of stored features. This collection will not be copied.
    Throws:
      `IllegalArgumentException` - if `baseType` is null and cannot be determined from the feature instances, or if some feature instances are not assignable to `baseType`.

- ## Method Details

  - ### refresh

    public void refresh()
    Notifies this `FeatureSet` that the elements in the collection of features have changed. This method re-verifies that all feature instances are assignable to the `baseType` and rebuilds the set of all feature types.
    Throws:
      `IllegalStateException` - if some features are not instances of `baseType`.

  - ### getType

    public DefaultFeatureType getType()
    Returns the type common to all feature instances in this set. By default, this type is determined at construction time and does not change even if the content of the feature collection is updated.
    Returns:
      a description of properties that are common to all features in this dataset.

  - ### getFeatureCount

    public OptionalLong getFeatureCount()
    Returns the number of features in this set.
    Overrides:
      `getFeatureCount` in class `AbstractFeatureSet`
    Returns:
      the number of features.

  - ### features

    public Stream<AbstractFeature> features(boolean parallel)
    Returns a stream of all features contained in this dataset.
    Parameters:
      `parallel` - `true` for a parallel stream (if supported), or `false` for a sequential stream.
    Returns:
      all features contained in this dataset.

  - ### prepareQueryOptimization

    protected void prepareQueryOptimization(FeatureQuery query, Optimization optimizer) throws DataStoreException
    Configures the optimization of a query with information about the expected types of all feature instances. This method is invoked indirectly when a feature subset is created from a query. The optimization depends on the set of all feature types found in the collection.
    Overrides:
      `prepareQueryOptimization` in class `AbstractFeatureSet`
    Parameters:
      `query` - definition of feature and feature properties filtering applied at reading time.
      `optimizer` - the optimization to configure.
    Throws:
      `DataStoreException` - if an error occurred during the configuration of the optimizer.
    Since:
      1.6

  - ### equals

    public boolean equals(Object obj)
    Tests whether this memory feature set is wrapping the same feature instances as the given object. This method checks also that the listeners are equal.
    Overrides:
      `equals` in class `Object`
    Parameters:
      `obj` - the object to compare.
    Returns:
      whether the two objects are memory resources wrapping the same features.

  - ### hashCode

    public int hashCode()
    Returns a hash code value for consistency with `equals(Object)`.
    Overrides:
      `hashCode` in class `Object`
    Returns:
      a hash code value.