# Class AshwoodEntitySorter

java.lang.Object
org.apache.cayenne.ashwood.AshwoodEntitySorter

All Implemented Interfaces:
  `EntitySorter`

Direct Known Subclasses:
  `WeightedAshwoodEntitySorter`

public class AshwoodEntitySorter extends Object implements EntitySorter
Implements dependency sorting algorithms for ObjEntities, DbEntities and Persistent objects. Presently it works for acyclic database schemas with possible multi-reflexive tables.

Since:
  3.1

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected Map<DbEntity, org.apache.cayenne.ashwood.AshwoodEntitySorter.ComponentRecord>`
  `components`
  `protected Comparator<DbEntity>`
  `dbEntityComparator`
  `protected final EntityResolver`
  `entityResolver`
  `protected Comparator<ObjEntity>`
  `objEntityComparator`
  `protected Map<DbEntity, List<DbRelationship>>`
  `reflexiveDbEntities`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `AshwoodEntitySorter(EntityResolver entityResolver)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected <E> Digraph<E,Boolean>`
  `buildDigraph(ObjEntity objEntity, List<E> objects, Function<E, Persistent> converter)`
  `protected Persistent`
  `findReflexiveMaster(Persistent object, ObjRelationship toOneRel, String targetEntityName)`
  `Comparator<DbEntity>`
  `getDbEntityComparator()`
  `protected Comparator<DbEntity>`
  `getDbEntityComparator(boolean dependantFirst)`
  `Comparator<ObjEntity>`
  `getObjEntityComparator()`
  `protected Comparator<ObjEntity>`
  `getObjEntityComparator(boolean dependantFirst)`
  `protected String[]`
  `getReflexiveRelationshipsNames(ObjEntity objEntity)`
  `boolean`
  `isReflexive(DbEntity metadata)`
  `void`
  `reindex()`
  Rebuilds the internal index off the current state of the sorter's EntityResolver.
  `void`
  `sortDbEntities(List<DbEntity> dbEntities, boolean deleteOrder)`
  Sorts a list of DbEntities.
  `void`
  `sortObjectsForEntity(ObjEntity objEntity, List<?> objects, boolean deleteOrder)`
  Sorts a list of objects belonging to the ObjEntity.
  `protected <E> void`
  `sortObjectsForEntity(ObjEntity objEntity, List<E> objects, boolean deleteOrder, Function<E, Persistent> converter)`
  `void`
  `sortObjEntities(List<ObjEntity> objEntities, boolean deleteOrder)`
  Sorts a list of ObjEntities.
  `protected <E> boolean`
  `topologicalSort(List<E> data, Digraph<E,Boolean> graph, boolean reverse)`

  ### Methods inherited from class Object

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

- ## Field Details

  - ### entityResolver

    protected final EntityResolver entityResolver

  - ### components

    protected volatile Map<DbEntity, org.apache.cayenne.ashwood.AshwoodEntitySorter.ComponentRecord> components

  - ### reflexiveDbEntities

    protected volatile Map<DbEntity, List<DbRelationship>> reflexiveDbEntities

  - ### dbEntityComparator

    protected Comparator<DbEntity> dbEntityComparator

  - ### objEntityComparator

    protected Comparator<ObjEntity> objEntityComparator

- ## Constructor Details

  - ### AshwoodEntitySorter

    public AshwoodEntitySorter(EntityResolver entityResolver)

- ## Method Details

  - ### reindex

    public void reindex()
    Description copied from interface: `EntitySorter`
    Rebuilds the internal index off the current state of the sorter's EntityResolver. Called when the set of entities changes.
    Specified by:
      `reindex` in interface `EntitySorter`
    Since:
      5.0

  - ### sortDbEntities

    public void sortDbEntities(List<DbEntity> dbEntities, boolean deleteOrder)
    Description copied from interface: `EntitySorter`
    Sorts a list of DbEntities.
    Specified by:
      `sortDbEntities` in interface `EntitySorter`

  - ### sortObjEntities

    public void sortObjEntities(List<ObjEntity> objEntities, boolean deleteOrder)
    Description copied from interface: `EntitySorter`
    Sorts a list of ObjEntities.
    Specified by:
      `sortObjEntities` in interface `EntitySorter`

  - ### sortObjectsForEntity

    public void sortObjectsForEntity(ObjEntity objEntity, List<?> objects, boolean deleteOrder)
    Description copied from interface: `EntitySorter`
    Sorts a list of objects belonging to the ObjEntity.
    Specified by:
      `sortObjectsForEntity` in interface `EntitySorter`

  - ### sortObjectsForEntity

    protected <E> void sortObjectsForEntity(ObjEntity objEntity, List<E> objects, boolean deleteOrder, Function<E, Persistent> converter)

  - ### buildDigraph

    protected <E> Digraph<E,Boolean> buildDigraph(ObjEntity objEntity, List<E> objects, Function<E, Persistent> converter)

  - ### topologicalSort

    protected <E> boolean topologicalSort(List<E> data, Digraph<E,Boolean> graph, boolean reverse)

  - ### getReflexiveRelationshipsNames

    protected String[] getReflexiveRelationshipsNames(ObjEntity objEntity)

  - ### findReflexiveMaster

    protected Persistent findReflexiveMaster(Persistent object, ObjRelationship toOneRel, String targetEntityName)

  - ### getDbEntityComparator

    public Comparator<DbEntity> getDbEntityComparator()
    Specified by:
      `getDbEntityComparator` in interface `EntitySorter`
    Returns:
      comparator for `DbEntity`

  - ### getObjEntityComparator

    public Comparator<ObjEntity> getObjEntityComparator()
    Specified by:
      `getObjEntityComparator` in interface `EntitySorter`
    Returns:
      comparator for `ObjEntity`

  - ### getDbEntityComparator

    protected Comparator<DbEntity> getDbEntityComparator(boolean dependantFirst)

  - ### getObjEntityComparator

    protected Comparator<ObjEntity> getObjEntityComparator(boolean dependantFirst)

  - ### isReflexive

    public boolean isReflexive(DbEntity metadata)
    Specified by:
      `isReflexive` in interface `EntitySorter`
    Parameters:
      `metadata` - to check
    Returns:
      is entity has reflexive relationships