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

Completed solution #4

Open
wants to merge 56 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
8fe7434
Implement LinkedQueue.java
tboychuk Jul 31, 2018
f5a4dde
Fix logic to follow FIFO rule
tboychuk Aug 1, 2018
1478575
Merge branch 'master' into exercise/completed
tboychuk Aug 1, 2018
79f73f3
Merge branch 'master' into exercise/completed
tboychuk Aug 1, 2018
3b46dca
Merge branch 'master' into exercise/completed
tboychuk Aug 1, 2018
e0fdefe
Implement exercise concurrent-linked-queue-simple
tboychuk Aug 1, 2018
a206bc0
Merge branch 'master' into exercise/completed
tboychuk Aug 1, 2018
6195fcb
Merge branch 'master' into exercise/completed
tboychuk Aug 1, 2018
755139b
Merge branch 'master' into exercise/completed
tboychuk Aug 2, 2018
ed6ab0e
Simplify class Node.java as a pure data structure, and hide it
tboychuk Aug 7, 2018
f2836e2
Merge branch 'master' into exercise/completed
tboychuk Aug 7, 2018
210e2f6
Complete exercise
tboychuk Aug 7, 2018
ba95354
Merge branch 'master' into exercise/completed
tboychuk Aug 7, 2018
d7007bf
Merge branch 'master' into exercise/completed
tboychuk Aug 9, 2018
c7f1d80
Fix method add(int index, T element)
tboychuk Aug 9, 2018
fffd4df
Fix index at method remove()
tboychuk Aug 9, 2018
791acad
Add comment for method findTail()
tboychuk Aug 9, 2018
0f93579
Merge branch 'master' into exercise/completed
tboychuk Aug 13, 2018
75ba68c
Fix LinkedList#set()
tboychuk Aug 13, 2018
1569048
Merge branch 'master' into exercise/completed
tboychuk Aug 14, 2018
8da56dd
Complete FileStats.java exercise
tboychuk Aug 14, 2018
0c82e41
Merge branch 'master' into exercise/completed
tboychuk Aug 14, 2018
0cca6c7
Remove redundant code from LinkedList#set()
tboychuk Aug 14, 2018
6ec5e02
Simplify LinkedList#add(index, element)
tboychuk Aug 14, 2018
4cbe0f5
Merge branch 'master' into exercise/completed
tboychuk Aug 15, 2018
cfc3847
Complete exercises
tboychuk Aug 15, 2018
9105439
Merge branch 'master' into exercise/completed
tboychuk Aug 15, 2018
461f803
Merge branch 'master' into exercise/completed
tboychuk Aug 16, 2018
0d0cd0e
Merge branch 'master' into exercise/completed
tboychuk Aug 29, 2018
8505265
Merge branch 'master' into exercise/completed
tboychuk Oct 23, 2018
89a43cb
Upgrade LinkedList.java implementation
tboychuk Oct 23, 2018
1392087
Merge branch 'master' into exercise/completed
tboychuk Oct 24, 2018
ee86666
Merge branch 'master' into exercise/completed
tboychuk Oct 24, 2018
c9f0244
Merge branch 'master' into exercise/completed
tboychuk Oct 24, 2018
20c1aaf
Merge branch 'master' into exercise/completed
tboychuk Oct 24, 2018
da6aa0b
Upgrade LinkedList.java
tboychuk Oct 25, 2018
9434e3d
Merge branch 'master' into exercise/completed
tboychuk Oct 25, 2018
afc5479
Merge branch 'master' into exercise/completed
tboychuk Oct 25, 2018
4370c02
Update FileReaders.java
tboychuk Oct 26, 2018
c0fa442
Upgrade the implementation of LinkedList.java
tboychuk Oct 27, 2018
36c2348
Upgrade the implementation of LinkedList.java
tboychuk Oct 27, 2018
bb74678
Upgrade the implementation of LinkedList.java
tboychuk Oct 27, 2018
4b82d55
Merge branch 'master' into exercise/completed
tboychuk Oct 30, 2018
7f13124
Merge branch 'master' into exercise/completed
tboychuk Jul 1, 2019
7bba9c0
#6 add java functional features exercises with solutions
Shtramak Jul 1, 2019
96b5af3
Merge pull request #10 from bobocode-projects/ticket/#6-completed
tboychuk Jul 2, 2019
249b5cf
Merge branch 'master' into exercise/completed
tboychuk Jul 2, 2019
92524fa
Merge branch 'master' into exercise/completed
tboychuk Jul 2, 2019
3ee80b1
Implement new methods is CrazyLambdas.java
tboychuk Jul 2, 2019
73beb18
Merge branch 'master' into exercise/completed
tboychuk Jul 26, 2019
b554fdc
Merge branch 'master' into exercise/completed
tboychuk Aug 15, 2019
4068f38
GP-4 Create new exercise CrazyOptionals.java and upgrade Accounts.java
tboychuk Aug 14, 2019
7cdc096
Merge pull request #16 from bobocode-projects/GP-4-CrazyOptionalExerc…
tboychuk Aug 15, 2019
9394eef
Merge branch 'master' into exercise/completed
tboychuk Apr 28, 2020
c9bbe03
Merge branch 'master' into exercise/completed
tboychuk Apr 28, 2020
606a6b5
Merge branch 'master' into exercise/completed
tboychuk Jul 16, 2020
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
46 changes: 28 additions & 18 deletions crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bobocode;

import java.math.BigDecimal;
import java.util.concurrent.ThreadLocalRandom;
import java.util.Map;
import java.util.function.*;

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

/**
Expand All @@ -21,7 +22,7 @@ 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
return String::isEmpty;
}

/**
Expand All @@ -31,7 +32,7 @@ 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
return String::repeat;
}

/**
Expand All @@ -41,7 +42,7 @@ 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
return bigDecimal -> "$" + bigDecimal;
}

/**
Expand All @@ -53,7 +54,7 @@ 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
return str -> str.length() >= min && str.length() < max;
}

/**
Expand All @@ -62,7 +63,7 @@ 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
return () -> ThreadLocalRandom.current().nextInt();
}


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

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

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

/**
Expand All @@ -99,7 +100,7 @@ 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
return Integer::parseInt;
}

/**
Expand All @@ -110,7 +111,7 @@ 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
return () -> a -> n * a;
}

/**
Expand All @@ -119,7 +120,7 @@ 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
return stringFunction -> stringFunction.compose(String::trim);
}

/**
Expand All @@ -130,7 +131,11 @@ 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
return () -> {
Thread thread = new Thread(runnable);
thread.start();
return thread;
};
}

/**
Expand All @@ -139,7 +144,7 @@ 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
return runnable -> new Thread(runnable).start();
}

/**
Expand All @@ -149,7 +154,11 @@ 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
return runnable -> () -> {
Thread thread = new Thread(runnable);
thread.start();
return thread;
};
}

/**
Expand All @@ -162,7 +171,7 @@ 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
return (intOperation, intPredicate) -> a -> intPredicate.test(a) ? intOperation.applyAsInt(a) : a;
}

/**
Expand All @@ -173,7 +182,7 @@ 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
return (functionMap, functionName) -> functionMap.getOrDefault(functionName, IntUnaryOperator.identity());
}

/**
Expand All @@ -182,7 +191,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
return () -> () -> () -> "WELL DONE!";
}
}


66 changes: 51 additions & 15 deletions crazy-optionals/src/main/java/com/bobocode/CrazyOptionals.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.Optional;
import java.util.OptionalDouble;

import static java.util.Comparator.comparing;

public class CrazyOptionals {

/**
Expand All @@ -24,7 +26,7 @@ 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");
return Optional.ofNullable(text); // Optional.ofNullable() will use Optional.empty() if text is null
}

/**
Expand All @@ -34,7 +36,10 @@ 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");
accountProvider.getAccount()
.ifPresent(account -> account.setBalance(account.getBalance().add(amount))); // instead of using if operator
// you can pass Consumer object that will be used in case Optional is not empty
// this approach is called declarative and is usually more precise
}

/**
Expand All @@ -44,7 +49,7 @@ 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");
return Optional.of(account); // Optional.of() will throw NullPointerException if account is null
}

/**
Expand All @@ -56,7 +61,8 @@ 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");
return accountProvider.getAccount()
.orElse(defaultAccount); // Optional#orElse() can be used to provide default value is case Optional is empty
}

/**
Expand All @@ -67,7 +73,10 @@ 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");
accountProvider.getAccount()
.ifPresentOrElse(accountService::processAccount, accountService::processWithNoAccount);
// one more declarative substitution of if-else operator.
// Please note its parameters: Consumer and Runnable
}

/**
Expand All @@ -78,7 +87,12 @@ 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");
return accountProvider.getAccount()
.orElseGet(Accounts::generateAccount); // functionally it works exactly the same as Optional#orElse()
// however it is based on lazy initialization using Supplier interface, which means that default value
// will not be computed (created) until Supplier#get() is called, which means it will be only computed
// when Optional is empty. This method should be used in favor of Optional#orElse() when the creation of default
// value requires additional resources
}

/**
Expand All @@ -88,7 +102,8 @@ 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");
return accountProvider.getAccount()
.map(Account::getBalance); // a null-safe mapping that allows you to go from Optional object to its field
}

/**
Expand All @@ -99,7 +114,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");
return accountProvider.getAccount()
.orElseThrow(() -> new AccountNotFoundException("No Account provided!")); // in case Optional is empty
// it allows to throw a custom exception
}

/**
Expand All @@ -109,7 +126,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");
return accountProvider.getAccount()
.flatMap(CreditAccount::getCreditBalance); // in case your getter already return Optional, you cannot use
// Optional#map() because it will create Optional<Optional<Account>>. In this case Optional#flatMap() should be used
}


Expand All @@ -121,7 +140,10 @@ 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");
return accountProvider.getAccount()
.filter(account -> account.getEmail().split("@")[1].equals("gmail.com"));
// in case you need to check if an Optional Account meets some criteria and return it or if it does not
// then return Optional.empty() and do that in a null-safe manner
}

/**
Expand All @@ -134,7 +156,9 @@ 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");
return accountProvider.getAccount()
.or(fallbackProvider::getAccount) // allows to use another Optional in case main Optional is empty
.orElseThrow(); // if both providers return Optional.empty() it throws NoSuchElementException
}

/**
Expand All @@ -145,7 +169,9 @@ 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");
return accounts.stream()
.max(comparing(Account::getBalance)) // as you probably know Stream#min() and Stream#max() return Optional
.orElseThrow();
}

/**
Expand All @@ -155,7 +181,10 @@ 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");
return accounts.stream()
.map(Account::getBalance) // map all stream accounts to balances
.mapToDouble(BigDecimal::doubleValue) // map all balances to primitive double values (returns DoubleStream)
.min(); // Optional API provides special classes for primitives as well
}

/**
Expand All @@ -165,7 +194,10 @@ 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");
accounts.stream()
.max(comparing(Account::getBalance)) // returns Optional account
.ifPresent(accountService::processAccount); // declarative if statement and processing
// the last method requires Consumer<Account> as argument, it is implements using method reference
}

/**
Expand All @@ -175,7 +207,11 @@ 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");
return accounts.stream()
.map(CreditAccount::getCreditBalance) // transforms each element of stream into Optional<BigDecimal>
.flatMap(Optional::stream) // uses special Optional#stream() to filter all elements that are empty
.mapToDouble(BigDecimal::doubleValue) // transform BigDecimal into primitive double (returns DoubleStream)
.sum(); // calculates a sum of primitive double
}
}

Loading