Skip to content

Commit

Permalink
Merge pull request #1289 from zholmes1/develop
Browse files Browse the repository at this point in the history
Added flagging capability for ExchangeFactory
  • Loading branch information
timmolter committed Jul 25, 2016
2 parents 13217af + 110340d commit 2ecd333
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
23 changes: 15 additions & 8 deletions xchange-core/src/main/java/org/knowm/xchange/BaseExchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ public abstract class BaseExchange implements Exchange {
protected PollingTradeService pollingTradeService;
protected PollingAccountService pollingAccountService;
protected StreamingExchangeService streamingExchangeService;

@Override
public void applySpecification(ExchangeSpecification exchangeSpecification) {
applySpecification(exchangeSpecification, true);
}

@Override
public void applySpecification(ExchangeSpecification exchangeSpecification, boolean doRemoteInit) {

ExchangeSpecification defaultSpecification = getDefaultExchangeSpecification();

Expand Down Expand Up @@ -113,13 +118,15 @@ public void applySpecification(ExchangeSpecification exchangeSpecification) {
}

initServices();

try {
remoteInit();
} catch (ExchangeException e) {
throw e;
} catch (IOException e) {
throw new ExchangeException(e.getMessage());

if (doRemoteInit) {
try {
remoteInit();
} catch (ExchangeException e) {
throw e;
} catch (IOException e) {
throw new ExchangeException(e.getMessage());
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions xchange-core/src/main/java/org/knowm/xchange/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public interface Exchange {
* @param exchangeSpecification The {@link ExchangeSpecification}
*/
void applySpecification(ExchangeSpecification exchangeSpecification);

/**
* Applies any exchange specific parameters
*
* @param exchangeSpecification The {@link ExchangeSpecification}
* @param doRemoteInit Call {@link remoteInit}
*/
void applySpecification(ExchangeSpecification exchangeSpecification, boolean doRemoteInit);

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
public enum ExchangeFactory {

INSTANCE;

private boolean doRemoteInit = true;

// flags
public static final int DO_REMOTE_INIT_TRUE = 10000;
public static final int DO_REMOTE_INIT_FALSE = 10001;

private final Logger log = LoggerFactory.getLogger(ExchangeFactory.class);

Expand All @@ -26,6 +32,27 @@ public enum ExchangeFactory {
private ExchangeFactory() {

}

/**
* Adds a flag, for example, disabling remoteInit when the {@link Exchange} is created
* @param flag See public static final ints in this class
* @return this
*/
public ExchangeFactory setFlag(int flag) {

switch (flag) {
case DO_REMOTE_INIT_TRUE:
doRemoteInit = true;
break;
case DO_REMOTE_INIT_FALSE:
doRemoteInit = false;
break;
default:
throw new IllegalArgumentException("That is not a valid flag for ExchangeFactory.");
}

return this;
}

/**
* Create an Exchange object.
Expand Down Expand Up @@ -53,7 +80,7 @@ public Exchange createExchange(String exchangeClassName) {
if (Exchange.class.isAssignableFrom(exchangeProviderClass)) {
// Instantiate through the default constructor and use the default exchange specification
Exchange exchange = (Exchange) exchangeProviderClass.newInstance();
exchange.applySpecification(exchange.getDefaultExchangeSpecification());
exchange.applySpecification(exchange.getDefaultExchangeSpecification(), doRemoteInit);
return exchange;
} else {
throw new ExchangeException("Class '" + exchangeClassName + "' does not implement Exchange");
Expand Down Expand Up @@ -104,5 +131,7 @@ public Exchange createExchange(ExchangeSpecification exchangeSpecification) {
// Cannot be here due to exceptions

}



}

0 comments on commit 2ecd333

Please sign in to comment.