Skip to content

Creating an Entity type

Elliot Ford edited this page Apr 8, 2015 · 11 revisions

What is an Entity type?

This is a subclass of Entity (outlined below). An Entity type can be used to 'cast' an entity to a different type - allowing entities to be used in an OO fashion.

What makes an Entity type?

An Entity type is a subclass of the interface Entity (see EntityProxies API docs).

An entity subclass can have the following method definitions:

  1. Setting an Attribute of type (will remove if argument passed is null).
  2. Getting an Attribute of type.
  3. Getting an Entity as type.
  4. Creating a new Entity of type.
  5. Getting a Set of all of the child entities of or as type.
  6. Getting a Stream of all of the child entities of or as type.

For an Entity proxy to be created the type be validated:

  1. All attributes types must not be primitives (can be null).
  2. Can only have super types that are (or are subclasses of) Entity.
  3. Must only contain default or annotated methods: * SetAttribute * GetAttribute * GetEntity * NewEntity * GetEntities * StreamEntities

Example Entity types

The most basic types can be used to mark or group entities for processing - so require no methods at all.

public interface Animal extends Entity{}
public interface FlyingAnimal extends Animal{}

Entity types can be bean-like with getters and setters.

public interface Friend extends Entity {

  @GetAttribute("name")
  String getName();

  @SetAttribute("name")
  void setName(String name);
}

Entities can manage other entities (which may be typed).

public interface Friend extends Entity {

  @StreamEnitites
  Stream<Friend> streamFriends();

  @GetEntities
  Set<Friend> getFriends();

  @NewEntity
  Friend newFriend();
}

Default methods allow entity types to be used similarly to concrete classes.

public interface WingedBeast extends Entity {

  default void takeFlight() {
    /* Set flying */
  }
}

Marking an Entity as type

An entity can be marked as multiple types (for easy filtering).

Entity e; // Previously created entity
e.markAsType(FlyingAnimal.class);

Entity type inheritance still applies:

assert e.isMarkedAsType(Animal.class);

Marked entities can be used to filter entities of that type:

jalse.streamEntitiesOfType(Animal.class).forEach(/* feed */);

'casting' an Entity as type.

Entities can be 'cast' to a particular type and used as such without error. This is because entity type methods are translated into AttributeContainer and EntityContainer methods.

Entity e; // Previously created entity
FlyingAnimal fa = Entities.asType(e, FlyingAnimal.class);

or

FlyingAnimal fa = e.asType(FlyingAnimal.class(;

Clone this wiki locally