Class Provider<T>

java.lang.Object
net.tangly.core.providers.Provider<T>
Type Parameters:
T - type of the instances
Direct Known Subclasses:
ProviderHasOid, ProviderInMemory, ProviderPersistence, ProviderView

public abstract class Provider<T> extends Object
Define the provider abstraction responsible for handling instances of a specific type. The provider declares the regular CRUD operations: Create, Read, Update, and Delete. The provider is the repository and often the factory in the domain-driven design terminology.

You should store domain-driven domain aggregates in a provider. Each aggregate should have a root with an identifier. All objects of an aggregate are owned by the aggregate and are always edited through the root. The concept defines a natural job and transactional boundary.

Create
The creation operation is integrated with the update operation update(Object)
Read
The read all items operation maps to the items() operation. The read an item with a unique key operation maps to findBy(Function, Object).
Update
The update operation maps to the operation update(Object).
Delete
The delete operation maps to the operation delete(Object).
  • Constructor Details

  • Method Details

    • findByOid

      public static <E extends HasOid, Long> Optional<E> findByOid(@NotNull @NotNull Provider<E> provider, long oid)
    • findById

      public static <E extends HasId, String> Optional<E> findById(@NotNull @NotNull Provider<E> provider, @NotNull String id)
    • containsById

      public static boolean containsById(@NotNull @NotNull Provider<? extends HasId> provider, @NotNull @NotNull String id)
    • items

      public abstract List<T> items()
      Returns a list containing all known instances of the entity type.
      Returns:
      list of all instances
    • update

      public abstract void update(@NotNull T entity)
      Updates the data associated with the entity. If the entity is new, the update is handled as a create operation. The update is transitive and all referenced entities are also updated. The entity given as a parameter becomes the instance managed through the provider.
      Parameters:
      entity - entity to update
    • delete

      public abstract void delete(@NotNull T entity)
      Deletes the data associated with the entity. The object identifier is invalidated.
      Parameters:
      entity - entity to delete
    • deleteAll

      public abstract void deleteAll()
      Deletes all the entities managed by the provider.
    • replace

      public void replace(T oldValue, T newValue)
      Replaces an existing value with a new one. A null value is ignored.
      Parameters:
      oldValue - remove the old value if not null
      newValue - add the new value if not null
    • updateAll

      public void updateAll(@NotNull @NotNull Iterable<? extends T> items)
      Updates the data associated with all entities.
      Parameters:
      items - entities to update
    • findBy

      public <U> Optional<T> findBy(@NotNull @NotNull Function<T,U> getter, U value)
      Returns the first entity which property matches the value.
      Type Parameters:
      U - type of the property
      Parameters:
      getter - getter to retrieve the property
      value - value to compare with
      Returns:
      optional of the first matching entity otherwise empty
    • mutex

      protected ReentrantReadWriteLock mutex()
    • execute

      protected void execute(@NotNull @NotNull Runnable runnable)