Class ChatQuery


  • public class ChatQuery
    extends java.lang.Object
    ChatQuery is a chainable query builder for filtering and processing collections of KSChatMessages using various criteria such as message content, type, sender, timestamps, and more. It safely caches the original data internally and applies dynamic filters on-demand using a composite Predicate.
    • Constructor Summary

      Constructors 
      Constructor Description
      ChatQuery​(java.util.Collection<KSChatMessages> chats)
      Constructs a new ChatQuery from a collection of chat messages.
    • Constructor Detail

      • ChatQuery

        public ChatQuery​(java.util.Collection<KSChatMessages> chats)
        Constructs a new ChatQuery from a collection of chat messages.
        Parameters:
        chats - The collection of chat messages to initialize the query with.
    • Method Detail

      • filter

        public ChatQuery filter​(java.util.function.Predicate<? super KSChatMessages> newPred)
        Adds a custom filter predicate to the query.
        Parameters:
        newPred - The predicate to apply.
        Returns:
        The updated ChatQuery instance for chaining.
      • contains

        public ChatQuery contains​(java.lang.String string)
        Filters messages that contain a specified string in their message content.
        Parameters:
        string - The substring or wildcard pattern to match.
        Returns:
        The updated ChatQuery instance.
      • from

        public ChatQuery from​(java.lang.String string)
        Filters messages sent by a specific username.
        Parameters:
        string - The sender's name (wildcard supported).
        Returns:
        The updated ChatQuery instance.
      • withType

        public ChatQuery withType​(KSChatMessages.MessageTypes type)
        Filters messages of a specific chat type.
        Parameters:
        type - The desired MessageTypes enum.
        Returns:
        The updated ChatQuery instance.
      • withoutType

        public ChatQuery withoutType​(KSChatMessages.MessageTypes type)
        Excludes messages of a specific chat type.
        Parameters:
        type - The MessageTypes to exclude.
        Returns:
        The updated ChatQuery instance.
      • betweenTimestamps

        public ChatQuery betweenTimestamps​(long startTimestamp,
                                           long endTimestamp)
        Filters messages between two timestamps (inclusive lower bound, exclusive upper).
        Parameters:
        startTimestamp - The start timestamp (inclusive).
        endTimestamp - The end timestamp (exclusive).
        Returns:
        The updated ChatQuery instance.
      • messagesAgo

        public ChatQuery messagesAgo​(long duration,
                                     java.util.concurrent.TimeUnit timeUnit)
        Filters messages sent within the given time duration from now.
        Parameters:
        duration - The duration to look back.
        timeUnit - The unit of time (e.g., SECONDS, MINUTES).
        Returns:
        The updated ChatQuery instance.
      • sorted

        public ChatQuery sorted​(java.util.Comparator<KSChatMessages> comparator)
        Returns a new ChatQuery with messages sorted using a given comparator.
        Parameters:
        comparator - The comparator to sort messages.
        Returns:
        A new ChatQuery instance with sorted messages.
      • distinct

        public ChatQuery distinct()
        Returns a new ChatQuery containing only distinct messages.
        Returns:
        A new ChatQuery with duplicate messages removed.
      • empty

        public boolean empty()
        Checks if the filtered message stream is empty.
        Returns:
        True if no messages match the filters; otherwise, false.
      • exists

        public boolean exists()
        Checks if any messages match the current filter criteria.
        Returns:
        True if one or more messages exist; otherwise, false.
      • first

        public KSChatMessages first()
        Retrieves the first message in the filtered stream.
        Returns:
        The first matching KSChatMessages, or null if none.
      • last

        public KSChatMessages last()
        Retrieves the last message in the filtered stream.
        Returns:
        The last matching KSChatMessages, or null if none.
      • random

        public KSChatMessages random()
        Retrieves a random message from the filtered stream.
        Returns:
        A random KSChatMessages, or null if none.
      • count

        public int count()
        Counts the number of messages matching the current filters.
        Returns:
        The total number of matching messages.
      • list

        public java.util.List<KSChatMessages> list()
        Returns a list of all messages matching the current filters.
        Returns:
        A list of filtered KSChatMessages.
      • first

        public java.util.List<KSChatMessages> first​(int i)
        Returns the first N messages matching the filters.
        Parameters:
        i - The maximum number of messages to retrieve.
        Returns:
        A list containing up to 'i' messages.
      • last

        public java.util.List<KSChatMessages> last​(int i)
        Returns the last N messages matching the filters.
        Parameters:
        i - The number of messages to retrieve from the end.
        Returns:
        A list of the last 'i' messages.