Package org.apache.openjpa.trader.domain

Examples of org.apache.openjpa.trader.domain.Stock


public class SectorBasedQueryTargetPolicy implements QueryTargetPolicy {

  @Override
  public String[] getTargets(String query, Map<Object, Object> params,
      String language, List<String> slices, Object context) {       
    Stock stock = null;
    if (TradingService.MATCH_ASK.equals(query)) {
      stock = ((Tradable)params.get("ask")).getStock();
    } else if (TradingService.MATCH_BID.equals(query)) {
      stock = ((Tradable)params.get("bid")).getStock();
    }
        return stock != null ? new String[]{slices.get(stock.getSector().ordinal())} : null;
  }
View Full Code Here


     * This distribution policy determines the sector of the stock and
     * picks the slice of the given list of slices at ordinal index of the
     * enumerated Sector.
     */
    public String distribute(Object pc, List<String> slices, Object context) {
        Stock stock = null;
        if (pc instanceof Tradable) {
            stock = ((Tradable)pc).getStock();
        } else if (pc instanceof Stock) {
            stock = (Stock)pc;
        } else if (pc instanceof Trade) {
          stock = ((Trade)pc).getStock();
        } else if (pc instanceof Trader) {
            throw new IllegalArgumentException("Trader should have been replicated");
        }
        return stock != null ? slices.get(stock.getSector().ordinal()) : null;
    }
View Full Code Here

    private static Random rng = new Random(System.currentTimeMillis());
   
    public MockTradingService() {
        Sector[] sectors = Sector.values();
        for (int i = 0; i < 10; i++) {
            Stock stock = new Stock("Stock-"+i, "Company-"+i, sectors[rng.nextInt(sectors.length)],
                    10*rng.nextDouble());
            _stocks.add(stock);
        }
    }
View Full Code Here

        initialize(stocks.get(0), false);

        symbols.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent event) {
                Stock stock = getSelectedStock();
                initialize(stock, false);
            }
        });

        ask.addClickHandler(new ClickHandler() {
View Full Code Here

     * ---------------------------------------------------------------------------------
     * Service Event Response Management
     * ---------------------------------------------------------------------------------
     */
    public void onStockUpdated(ServiceEvent.StockUpdated event) {
        Stock updated = event.getPayload();
        Stock current = getSelectedStock();
        if (updated.equals(current)) {
            initialize(updated, true);
        }
    }
View Full Code Here

    }

    @Override
    public void onStockUpdated(ServiceEvent.StockUpdated event) {
        int n = getRowCount();
        Stock updatedStock = event.getPayload();
        for (int i = 0; i < n; i++) {
            Tradable t = get(i);
            if (updatedStock.equals(t.getStock())) {
                t.updateStock(updatedStock);
                update(t, new Integer[]{1,4});
            }
        }
    }
View Full Code Here

    }

    public Stock getStock(String symbol) {
        EntityManager em = getEntityManager();
        begin();
        Stock stock = em.find(Stock.class, symbol);
        em.refresh(stock);
        commit();
        return stock;
    }
View Full Code Here

        begin();
        List<Stock> stocks = em.createQuery(GET_ALL_STOCKS, Stock.class).getResultList();
        if (stocks.isEmpty()) {
            for (int i = 0; i < data.length; i++) {
                Object[] d = data[i];
                Stock stock = new Stock((String)d[0], (String)d[0], (Sector)d[1], (Double)d[2]);
                em.persist(stock);
            }
       
            for (int i = 0; i < 4; i++) {
                Trader trader = new Trader("Trader-"+i);
View Full Code Here

        em.getTransaction().begin();
        List<Stock> stocks = em.createQuery(GET_ALL_STOCKS, Stock.class).getResultList();
        int n = stocks.size();
        for (int i = 0; i < n; i++) {
            if (rng.nextDouble() < 0.25) {
                Stock stock = stocks.get(i);
                double oldPrice = stock.getMarketPrice();
                double delta = (rng.nextDouble() - 0.5) * MAX_CHANGE;
                double newPrice = Math.max(oldPrice + delta, 0.01);
                stock.setMarketPrice(newPrice);
            }
        }
        em.getTransaction().commit();
    }
View Full Code Here

TOP

Related Classes of org.apache.openjpa.trader.domain.Stock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.