All Downloads are FREE. Search and download functionalities are using the official Maven repository.

quickfix.ApplicationExtendedFunctionalAdapter Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
package quickfix;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * This is an adapter implementation of ApplicationExtended interface that transforms the usage into more
 * functional style. It breaks down each interface method into a number of single-method interfaces, which can be
 * supplied with lambda expressions. Each single-method interface has its own add and remove listener method.
 *
 * 
    *
  1. Support multiple listeners of the same operation, e.g. onLogon. The method of the listeners will be invoked * in the same order of when add method was invoked, i.e. FIFO
  2. *
  3. Support fail fast evaluation of canLogOn listeners. False will be returned for the first false value returned * from the predicate of canLogOn. Otherwise return true.
  4. *
  5. Provides a thread-safe way to delegate to, add and remove listeners under the assumption that adding and * removing listeners are rare.
  6. *
*/ public class ApplicationExtendedFunctionalAdapter extends ApplicationFunctionalAdapter implements ApplicationExtended { private final List> canLogonPredicates = new CopyOnWriteArrayList<>(); private final List> onBeforeSessionResetListeners = new CopyOnWriteArrayList<>(); /** * Add a Predicate of Session to the canLogon evaluation. * * @param canlogon the Predicate of Session to the canLogon evaluation. */ public void addCanLogOnPredicate(Predicate canlogon) { canLogonPredicates.add(canlogon); } /** * Remove a Predicate of Session from the canLogon evaluation. * * @param canlogon the Predicate of Session to the canLogon evaluation. */ public void removeCanLogOnPredicate(Predicate canlogon) { canLogonPredicates.remove(canlogon); } /** * Add a Consumer of SessionID to listen to onBeforeSessionReset operation. * * @param onBeforeSessionReset the Consumer of SessionID to listen to onBeforeSessionReset operation. */ public void addOnBeforeSessionResetListener(Consumer onBeforeSessionReset) { onBeforeSessionResetListeners.add(onBeforeSessionReset); } /** * Remove a Consumer of SessionID from onBeforeSessionReset operation. * * @param onBeforeSessionReset the Consumer of SessionID to listen to onBeforeSessionReset operation. */ public void removeBeforeSessionResetListener(Consumer onBeforeSessionReset) { onBeforeSessionResetListeners.remove(onBeforeSessionReset); } @Override public boolean canLogon(SessionID sessionID) { return canLogonPredicates.stream() .allMatch(p -> p.test(sessionID)); } @Override public void onBeforeSessionReset(SessionID sessionID) { onBeforeSessionResetListeners.forEach(c -> c.accept(sessionID)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy