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

io.scalecube.services.examples.orderbook.service.engine.OrderBooks Maven / Gradle / Ivy

package io.scalecube.services.examples.orderbook.service.engine;

import io.scalecube.services.examples.orderbook.service.engine.events.AddOrder;
import io.scalecube.services.examples.orderbook.service.engine.events.MatchOrder;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import reactor.core.publisher.Flux;

public class OrderBooks {

  private Map books = new HashMap<>();
  private Long2ObjectOpenHashMap orders;

  /**
   * Create order books from instruments.
   *
   * @param instruments the list of instruments (IDs)
   */
  public OrderBooks(List instruments) {
    this.orders = new Long2ObjectOpenHashMap<>();

    for (String instrument : instruments) {
      OrderBook book = new OrderBook();
      books.put(instrument, book);
    }
  }

  public Flux listenMatch(String instrument) {
    return books.get(instrument).matchListener();
  }

  public Flux listenAdd(String instrument) {
    return books.get(instrument).addListener();
  }

  public OrderBook book(String instrument) {
    return books.get(instrument);
  }

  /** Fake enterOrder implementation. */
  public void enterOrder(Order order, String instrument) {
    books
        .get(instrument)
        .enter(order.id(), order.level().side(), order.level().price(), order.size());
  }

  public void cancel(Order order) {
    orders.remove(order.id());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy