Skip to content

Commit

Permalink
Simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
reiss authored and reiss committed Nov 8, 2017
1 parent c43b8f3 commit 71c67b5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Set;

import com.dukascopy.api.ICurrency;
import com.dukascopy.api.Instrument;
Expand All @@ -19,30 +20,24 @@ public class CrossInstrument {
private final Instrument secondInstrument;
private final Instrument instrument;
private final ICurrency crossCurrency;
private final boolean isEmpty;
private final boolean shouldDivide;
private final int pipScale;
private final Set<Instrument> crossInstruments;
private final RoundingMode roundingMode = RoundingMode.HALF_UP;

public CrossInstrument(final Instrument firstInstrument,
final Instrument secondInstrument) {
this.firstInstrument = firstInstrument;
this.secondInstrument = secondInstrument;

crossInstruments = Sets.newHashSet(firstInstrument, secondInstrument);
final Maybe<Instrument> maybeCross = InstrumentFactory.maybeCross(firstInstrument, secondInstrument);

instrument = maybeCross.blockingGet();
isEmpty = maybeCross
.isEmpty()
.blockingGet();
crossCurrency = calcCrossCurrency();
shouldDivide = shouldDivide();
pipScale = instrument.getPipScale() + 1;
}

public boolean isValid() {
return !isEmpty;
}

public Instrument get() {
return instrument;
}
Expand All @@ -63,11 +58,11 @@ public FxRate rate(final FxRate rateA,
final double crossValue = shouldDivide
? bdcFirst.divide(bdcSecond,
pipScale,
RoundingMode.HALF_UP)
roundingMode)
.doubleValue()
: bdcFirst
.multiply(bdcSecond)
.setScale(pipScale, RoundingMode.HALF_UP)
.setScale(pipScale, roundingMode)
.doubleValue();

return new FxRate(crossValue, instrument);
Expand All @@ -77,8 +72,7 @@ private ICurrency calcCrossCurrency() {
return CurrencyFactory
.fromInstruments(firstInstrument, secondInstrument)
.stream()
.filter(currency -> CurrencyUtil.isInAllInstruments(currency,
Sets.newHashSet(firstInstrument, secondInstrument)))
.filter(currency -> CurrencyUtil.isInAllInstruments(currency, crossInstruments))
.collect(toSet())
.iterator()
.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -26,14 +25,6 @@ public void setUp() {
crossInstrumentD = new CrossInstrument(instrumentEURGBP, instrumentGBPJPY);
}

@Test
public void instanceIsValid() {
assertTrue(crossInstrumentA.isValid());
assertTrue(crossInstrumentB.isValid());
assertTrue(crossInstrumentC.isValid());
assertTrue(crossInstrumentD.isValid());
}

@Test
public void instrumentIsCorrect() {
assertThat(crossInstrumentA.get(), equalTo(instrumentEURGBP));
Expand Down

0 comments on commit 71c67b5

Please sign in to comment.