Search in sources :

Example 46 with BiConsumer

use of java.util.function.BiConsumer in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class IntegrationTest method testRawOrderbookStream.

/**
 * Test the raw orderbook stream
 */
@Test(timeout = 10000)
public void testRawOrderbookStream() {
    final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
    // Await at least 20 callbacks
    final CountDownLatch latch = new CountDownLatch(20);
    try {
        bitfinexClient.connect();
        final RawOrderbookConfiguration orderbookConfiguration = new RawOrderbookConfiguration(BitfinexCurrencyPair.BTC_USD);
        final RawOrderbookManager rawOrderbookManager = bitfinexClient.getRawOrderbookManager();
        final BiConsumer<RawOrderbookConfiguration, RawOrderbookEntry> callback = (c, o) -> {
            Assert.assertTrue(o.getAmount().doubleValue() != 0);
            Assert.assertTrue(o.getPrice().doubleValue() != 0);
            Assert.assertTrue(o.getOrderId() >= 0);
            Assert.assertTrue(o.toString().length() > 0);
            latch.countDown();
        };
        rawOrderbookManager.registerOrderbookCallback(orderbookConfiguration, callback);
        rawOrderbookManager.subscribeOrderbook(orderbookConfiguration);
        latch.await();
        rawOrderbookManager.unsubscribeOrderbook(orderbookConfiguration);
        Assert.assertTrue(rawOrderbookManager.removeOrderbookCallback(orderbookConfiguration, callback));
        Assert.assertFalse(rawOrderbookManager.removeOrderbookCallback(orderbookConfiguration, callback));
    } catch (Exception e) {
        // Should not happen
        e.printStackTrace();
        Assert.assertTrue(false);
    } finally {
        bitfinexClient.close();
    }
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) OrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry) OrderBookPrecision(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookPrecision) Test(org.junit.Test) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) OrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderbookManager) OrderBookFrequency(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookFrequency) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) RawOrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.RawOrderbookManager) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) RawOrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry) BiConsumer(java.util.function.BiConsumer) ExecutedTrade(com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade) Assert(org.junit.Assert) BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) RawOrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.RawOrderbookManager) RawOrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry) CountDownLatch(java.util.concurrent.CountDownLatch) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) Test(org.junit.Test)

Example 47 with BiConsumer

use of java.util.function.BiConsumer in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class IntegrationTest method testTickerStream.

/**
 * Test the tick stream
 */
@Test(timeout = 60000)
public void testTickerStream() {
    final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
    // Await at least 2 callbacks
    final CountDownLatch latch = new CountDownLatch(2);
    try {
        bitfinexClient.connect();
        final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(BitfinexCurrencyPair.BTC_USD);
        final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
        final BiConsumer<BitfinexTickerSymbol, BitfinexTick> callback = (c, o) -> {
            latch.countDown();
        };
        orderbookManager.registerTickCallback(symbol, callback);
        orderbookManager.subscribeTicker(symbol);
        latch.await();
        Assert.assertTrue(bitfinexClient.isTickerActive(symbol));
        orderbookManager.unsubscribeTicker(symbol);
        Assert.assertFalse(bitfinexClient.isTickerActive(symbol));
        Assert.assertTrue(orderbookManager.removeTickCallback(symbol, callback));
        Assert.assertFalse(orderbookManager.removeTickCallback(symbol, callback));
    } catch (Exception e) {
        // Should not happen
        e.printStackTrace();
        Assert.assertTrue(false);
    } finally {
        bitfinexClient.close();
    }
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) OrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry) OrderBookPrecision(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookPrecision) Test(org.junit.Test) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) OrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderbookManager) OrderBookFrequency(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookFrequency) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) RawOrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.RawOrderbookManager) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) RawOrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry) BiConsumer(java.util.function.BiConsumer) ExecutedTrade(com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade) Assert(org.junit.Assert) BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) CountDownLatch(java.util.concurrent.CountDownLatch) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) Test(org.junit.Test)

Example 48 with BiConsumer

use of java.util.function.BiConsumer in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class IntegrationTest method testCandleStream.

/**
 * Test the candle stream
 */
