Class TranslationQueue


  • public class TranslationQueue
    extends java.lang.Object
    Translation queue object that holds the translations required from one language to another. The queue is enabled to have the same translation multiple times and only queries the translator once. Future implementations could hold a history of the most used ones, etc...
    Author:
    Christopher Mindus
    • Constructor Summary

      Constructors 
      ModifierConstructorDescription
      protectedTranslationQueue​(java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode, ITranslator translator)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and TypeMethodDescription
      protected booleanaddRequest​(KString sourceText, ITranslationReply reply)
      Adds a translation request.
      protected voidcancel()
      Cancels the queue.
      protected voidcancelJob()
      Cancels the job instance if any, called from a synchronized block.
      protected voidclearJobInstance()
      Clears the job instance if any, called from a synchronized block.
      voiddeconfigure()
      Deconfigures the queue and its translator.
      protected KString[]getAll()
      Gets all current elements in queue, without removing them.
      protected KStringgetNext()
      Gets an item from the queue that needs processing.
      protected intgetQueueElementCount()
      Gets the queue translations count.
      longgetTranslatedCount()
      Gets the count of translated texts in this queue.
      protected booleanisCanceled()
      Checks the cancel state.
      protected voidonCompleted​(KString sourceText, Translation translation)
      Informs the queue of a completed translation.
      protected booleanprocessQueuedElement()
      Processes one element from queue.
      protected voidscheduleJobInstance()
      Schedules a new translation job instance if required, called from a synchronized block.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • sourceLanguageCode

        protected final java.lang.String sourceLanguageCode
        The source language code, null for auto-detect.
      • targetLanguageCode

        protected final java.lang.String targetLanguageCode
        The target language code.
      • sourceLanguageDescription

        protected final java.lang.String sourceLanguageDescription
        The description of source language.
      • targetLanguageDescription

        protected final java.lang.String targetLanguageDescription
        The description of target language.
      • translator

        protected final ITranslator translator
        The translator.
    • Constructor Detail

      • TranslationQueue

        protected TranslationQueue​(java.lang.String sourceLanguageCode,
                                   java.lang.String targetLanguageCode,
                                   ITranslator translator)
        Constructor.
        Parameters:
        sourceLanguageCode - The source language code, null for auto-detect.
        targetLanguageCode - The target language code.
        translator - The translator.
    • Method Detail

      • addRequest

        protected boolean addRequest​(KString sourceText,
                                     ITranslationReply reply)
        Adds a translation request.
        Parameters:
        sourceText - The source text.
        reply - Callback when translation completes.
        Returns:
        true for new request added, false for request already pending.
      • scheduleJobInstance

        protected void scheduleJobInstance()
        Schedules a new translation job instance if required, called from a synchronized block.

        Override as required.

      • getNext

        protected KString getNext()
        Gets an item from the queue that needs processing.
        Returns:
        The string to translate, null for no more entries.
      • getAll

        protected KString[] getAll()
        Gets all current elements in queue, without removing them.
        Returns:
        The string array to translate, null for no more entries.
      • clearJobInstance

        protected void clearJobInstance()
        Clears the job instance if any, called from a synchronized block.

        Override as required.

      • cancelJob

        protected void cancelJob()
        Cancels the job instance if any, called from a synchronized block.

        Override as required.

      • cancel

        protected void cancel()
        Cancels the queue.
      • isCanceled

        protected boolean isCanceled()
        Checks the cancel state.
      • getQueueElementCount

        protected int getQueueElementCount()
        Gets the queue translations count.
      • processQueuedElement

        protected boolean processQueuedElement()
        Processes one element from queue.
        Returns:
        true for success, false for no more elements in queue.
      • onCompleted

        protected void onCompleted​(KString sourceText,
                                   Translation translation)
        Informs the queue of a completed translation.
        Parameters:
        sourceText - The source text.
        translation - The translation instance.
      • getTranslatedCount

        public long getTranslatedCount()
        Gets the count of translated texts in this queue.
        Returns:
        The count of successful translations.
      • deconfigure

        public void deconfigure()
        Deconfigures the queue and its translator.