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

  • 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
    • Constructor Detail

      • 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 Detail

      • 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.
      • 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.
      • internalDirectGetAll

        @Nonnull
        @ReturnsMutableCopy
        @IsLocked(READ)
        protected final Iterable<IMPLTYPE> internalDirectGetAll()
      • 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.
      • isNotEmpty

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

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

        @IsLocked(READ)
        protected final void internalForEachValue​(@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.
      • 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