@Test(timeout = 10000)
public void testCandleStream() {
    final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
    // Await at least 10 callbacks
    final CountDownLatch latch = new CountDownLatch(10);
    try {
        bitfinexClient.connect();
        final BitfinexCandlestickSymbol symbol = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1);
        final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
        final BiConsumer<BitfinexCandlestickSymbol, BitfinexTick> callback = (c, o) -> {
            latch.countDown();
        };
        orderbookManager.registerCandlestickCallback(symbol, callback);
        orderbookManager.subscribeCandles(symbol);
        latch.await();
        orderbookManager.unsubscribeCandles(symbol);
        Assert.assertTrue(orderbookManager.removeCandlestickCallback(symbol, callback));
        Assert.assertFalse(orderbookManager.removeCandlestickCallback(symbol, callback));
    } catch (Exception e) {
        // Should not happen
        e.printStackTrace();
        Assert.assertTrue(false);
    } finally {
        bitfinexClient.close();
    }
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) OrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry) OrderBookPrecision(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookPrecision) Test(org.junit.Test) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) OrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderbookManager) OrderBookFrequency(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookFrequency) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) RawOrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.RawOrderbookManager) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) RawOrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry) BiConsumer(java.util.function.BiConsumer) ExecutedTrade(com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade) Assert(org.junit.Assert) BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) CountDownLatch(java.util.concurrent.CountDownLatch) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) Test(org.junit.Test)

Example 49 with BiConsumer

use of java.util.function.BiConsumer in project arez by arez.

the class SpyEventProcessorTest method maintainNesting.

