Class MessageStore<M extends Message>

java.lang.Object
eu.bandm.tools.message.MessageStore<M>
Type Parameters:
M - the type of the messages
All Implemented Interfaces:
MessageReceiver<M>

public class MessageStore<M extends Message> extends Object implements MessageReceiver<M>
Common class for different kinds of message storages. This class provides the basis for a graphic interfaces and bulk processing.

All messages received are added to an internal sequential storage. When the field order contains a non-null Comparator, all incoming messages are sorted accordingly; messages which compare to zero = equal appear chronologically. When the field order contains null, all incoming messages are stored chronologically. When the comparator changes to a non-null value, all messages are re-sorted. When it changes to null, no re-sorting happens, and all future messages are appended chronologically.

There are two kinds of graphic representations, both in the context of javax.swing, namely MessageStore<M extends Message>.TableModel and MessageStore<M extends Message>.ListModel. The user can derive from these classes and provide its own instance by setTableModel(TableModel) and setListModel(ListModel). If not, a standard implementation will be provided by getTableModel() and getListModel(). A standard GUI implementation using the table model is SwingMessageTable.

This class can also be used for collecting, sorting and further processing: all messages currently stored can be propagated explicitly to some MessageReceiver by calling drainTo(MessageReceiver).

  • Field Details

    • messages

      protected final List<M extends Message> messages
      All stored messages. Either ordered by applying the comparator order or by arrival. levels is a parallel storage for the nesting levels of the messages, which can be represented e.g. by GUIs.
    • tableModel

      @Opt protected MessageStore<M extends Message>.@Opt TableModel tableModel
      The Swing table model, either user defined or default.
    • listModel

      @Opt protected MessageStore<M extends Message>.@Opt ListModel listModel
      The Swing list model, either user defined or default.
    • order

      @Opt protected @Opt Comparator<? super M extends Message> order
      If set, defines the storage order. Otherwise it is by arrival.
    • nestingLevel

      protected int nestingLevel
      Internal state for storing the nesting of Message.Kind.logStart and Message.Kind.logEnd messages. Is always >=0, also in case of inconsistent nesting of incoming messages.
    • levels

      Stores the nesting level for all received messages. The chosen implementation allows re-sorting of the messages while keeping the nesting information.
  • Constructor Details

    • MessageStore

      public MessageStore()
      Only constructor.
  • Method Details

    • drainTo

      public void drainTo(MessageReceiver<? super M> receiver)
      Send all currently contained messages (in their stored order) to the receiver. The contents of the store is not altered.

      This method is synchronized, so that a further sending to this store cannot interfer.

      Parameters:
      receiver - to which the messages shall be sent.
    • drainAndClear

      public void drainAndClear(MessageReceiver<? super M> receiver)
      Send all currently contained messages to the receiver and clear the storage.

      This method is synchronized, so that a further sending to this store cannot interfer.

      Parameters:
      receiver - to which the messages shall be sent.
    • getMessages

      public List<M> getMessages()
      Returns a copy of the current content. The returned list is unmodifiable, but the contained messages are identical to those in this instance. Thus they can be manipulated. which will be reflected by this instance. The returned list will reflect all future changes to this instance.
      Returns:
      the list of all currently contained messages.
    • get

      public M get(int i)
      Return the message stored at the given position. The returned instance is identical to that contained in the message store; thus the message store will reflect any changes to it.
      Parameters:
      i - the position of the returned message, >= 0.
      Returns:
      the message stored at the given poisition.
      Throws:
      IndexOutOfBoundsException - - if the index is out of range (index < 0 || index >= getSize())
    • getSize

      public int getSize()
      Returns the number of currently contained messages.
      Returns:
      the number of currently contained messages.
    • isEmpty

      public boolean isEmpty()
      Returns whether the store is empty.
      Returns:
      whether the store is empty.
    • setTableModel

      public void setTableModel(@Opt MessageStore<M>.@Opt TableModel tm)
      Provide a user-defined table model. Can be set to null to prevent all further GUI updates.
      Parameters:
      tm - the table model to be used.
      See Also:
    • setListModel

      public void setListModel(@Opt MessageStore<M>.@Opt ListModel lm)
      Provide a user-defined list model. Can be set to null to prevent all further GUI updates.
      Parameters:
      lm - the list model to be used.
      See Also:
    • getTableModel

      public MessageStore<M>.TableModel getTableModel()
      Returns the user-defined table model, if set, or a lazy constructed instance of the default table model MessageStore<M extends Message>.TableModel.
      Returns:
      the currently used table model.
      See Also:
    • getListModel

      public MessageStore<M>.ListModel getListModel()
      Returns the user-defined list model, if set, or a lazy constructed instance of the default list model MessageStore<M extends Message>.ListModel.
      Returns:
      the currently used list model.
      See Also:
    • clear

      public void clear()
      Clears the internal storage, thus deletes all messages, and notifies the GUIs.
    • notifyGuiAdd

      protected void notifyGuiAdd(int i)
      An item has been inserted at a particular position.
      Parameters:
      i - the position of the insertion.
    • notifyGuiChanged_ALL

      protected void notifyGuiChanged_ALL()
      Called in case of list contents change by setOrder(Comparator) or clear().
    • setOrder

      public void setOrder(@Opt @Opt Comparator<? super M> comp)
      Set the comparator which rules the sequential order of storing. The current content will be re-ordered and the GUI models will be notified.
      Parameters:
      comp - the comparator to apply. If it is null, then no re-ordering takes place, and future messages will again be appended to the end of the store.
    • getOrder

      @Opt public @Opt Comparator<? super M> getOrder()
      Returns the comparator which rules the sequential order of storing.
      Returns:
      the comparator which rules the sequential order of storing.
    • clearNestingLevel

      public void clearNestingLevel()
      Resets the current nesting level to 0.
    • receive

      public void receive(M message)
      Store the message to the interal list and notify the GUI representations.
      Specified by:
      receive in interface MessageReceiver<M extends Message>
      Parameters:
      message - to receive.