Class AbstractMapBasedWALDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>,IMPLTYPE extends INTERFACETYPE>

java.lang.Object
com.helger.dao.AbstractDAO
com.helger.dao.wal.AbstractWALDAO<IMPLTYPE>
com.helger.dao.wal.AbstractMapBasedWALDAO<INTERFACETYPE,IMPLTYPE>
Type Parameters:
INTERFACETYPE - Interface type to be handled
IMPLTYPE - Implementation type to be handled
All Implemented Interfaces:
com.helger.commons.lang.IHasSize, com.helger.commons.log.IHasConditionalLogger, IAutoSaveAware, IDAO, IMapBasedDAO<INTERFACETYPE>

@ThreadSafe public abstract class AbstractMapBasedWALDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>,IMPLTYPE extends INTERFACETYPE> extends AbstractWALDAO<IMPLTYPE> implements IMapBasedDAO<INTERFACETYPE>
Base class for WAL based DAO that uses a simple ICommonsMap for data storage.
Author:
Philip Helger
  • Field Details

  • Constructor Details

    • AbstractMapBasedWALDAO

      protected AbstractMapBasedWALDAO(@Nonnull Class<IMPLTYPE> aImplClass, @Nonnull com.helger.commons.io.relative.IFileRelativeIO aIO, @Nullable String sFilename, @Nonnull AbstractMapBasedWALDAO.InitSettings<IMPLTYPE> aInitSettings) throws DAOException
      Default constructor. Automatically tries to read the file in the constructor (except this is changed in the init settings). WAL based classes must have a fixed filename!
      Parameters:
      aImplClass - Implementation class. May not be null.
      aIO - IO abstraction to be used. May not be null.
      sFilename - The filename to read and write.
      aInitSettings - Optional initialization settings to be used. May not be null.
      Throws:
      DAOException - If reading and reading fails
  • Method Details

    • onRecoveryCreate

      @MustBeLocked(WRITE) protected void onRecoveryCreate(@Nonnull IMPLTYPE aItem)
      Description copied from class: AbstractWALDAO
      Called when a recovery is needed to create a new item.
      Specified by:
      onRecoveryCreate in class AbstractWALDAO<IMPLTYPE extends INTERFACETYPE>
      Parameters:
      aItem - The element to be created. Never null.
    • onRecoveryUpdate

      @MustBeLocked(WRITE) protected void onRecoveryUpdate(@Nonnull IMPLTYPE aItem)
      Description copied from class: AbstractWALDAO
      Called when a recovery is needed to update an existing item.
      Specified by:
      onRecoveryUpdate in class AbstractWALDAO<IMPLTYPE extends INTERFACETYPE>
      Parameters:
      aItem - The element to be updated. Never null.
    • onRecoveryDelete

      @MustBeLocked(WRITE) protected void onRecoveryDelete(@Nonnull IMPLTYPE aItem)
      Description copied from class: AbstractWALDAO
      Called when a recovery is needed to delete an existing item.
      Specified by:
      onRecoveryDelete in class AbstractWALDAO<IMPLTYPE extends INTERFACETYPE>
      Parameters:
      aItem - The element to be deleted. Never null.
    • onRead

      @Nonnull protected com.helger.commons.state.EChange onRead(@Nonnull com.helger.xml.microdom.IMicroDocument aDoc)
      Description copied from class: AbstractWALDAO
      Fill the internal structures with from the passed XML document. This method is only called within a write lock!
      Specified by:
      onRead in class AbstractWALDAO<IMPLTYPE extends INTERFACETYPE>
      Parameters:
      aDoc - The XML document to read from. Never null.
      Returns:
      EChange.CHANGED if reading the data changed something in the internal structures that requires a writing.
    • internalGetAllSortedByKey

      @MustBeLocked(READ) @CodingStyleguideUnaware protected final Collection<IMPLTYPE> internalGetAllSortedByKey()
    • createWriteData

      @Nonnull @MustBeLocked(READ) protected com.helger.xml.microdom.IMicroDocument createWriteData()
      Description copied from class: AbstractWALDAO
      Create the XML document that should be saved to the file. This method is only called within a write lock!
      Specified by:
      createWriteData in class AbstractWALDAO<IMPLTYPE extends INTERFACETYPE>
      Returns:
      The non-null document to write to the file.
    • callbacks

      @Nonnull @ReturnsMutableObject public final com.helger.commons.callback.CallbackList<IDAOChangeCallback<INTERFACETYPE>> callbacks()
    • markAsChanged

      @MustBeLocked(WRITE) @Deprecated @DevelopersNote("Avoid that this method is overridden") protected final void markAsChanged(@Nonnull IMPLTYPE aModifiedElement, @Nonnull EDAOActionType eActionType)
      Deprecated.
      Avoid that this method is overridden
      The super method must be called every time something changed in the DAO. It triggers the writing to a file if auto-save is active. This method must be called within a write-lock as it is not locked!
      Overrides:
      markAsChanged in class AbstractWALDAO<IMPLTYPE extends INTERFACETYPE>
      Parameters:
      aModifiedElement - The modified data element. May not be null.
      eActionType - The action that was performed. May not be null.
    • internalCreateItem

      @MustBeLocked(WRITE) @Nonnull protected final IMPLTYPE internalCreateItem(@Nonnull IMPLTYPE aNewItem)
      Add an item including invoking the callback. Must only be invoked inside a write-lock.
      Parameters:
      aNewItem - The item to be added. May not be null.
      Returns:
      The passed parameter as-is. Never null.
      Throws:
      IllegalArgumentException - If an item with the same ID is already contained
    • internalCreateItem

      @MustBeLocked(WRITE) @Nonnull protected final IMPLTYPE internalCreateItem(@Nonnull IMPLTYPE aNewItem, boolean bInvokeCallbacks)
      Add an item including invoking the callback. Must only be invoked inside a write-lock.
      Parameters:
      aNewItem - The item to be added. May not be null.
      bInvokeCallbacks - true to invoke callbacks, false to not do so.
      Returns:
      The passed parameter as-is. Never null.
      Throws:
      IllegalArgumentException - If an item with the same ID is already contained
      Since:
      9.2.1
    • internalUpdateItem

      @MustBeLocked(WRITE) protected final void internalUpdateItem(@Nonnull IMPLTYPE aItem)
      Update an existing item including invoking the callback. Must only be invoked inside a write-lock.
      Parameters:
      aItem - The item to be updated. May not be null.
      Throws:
      IllegalArgumentException - If no item with the same ID is already contained
    • internalUpdateItem

      @MustBeLocked(WRITE) protected final void internalUpdateItem(@Nonnull IMPLTYPE aItem, boolean bInvokeCallbacks)
      Update an existing item including invoking the callback. Must only be invoked inside a write-lock.
      Parameters:
      aItem - The item to be updated. May not be null.
      bInvokeCallbacks - true to invoke callbacks, false to not do so.
      Throws:
      IllegalArgumentException - If no item with the same ID is already contained
      Since:
      9.2.1
    • internalDeleteItem

      @MustBeLocked(WRITE) @Nullable protected final IMPLTYPE internalDeleteItem(@Nullable String sID)
      Delete the item by removing it from the map. If something was remove the onDeleteItem callback is invoked. Must only be invoked inside a write-lock.
      Parameters:
      sID - The ID to be removed. May be null.
      Returns:
      The deleted item. If null no such item was found and therefore nothing was removed.
    • internalDeleteItem

      @MustBeLocked(WRITE) @Nullable protected final IMPLTYPE internalDeleteItem(@Nullable String sID, boolean bInvokeCallbacks)
      Delete the item by removing it from the map. If something was remove the onDeleteItem callback is invoked. Must only be invoked inside a write-lock.
      Parameters:
      sID - The ID to be removed. May be null.
      bInvokeCallbacks - true to invoke callbacks, false to not do so.
      Returns:
      The deleted item. If null no such item was found and therefore nothing was removed.
      Since:
      9.2.1
    • internalMarkItemDeleted

      @MustBeLocked(WRITE) protected final void internalMarkItemDeleted(@Nonnull IMPLTYPE aItem)
      Mark an item as "deleted" without actually deleting it from the map. This method only triggers the update action but does not alter the item. Must only be invoked inside a write-lock.
      Parameters:
      aItem - The item that was marked as "deleted"
    • internalMarkItemDeleted

      @MustBeLocked(WRITE) protected final void internalMarkItemDeleted(@Nonnull IMPLTYPE aItem, boolean bInvokeCallbacks)
      Mark an item as "deleted" without actually deleting it from the map. This method only triggers the update action but does not alter the item. Must only be invoked inside a write-lock.
      Parameters:
      aItem - The item that was marked as "deleted"
      bInvokeCallbacks - true to invoke callbacks, false to not do so.
      Since:
      9.2.1
    • internalMarkItemUndeleted

      @MustBeLocked(WRITE) protected final void internalMarkItemUndeleted(@Nonnull IMPLTYPE aItem)
      Mark an item as "no longer deleted" without actually adding it to the map. This method only triggers the update action but does not alter the item. Must only be invoked inside a write-lock.
      Parameters:
      aItem - The item that was marked as "no longer deleted"
    • internalMarkItemUndeleted

      @MustBeLocked(WRITE) protected final void internalMarkItemUndeleted(@Nonnull IMPLTYPE aItem, boolean bInvokeCallbacks)
      Mark an item as "no longer deleted" without actually adding it to the map. This method only triggers the update action but does not alter the item. Must only be invoked inside a write-lock.
      Parameters:
      aItem - The item that was marked as "no longer deleted"
      bInvokeCallbacks - true to invoke callbacks, false to not do so.
      Since:
      9.2.1
    • internalRemoveAllItemsNoCallback

      @MustBeLocked(WRITE) @Nonnull protected final com.helger.commons.state.EChange internalRemoveAllItemsNoCallback()
      Remove all items without triggering any callback. Must only be invoked inside a write-lock.
      Returns:
      EChange.CHANGED if something was contained, EChange.UNCHANGED otherwise.
    • getNone

      @Nonnull @ReturnsMutableCopy public final <T> com.helger.commons.collection.impl.ICommonsList<T> getNone()
      Specified by:
      getNone in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Type Parameters:
      T - Response type.
      Returns:
      An empty collection of the suitable implementation type.
    • getAll

      @Nonnull @ReturnsMutableCopy @IsLocked(READ) public final com.helger.commons.collection.impl.ICommonsList<INTERFACETYPE> getAll()
      Specified by:
      getAll in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Returns:
      A list of all contained items. Never null.
    • getAll

      @Nonnull @ReturnsMutableCopy @IsLocked(READ) public final com.helger.commons.collection.impl.ICommonsList<INTERFACETYPE> getAll(@Nullable Predicate<? super INTERFACETYPE> aFilter)
      Specified by:
      getAll in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aFilter - Filter to be applied. May be null.
      Returns:
      A list of all contained items matching the filter. Never null.
    • internalDirectGetAll

      @Nonnull @ReturnsMutableCopy @IsLocked(READ) protected final Iterable<IMPLTYPE> internalDirectGetAll()
    • internalGetAll

      @Nonnull @ReturnsMutableCopy @IsLocked(READ) protected final com.helger.commons.collection.impl.ICommonsList<IMPLTYPE> internalGetAll(@Nullable Predicate<? super IMPLTYPE> aFilter)
    • findAll

      @IsLocked(READ) public final void findAll(@Nullable Predicate<? super INTERFACETYPE> aFilter, @Nonnull Consumer<? super INTERFACETYPE> aConsumer)
      Description copied from interface: IMapBasedDAO
      Find all items matching the filter and invoke the consumer on all matching entries.
      Specified by:
      findAll in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aFilter - The filter to be applied. May be null.
      aConsumer - The consumer to be invoked for all matches. May not be null.
    • getAllMapped

      @Nonnull @ReturnsMutableCopy @IsLocked(READ) public final <RETTYPE> com.helger.commons.collection.impl.ICommonsList<RETTYPE> getAllMapped(@Nullable Predicate<? super INTERFACETYPE> aFilter, @Nonnull Function<? super INTERFACETYPE,? extends RETTYPE> aMapper)
      Description copied from interface: IMapBasedDAO
      Get all contained items matching the provided filter and map them to something else.
      Specified by:
      getAllMapped in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Type Parameters:
      RETTYPE - Return type to which is mapped
      Parameters:
      aFilter - The filter to be applied. May be null.
      aMapper - The mapper to be invoked. May not be null.
      Returns:
      The list all matching and mapped elements. Never null.
    • findAllMapped

      @IsLocked(READ) public final <RETTYPE> void findAllMapped(@Nullable Predicate<? super INTERFACETYPE> aFilter, @Nonnull Function<? super INTERFACETYPE,? extends RETTYPE> aMapper, @Nonnull Consumer<? super RETTYPE> aConsumer)
      Description copied from interface: IMapBasedDAO
      Find all contained items that match the filter, map them to a different type and invoke the consumer on all mapped items.
      Specified by:
      findAllMapped in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Type Parameters:
      RETTYPE - Return type to which is mapped
      Parameters:
      aFilter - The filter to be applied. May be null.
      aMapper - The mapper to be invoked. May not be null.
      aConsumer - The consumer to be invoked for all matches. May not be null.
    • findFirst

      @IsLocked(READ) @Nullable public final INTERFACETYPE findFirst(@Nullable Predicate<? super INTERFACETYPE> aFilter)
      Description copied from interface: IMapBasedDAO
      Find the the first element that matches the filter.
      Specified by:
      findFirst in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aFilter - The filter to be applied. May be null.
      Returns:
      The first matching item or null.
    • findFirstMapped

      @Nullable @IsLocked(READ) public final <RETTYPE> RETTYPE findFirstMapped(@Nullable Predicate<? super INTERFACETYPE> aFilter, @Nonnull Function<? super INTERFACETYPE,? extends RETTYPE> aMapper)
      Specified by:
      findFirstMapped in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
    • isNotEmpty

      @IsLocked(READ) public final boolean isNotEmpty()
      Specified by:
      isNotEmpty in interface com.helger.commons.lang.IHasSize
    • containsAny

      @IsLocked(READ) public final boolean containsAny(@Nullable Predicate<? super INTERFACETYPE> aFilter)
      Specified by:
      containsAny in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aFilter - The filter to be applied. May be null.
      Returns:
      true if at least one item matches the filter
    • isEmpty

      @IsLocked(READ) public final boolean isEmpty()
      Specified by:
      isEmpty in interface com.helger.commons.lang.IHasSize
    • containsNone

      @IsLocked(READ) public final boolean containsNone(@Nullable Predicate<? super INTERFACETYPE> aFilter)
      Specified by:
      containsNone in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aFilter - The filter to be applied. May be null.
      Returns:
      true if no item matches the filter
    • containsOnly

      @IsLocked(READ) public final boolean containsOnly(@Nullable Predicate<? super INTERFACETYPE> aFilter)
      Specified by:
      containsOnly in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aFilter - The filter to be applied. May be null.
      Returns:
      true if all items match the filter
    • forEach

      @IsLocked(READ) public final void forEach(@Nullable BiConsumer<? super String,? super INTERFACETYPE> aConsumer)
      Description copied from interface: IMapBasedDAO
      Iterate each entry
      Specified by:
      forEach in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aConsumer - Consumer to use. May be null.
    • forEach

      @IsLocked(READ) public final void forEach(@Nullable BiPredicate<? super String,? super INTERFACETYPE> aFilter, @Nullable BiConsumer<? super String,? super INTERFACETYPE> aConsumer)
      Description copied from interface: IMapBasedDAO
      Iterate each entry
      Specified by:
      forEach in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aFilter - Optional filter to limit the entries for which the consumer is called. May be null.
      aConsumer - Consumer to use. May be null.
    • forEachKey

      @IsLocked(READ) public final void forEachKey(@Nullable Consumer<? super String> aConsumer)
      Description copied from interface: IMapBasedDAO
      Iterate each key
      Specified by:
      forEachKey in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aConsumer - Consumer to use. May be null.
    • forEachKey

      @IsLocked(READ) public final void forEachKey(@Nullable Predicate<? super String> aFilter, @Nullable Consumer<? super String> aConsumer)
      Description copied from interface: IMapBasedDAO
      Iterate each key
      Specified by:
      forEachKey in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aFilter - Optional filter to limit the entries for which the consumer is called. May be null.
      aConsumer - Consumer to use. May be null.
    • forEachValue

      @IsLocked(READ) public final void forEachValue(@Nullable Consumer<? super INTERFACETYPE> aConsumer)
      Description copied from interface: IMapBasedDAO
      Iterate each value
      Specified by:
      forEachValue in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aConsumer - Consumer to use. May be null.
    • internalForEachValue

      @IsLocked(READ) protected final void internalForEachValue(@Nullable Consumer<? super IMPLTYPE> aConsumer)
    • forEachValue

      @IsLocked(READ) public final void forEachValue(@Nullable Predicate<? super INTERFACETYPE> aFilter, @Nullable Consumer<? super INTERFACETYPE> aConsumer)
      Description copied from interface: IMapBasedDAO
      Iterate each value
      Specified by:
      forEachValue in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aFilter - Optional filter to limit the entries for which the consumer is called. May be null.
      aConsumer - Consumer to use. May be null.
    • internalForEachValue

      @IsLocked(READ) protected final void internalForEachValue(@Nullable Predicate<? super IMPLTYPE> aFilter, @Nullable Consumer<? super IMPLTYPE> aConsumer)
    • internalGetOfID

      @Nullable @MustBeLocked(READ) protected final IMPLTYPE internalGetOfID(@Nullable String sID)
      Same as getOfID(String) but the locking must happen on the called side.
      Parameters:
      sID - The ID to search. May be null.
      Returns:
      null if no such item exists
      Since:
      9.1.5
    • getOfID

      @Nullable @IsLocked(READ) protected final IMPLTYPE getOfID(@Nullable String sID)
      Find the element with the provided ID. Locking is done internally.
      Parameters:
      sID - The ID to search. May be null.
      Returns:
      null if no such item exists
    • getAtIndex

      @Nullable @IsLocked(READ) protected final INTERFACETYPE getAtIndex(@Nonnegative int nIndex)
      Get the item at the specified index. This method only returns defined results if an ordered map is used for data storage.
      Parameters:
      nIndex - The index to retrieve. Should be ≥ 0.
      Returns:
      null if an invalid index was provided.
    • internalContainsWithID

      @MustBeLocked(READ) protected final boolean internalContainsWithID(@Nullable String sID)
    • containsWithID

      @IsLocked(READ) public final boolean containsWithID(@Nullable String sID)
      Specified by:
      containsWithID in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      sID - The ID to be checked. May be null.
      Returns:
      true if an item with the provided ID is contained.
    • containsAllIDs

      @IsLocked(READ) public final boolean containsAllIDs(@Nullable Iterable<String> aIDs)
      Description copied from interface: IMapBasedDAO
      Check if all IDs are contained
      Specified by:
      containsAllIDs in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aIDs - IDs to check
      Returns:
      true if all IDs are contained
    • getAllIDs

      @Nonnull @ReturnsMutableCopy public final com.helger.commons.collection.impl.ICommonsSet<String> getAllIDs()
      Specified by:
      getAllIDs in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Returns:
      A set with the IDs of all contained items. Never null but maybe empty.
    • size

      @Nonnegative public final int size()
      Specified by:
      size in interface com.helger.commons.lang.IHasSize
    • getCount

      @Nonnegative public final int getCount(@Nullable Predicate<? super INTERFACETYPE> aFilter)
      Specified by:
      getCount in interface IMapBasedDAO<INTERFACETYPE extends com.helger.commons.id.IHasID<String>>
      Parameters:
      aFilter - The filter to be applied. May be null.
      Returns:
      The number of items matching the provided filter. Always ≥ 0.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractWALDAO<IMPLTYPE extends INTERFACETYPE>