@Test
public void maintainNesting() throws Throwable {
    final TestSpyEventProcessor processor = new TestSpyEventProcessor();
    final ObserverCreatedEvent event = new ObserverCreatedEvent(new NullObserverInfo());
    final AtomicInteger handleCallCount = new AtomicInteger();
    final BiConsumer<SpyUtil.NestingDelta, ObserverCreatedEvent> handler = (delta, e) -> {
        handleCallCount.incrementAndGet();
        assertEquals(delta, SpyUtil.NestingDelta.NONE);
        assertEquals(e, event);
    };
    processor.on(ObserverCreatedEvent.class, handler);
    assertEquals(processor.getNestingDelta(event), SpyUtil.NestingDelta.NONE);
    assertEquals(handleCallCount.get(), 0);
    assertEquals(processor.getNestingLevel(), 0);
    assertEquals(processor._handleUnhandledEventCallCount, 0);
    processor.onSpyEvent(event);
    assertEquals(handleCallCount.get(), 1);
    assertEquals(processor.getNestingLevel(), 0);
    assertEquals(processor._handleUnhandledEventCallCount, 0);
}
Also used : TransactionCompletedEvent(arez.spy.TransactionCompletedEvent) TransactionStartedEvent(arez.spy.TransactionStartedEvent) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractArezExtrasTest(arez.extras.AbstractArezExtrasTest) BiConsumer(java.util.function.BiConsumer) ValueUtil(org.realityforge.guiceyloops.shared.ValueUtil) Test(org.testng.annotations.Test) ObserverCreatedEvent(arez.spy.ObserverCreatedEvent) Nonnull(javax.annotation.Nonnull) ObserverCreatedEvent(arez.spy.ObserverCreatedEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractArezExtrasTest(arez.extras.AbstractArezExtrasTest) Test(org.testng.annotations.Test)

Example 50 with BiConsumer

use of java.util.function.BiConsumer in project portfolio by buchen.

the class HelloBankPDFExtractor method addDividendTransaction.

@SuppressWarnings("nls")
private void addDividendTransaction() {
    DocumentType type = new DocumentType("Geschäftsart: Ertrag", (context, lines) -> {
        Pattern exchangeRatePattern = Pattern.compile("Devisenkurs: (?<exchangeRate>[\\d+,.]*) \\(\\d+.\\d+.\\d{4}+\\) [\\d+,.]* \\w{3}+ *");
        for (String line : lines) {
            Matcher matcher = exchangeRatePattern.matcher(line);
            if (matcher.matches())
                context.put("exchangeRate", matcher.group(1));
        }
    });
    this.addDocumentTyp(type);
    Block block = new Block("Geschäftsart: Ertrag");
    type.addBlock(block);
    BiConsumer<AccountTransaction, Map<String, String>> taxProcessor = (t, v) -> {
        long tax = asAmount(v.get("tax"));
        String currency = asCurrencyCode(v.get("currency"));
        if (currency.equals(t.getCurrencyCode())) {
            t.addUnit(new Unit(Unit.Type.TAX, Money.of(t.getCurrencyCode(), tax)));
        } else {
            String exchangeRateString = type.getCurrentContext().get("exchangeRate");
            if (exchangeRateString != null) {
                BigDecimal exchangeRate = BigDecimal.ONE.divide(asExchangeRate(exchangeRateString), 10, BigDecimal.ROUND_HALF_UP);
                if (currency.equals(t.getSecurity().getCurrencyCode())) {
                    long convertedTax = BigDecimal.valueOf(tax).multiply(exchangeRate).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
                    t.addUnit(new Unit(Unit.Type.TAX, Money.of(t.getCurrencyCode(), convertedTax), Money.of(currency, tax), exchangeRate));
                } else {
                    long convertedTax = BigDecimal.valueOf(tax).multiply(exchangeRate).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
                    t.addUnit(new Unit(Unit.Type.TAX, Money.of(t.getCurrencyCode(), convertedTax)));
                }
            }
        }
    };
    block.set(new Transaction<AccountTransaction>().subject(() -> {
        AccountTransaction transaction = new AccountTransaction();
        transaction.setType(AccountTransaction.Type.DIVIDENDS);
        return transaction;
    }).section("isin", "name", // 
    "currency").match(// 
    "Titel: (?<isin>\\S*) (?<name>.*)$").match("Dividende: [\\d+,.]* (?<currency>\\w{3}+) *").assign((t, v) -> t.setSecurity(getOrCreateSecurity(v))).section(// 
    "shares").match(// 
    "^(Abgang: )?(?<shares>[\\d+,.]*) Stk$").assign((t, v) -> t.setShares(asShares(v.get("shares")))).section("amount", "currency").match("Zu Gunsten .* (?<amount>[\\d+,.]*) (?<currency>\\w{3}+) *$").assign((t, v) -> {
        t.setAmount(asAmount(v.get("amount")));
        t.setCurrencyCode(asCurrencyCode(v.get("currency")));
    }).section(// 
    "date").match(// 
    "Valuta (?<date>\\d+.\\d+.\\d{4}+)").assign((t, v) -> t.setDateTime(asDate(v.get("date")))).section("tax", // 
    "currency").optional().match("Inkassoprovision: -(?<tax>[\\d+,.]*) (?<currency>\\w{3}+) *").assign(taxProcessor).section("tax", // 
    "currency").optional().match("Umsatzsteuer: -(?<tax>[\\d+,.]*) (?<currency>\\w{3}+) *").assign(taxProcessor).section("tax", // 
    "currency").optional().match(// 
    "Fremde Spesen: -(?<tax>[\\d+,.-]*) (?<currency>\\w{3}+) *").assign(taxProcessor).section("tax", // 
    "currency").optional().match(// 
    "KESt Ausländische Dividende: -(?<tax>[\\d+,.]*) (?<currency>\\w{3}+) *").assign(taxProcessor).section("tax", // 
    "currency").optional().match(// 
    "Quellensteuer[^.]*: -(?<tax>[\\d+,.]*) (?<currency>\\w{3}+) *").assign(taxProcessor).section("gross", // 
    "currency").optional().match("Bruttoertrag: (?<gross>[\\d+,.-]*) (?<currency>\\w{3}+) *").assign((t, v) -> {
        long gross = asAmount(v.get("gross"));
        String currency = asCurrencyCode(v.get("currency"));
        if (currency.equals(t.getSecurity().getCurrencyCode())) {
            String exchangeRateString = type.getCurrentContext().get("exchangeRate");
            if (exchangeRateString != null) {
                BigDecimal exchangeRate = BigDecimal.ONE.divide(asExchangeRate(exchangeRateString), 10, BigDecimal.ROUND_HALF_UP);
                long convertedGross = BigDecimal.valueOf(gross).multiply(exchangeRate).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
                t.addUnit(new Unit(Unit.Type.GROSS_VALUE, Money.of(t.getCurrencyCode(), convertedGross), Money.of(currency, gross), exchangeRate));
            }
        }
    }).wrap(TransactionItem::new));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) Matcher(java.util.regex.Matcher) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Pattern(java.util.regex.Pattern) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) BigDecimal(java.math.BigDecimal) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) Map(java.util.Map)

Aggregations

BiConsumer (java.util.function.BiConsumer)255 Test (org.junit.Test)110 List (java.util.List)106 Map (java.util.Map)77 IOException (java.io.IOException)75 Consumer (java.util.function.Consumer)69 ArrayList (java.util.ArrayList)68 HashMap (java.util.HashMap)64 Collectors (java.util.stream.Collectors)53 CountDownLatch (java.util.concurrent.CountDownLatch)52 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)50 Collections (java.util.Collections)46 Set (java.util.Set)46 Collection (java.util.Collection)45 Arrays (java.util.Arrays)44 TimeUnit (java.util.concurrent.TimeUnit)43 Assert (org.junit.Assert)43 Function (java.util.function.Function)41 Optional (java.util.Optional)40 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)35