# Path: pypipeline/core/Processor.py
# class Processor:
#     def __init__(self, obj):
#         self.object = obj
#         self.next = None
# 
#     def process(self, exchange):
#         try:
#             self._process(exchange)
#         except Exception as e:
#             raise e
# 
#     def _process(self, exchange):
#         self.object.process(exchange)
#         if self.next is not None:
#             self.next.process(exchange)


from pypipeline.core.Processor import Processor
import time


class ResequencerProcessor(Processor):

    def __init__(self, resequencer):
        super().__init__(None)
        self.resequencer = resequencer
        self.exchanges = []
        if resequencer.timeout is not None:
            self.time = time.time()
        if resequencer.count is not None:
            self.current_count = 0

    def _process(self, exchange):
        self.exchanges.append(exchange)
        self.current_count += 1
        if self.resequencer.count is not None and self.current_count == self.resequencer.count:
            self.resequencer.resequence(self.exchanges)
            self.forward(self.exchanges)
            return
        if self.resequencer.timeout is not None and time.time() - self.time >= self.resequencer.timeout:
            self.resequencer.resequence(self.exchanges)
            self.forward(self.exchanges)
            return

    def forward(self, exchanges):
        if self.next is not None:

# Path: pypipeline/core/Processor.py
# class Processor:
#     def __init__(self, obj):
#         self.object = obj
#         self.next = None
# 
#     def process(self, exchange):
#         try:
#             self._process(exchange)
#         except Exception as e:
#             raise e
# 
#     def _process(self, exchange):
#         self.object.process(exchange)
#         if self.next is not None:
#             self.next.process(exchange)


# Path: pypipeline/eip/resequence/ResequencerProcessor.py
from pypipeline.core.Processor import Processor
import time



class ResequencerProcessor(Processor):

    def __init__(self, resequencer):
        super().__init__(None)
        self.resequencer = resequencer
        self.exchanges = []
        if resequencer.timeout is not None:
            self.time = time.time()
        if resequencer.count is not None:
            self.current_count = 0

    def _process(self, exchange):
        self.exchanges.append(exchange)
        self.current_count += 1
        if self.resequencer.count is not None and self.current_count == self.resequencer.count:
            self.resequencer.resequence(self.exchanges)
            self.forward(self.exchanges)
            return
        if self.resequencer.timeout is not None and time.time() - self.time >= self.resequencer.timeout:
            self.resequencer.resequence(self.exchanges)
            self.forward(self.exchanges)
            return

    def forward(self, exchanges):
        if self.next is not None:

            for exchange in exchanges: