Skip to content
This repository has been archived by the owner on Feb 10, 2021. It is now read-only.

fix package in test #32

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 46 additions & 18 deletions crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.util.Map;
import java.util.Random;
import java.util.function.*;

public class CrazyLambdas {
Expand All @@ -12,7 +13,8 @@ public class CrazyLambdas {
* @return a string supplier
*/
public static Supplier<String> helloSupplier() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return () -> "Hello";
}

/**
Expand All @@ -21,7 +23,8 @@ public static Supplier<String> helloSupplier() {
* @return a string predicate
*/
public static Predicate<String> isEmptyPredicate() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return String::isEmpty;
}

/**
Expand All @@ -31,7 +34,8 @@ public static Predicate<String> isEmptyPredicate() {
* @return function that repeats Strings
*/
public static BiFunction<String, Integer, String> stringMultiplier() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return String::repeat;
}

/**
Expand All @@ -41,7 +45,8 @@ public static BiFunction<String, Integer, String> stringMultiplier() {
* @return function that converts adds dollar sign
*/
public static Function<BigDecimal, String> toDollarStringFunction() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return bigDecimal -> "$" + bigDecimal.toString();
}

/**
Expand All @@ -53,7 +58,8 @@ public static Function<BigDecimal, String> toDollarStringFunction() {
* @return a string predicate
*/
public static Predicate<String> lengthInRangePredicate(int min, int max) {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return s -> s.length() >= min && s.length() < max;
}

/**
Expand All @@ -62,7 +68,8 @@ public static Predicate<String> lengthInRangePredicate(int min, int max) {
* @return int supplier
*/
public static IntSupplier randomIntSupplier() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return () -> new Random().nextInt();
}


Expand All @@ -72,7 +79,8 @@ public static IntSupplier randomIntSupplier() {
* @return int operation
*/
public static IntUnaryOperator boundedRandomIntSupplier() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return operand -> new Random(operand).nextInt();
}

/**
Expand All @@ -81,7 +89,8 @@ public static IntUnaryOperator boundedRandomIntSupplier() {
* @return square operation
*/
public static IntUnaryOperator intSquareOperation() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return operand -> operand * operand;
}

/**
Expand All @@ -90,7 +99,8 @@ public static IntUnaryOperator intSquareOperation() {
* @return binary sum operation
*/
public static LongBinaryOperator longSumOperation() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return Long::sum;
}

/**
Expand All @@ -99,7 +109,8 @@ public static LongBinaryOperator longSumOperation() {
* @return string to int converter
*/
public static ToIntFunction<String> stringToIntConverter() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return Integer::parseInt;
}

/**
Expand All @@ -110,7 +121,8 @@ public static ToIntFunction<String> stringToIntConverter() {
* @return a function supplier
*/
public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return () -> operand -> operand * n;
}

/**
Expand All @@ -119,7 +131,8 @@ public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
* @return function that composes functions with trim() function
*/
public static UnaryOperator<Function<String, String>> composeWithTrimFunction() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return function -> function.compose(String::trim);
}

/**
Expand All @@ -130,7 +143,12 @@ public static UnaryOperator<Function<String, String>> composeWithTrimFunction()
* @return a thread supplier
*/
public static Supplier<Thread> runningThreadSupplier(Runnable runnable) {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return () -> {
Thread thread = new Thread(runnable);
thread.start();
return thread;
};
}

/**
Expand All @@ -139,7 +157,8 @@ public static Supplier<Thread> runningThreadSupplier(Runnable runnable) {
* @return a runnable consumer
*/
public static Consumer<Runnable> newThreadRunnableConsumer() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return Runnable::run;
}

/**
Expand All @@ -149,7 +168,12 @@ public static Consumer<Runnable> newThreadRunnableConsumer() {
* @return a function that transforms runnable into a thread supplier
*/
public static Function<Runnable, Supplier<Thread>> runnableToThreadSupplierFunction() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return function -> () -> {
Thread thread = new Thread(function);
thread.start();
return thread;
};
}

/**
Expand All @@ -162,7 +186,9 @@ public static Function<Runnable, Supplier<Thread>> runnableToThreadSupplierFunct
* @return a binary function that receiver predicate and function and compose them to create a new function
*/
public static BiFunction<IntUnaryOperator, IntPredicate, IntUnaryOperator> functionToConditionalFunction() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return (intUnaryOperator, intPredicate) ->
intUnaryOperator.compose(i -> intPredicate.test(i) ? i : intUnaryOperator.applyAsInt(i));
}

/**
Expand All @@ -173,7 +199,8 @@ public static BiFunction<IntUnaryOperator, IntPredicate, IntUnaryOperator> funct
* @return a high-order function that fetches a function from a function map by a given name or returns identity()
*/
public static BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator> functionLoader() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return (map, s) -> map.getOrDefault(s, IntUnaryOperator.identity());
}

/**
Expand All @@ -182,7 +209,8 @@ public static BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator
* @return a supplier instance
*/
public static Supplier<Supplier<Supplier<String>>> trickyWellDoneSupplier() {
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
// todo: done
return () -> () -> () -> "WELL DONE!";
}
}

76 changes: 58 additions & 18 deletions crazy-optionals/src/main/java/com/bobocode/CrazyOptionals.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.*;

public class CrazyOptionals {

Expand All @@ -24,7 +22,8 @@ public class CrazyOptionals {
* @return optional object that holds text
*/
public static Optional<String> optionalOfString(@Nullable String text) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
return Optional.ofNullable(text);
}

/**
Expand All @@ -34,7 +33,9 @@ public static Optional<String> optionalOfString(@Nullable String text) {
* @param amount money to deposit
*/
public static void deposit(AccountProvider accountProvider, BigDecimal amount) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
accountProvider.getAccount().
ifPresent(account -> account.setBalance(account.getBalance().add(amount)));
}

/**
Expand All @@ -44,7 +45,8 @@ public static void deposit(AccountProvider accountProvider, BigDecimal amount) {
* @return optional object that holds account
*/
public static Optional<Account> optionalOfAccount(@Nonnull Account account) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
return Optional.of(account);
}

/**
Expand All @@ -56,7 +58,9 @@ public static Optional<Account> optionalOfAccount(@Nonnull Account account) {
* @return account from provider or defaultAccount
*/
public static Account getAccount(AccountProvider accountProvider, Account defaultAccount) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
return accountProvider.getAccount()
.orElse(defaultAccount);
}

/**
Expand All @@ -67,7 +71,9 @@ public static Account getAccount(AccountProvider accountProvider, Account defaul
* @param accountService
*/
public static void processAccount(AccountProvider accountProvider, AccountService accountService) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
accountProvider.getAccount()
.ifPresentOrElse(accountService::processAccount, accountService::processWithNoAccount);
}

/**
Expand All @@ -78,7 +84,10 @@ public static void processAccount(AccountProvider accountProvider, AccountServic
* @return provided or generated account
*/
public static Account getOrGenerateAccount(AccountProvider accountProvider) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
// return accountProvider.getAccount().orElse(Accounts.generateAccount());
return accountProvider.getAccount()
.orElseGet(Accounts::generateAccount);
}

/**
Expand All @@ -88,7 +97,9 @@ public static Account getOrGenerateAccount(AccountProvider accountProvider) {
* @return optional balance
*/
public static Optional<BigDecimal> retrieveBalance(AccountProvider accountProvider) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
return accountProvider.getAccount()
.map(Account::getBalance);
}

/**
Expand All @@ -99,7 +110,9 @@ public static Optional<BigDecimal> retrieveBalance(AccountProvider accountProvid
* @return provided account
*/
public static Account getAccount(AccountProvider accountProvider) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
return accountProvider.getAccount()
.orElseThrow(() -> new AccountNotFoundException("No Account provided!"));
}

/**
Expand All @@ -109,7 +122,9 @@ public static Account getAccount(AccountProvider accountProvider) {
* @return optional credit balance
*/
public static Optional<BigDecimal> retrieveCreditBalance(CreditAccountProvider accountProvider) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
return accountProvider.getAccount()
.flatMap(CreditAccount::getCreditBalance);
}


Expand All @@ -121,7 +136,11 @@ public static Optional<BigDecimal> retrieveCreditBalance(CreditAccountProvider a
* @return optional gmail account
*/
public static Optional<Account> retrieveAccountGmail(AccountProvider accountProvider) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
return accountProvider.getAccount()
.filter(account -> account.getEmail().split("@")[1].equals("gmail.com"))
// .or(Optional::empty)
;
}

/**
Expand All @@ -134,7 +153,10 @@ public static Optional<Account> retrieveAccountGmail(AccountProvider accountProv
* @return account got from either accountProvider or fallbackProvider
*/
public static Account getAccountWithFallback(AccountProvider accountProvider, AccountProvider fallbackProvider) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
return accountProvider.getAccount()
.or(fallbackProvider::getAccount)
.orElseThrow();
}

/**
Expand All @@ -145,7 +167,10 @@ public static Account getAccountWithFallback(AccountProvider accountProvider, Ac
* @return account with the highest balance
*/
public static Account getAccountWithMaxBalance(List<Account> accounts) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
return accounts.stream()
.max(Comparator.comparing(Account::getBalance))
.orElseThrow(NoSuchElementException::new);
}

/**
Expand All @@ -155,7 +180,11 @@ public static Account getAccountWithMaxBalance(List<Account> accounts) {
* @return the lowest balance values
*/
public static OptionalDouble findMinBalanceValue(List<Account> accounts) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
return accounts.stream()
.map(Account::getBalance)
.mapToDouble(BigDecimal::doubleValue)
.min();
}

/**
Expand All @@ -165,7 +194,13 @@ public static OptionalDouble findMinBalanceValue(List<Account> accounts) {
* @param accountService
*/
public static void processAccountWithMaxBalance(List<Account> accounts, AccountService accountService) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
// accountService.processAccount(accounts.stream()
// .max(Comparator.comparing(Account::getBalance))
// .get());
accounts.stream()
.max(Comparator.comparing(Account::getBalance))
.ifPresent(accountService::processAccount);
}

/**
Expand All @@ -175,7 +210,12 @@ public static void processAccountWithMaxBalance(List<Account> accounts, AccountS
* @return total credit balance
*/
public static double calculateTotalCreditBalance(List<CreditAccount> accounts) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
// todo: done
return accounts.stream()
.map(CreditAccount::getCreditBalance)
.flatMap(Optional::stream)
.mapToDouble(BigDecimal::doubleValue)
.sum();
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com;
package com.bobocode;


import com.bobocode.CrazyOptionals;
Expand Down
Loading