Package com.opengamma.livedata.test

Source Code of com.opengamma.livedata.test.TestLiveDataClient

/**
* Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.livedata.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.fudgemsg.FudgeMsg;

import com.opengamma.livedata.LiveDataSpecification;
import com.opengamma.livedata.LiveDataValueUpdateBean;
import com.opengamma.livedata.UserPrincipal;
import com.opengamma.livedata.client.AbstractLiveDataClient;
import com.opengamma.livedata.client.SubscriptionHandle;
import com.opengamma.livedata.msg.LiveDataSubscriptionResponse;
import com.opengamma.livedata.msg.LiveDataSubscriptionResult;

/**
* A {@code LiveDataClient} that works completely in memory
* and actually does not contact any server.
*/
public class TestLiveDataClient extends AbstractLiveDataClient {
 
  private final List<LiveDataSpecification> _cancelRequests = new ArrayList<LiveDataSpecification>();
  private final List<Collection<SubscriptionHandle>> _subscriptionRequests = new ArrayList<Collection<SubscriptionHandle>>();
  private final AtomicLong _sequenceGenerator = new AtomicLong(0);
 
  @Override
  protected void cancelPublication(LiveDataSpecification fullyQualifiedSpecification) {
    _cancelRequests.add(fullyQualifiedSpecification);
  }

  @Override
  protected void handleSubscriptionRequest(Collection<SubscriptionHandle> subHandles) {
    _subscriptionRequests.add(subHandles);
    for (SubscriptionHandle subHandle : subHandles) {
      LiveDataSubscriptionResponse response = new LiveDataSubscriptionResponse(
          subHandle.getRequestedSpecification(),
          LiveDataSubscriptionResult.SUCCESS,
          null,
          subHandle.getRequestedSpecification(),
          "test distribution spec",
          null);       
      subscriptionRequestSatisfied(subHandle, response);
    }
  }

  public List<LiveDataSpecification> getCancelRequests() {
    return _cancelRequests;
  }

  public List<Collection<SubscriptionHandle>> getSubscriptionRequests() {
    return _subscriptionRequests;
  }
 
  public void marketDataReceived(LiveDataSpecification fullyQualifiedSpecification, FudgeMsg fields) {
    LiveDataValueUpdateBean bean = new LiveDataValueUpdateBean(_sequenceGenerator.incrementAndGet(), fullyQualifiedSpecification, fields);
    getValueDistributor().notifyListeners(bean);
  }

  @Override
  public Map<LiveDataSpecification, Boolean> isEntitled(UserPrincipal user,
      Collection<LiveDataSpecification> requestedSpecifications) {
    Map<LiveDataSpecification, Boolean> returnValue = new HashMap<LiveDataSpecification, Boolean>();
    for (LiveDataSpecification spec : requestedSpecifications) {
      returnValue.put(spec, isEntitled(user, spec));           
    }
    return returnValue;
  }

  @Override
  public boolean isEntitled(UserPrincipal user, LiveDataSpecification requestedSpecification) {
    return true;
  }
 
}
TOP

Related Classes of com.opengamma.livedata.test.TestLiveDataClient

TOP
Copyright © 2018 www.massapi.com. 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.