This repository was archived by the owner on Feb 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathCrazyOptionals.java
More file actions
221 lines (201 loc) · 7.64 KB
/
CrazyOptionals.java
File metadata and controls
221 lines (201 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.bobocode;
import com.bobocode.data.Accounts;
import com.bobocode.exception.AccountNotFoundException;
import com.bobocode.function.AccountProvider;
import com.bobocode.function.AccountService;
import com.bobocode.function.CreditAccountProvider;
import com.bobocode.model.Account;
import com.bobocode.model.CreditAccount;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.util.*;
public class CrazyOptionals {
/**
* Creates an instance of {@link Optional<String>} using a text parameter
*
* @param text
* @return optional object that holds text
*/
public static Optional<String> optionalOfString(@Nullable String text) {
// todo: done
return Optional.ofNullable(text);
}
/**
* Adds a provided amount of money to the balance of an optional account.
*
* @param accountProvider
* @param amount money to deposit
*/
public static void deposit(AccountProvider accountProvider, BigDecimal amount) {
// todo: done
accountProvider.getAccount().
ifPresent(account -> account.setBalance(account.getBalance().add(amount)));
}
/**
* Creates an instance of {@link Optional<Account>} using an account parameter
*
* @param account
* @return optional object that holds account
*/
public static Optional<Account> optionalOfAccount(@Nonnull Account account) {
// todo: done
return Optional.of(account);
}
/**
* Returns the {@link Account} got from {@link AccountProvider}. If account provider does not provide an account,
* returns a defaultAccount
*
* @param accountProvider
* @param defaultAccount
* @return account from provider or defaultAccount
*/
public static Account getAccount(AccountProvider accountProvider, Account defaultAccount) {
// todo: done
return accountProvider.getAccount()
.orElse(defaultAccount);
}
/**
* Passes account to {@link AccountService#processAccount(Account)} when account is provided.
* Otherwise calls {@link AccountService#processWithNoAccount()}
*
* @param accountProvider
* @param accountService
*/
public static void processAccount(AccountProvider accountProvider, AccountService accountService) {
// todo: done
accountProvider.getAccount()
.ifPresentOrElse(accountService::processAccount, accountService::processWithNoAccount);
}
/**
* Returns account provided by {@link AccountProvider}. If no account is provided it generates one using {@link Accounts}
* Please note that additional account should not be generated if {@link AccountProvider} returned one.
*
* @param accountProvider
* @return provided or generated account
*/
public static Account getOrGenerateAccount(AccountProvider accountProvider) {
// todo: done
// return accountProvider.getAccount().orElse(Accounts.generateAccount());
return accountProvider.getAccount()
.orElseGet(Accounts::generateAccount);
}
/**
* Retrieves a {@link BigDecimal} balance using null-safe mappings.
*
* @param accountProvider
* @return optional balance
*/
public static Optional<BigDecimal> retrieveBalance(AccountProvider accountProvider) {
// todo: done
return accountProvider.getAccount()
.map(Account::getBalance);
}
/**
* Returns an {@link Account} provided by {@link AccountProvider}. If no account is provided,
* throws {@link AccountNotFoundException} with a message "No Account provided!"
*
* @param accountProvider
* @return provided account
*/
public static Account getAccount(AccountProvider accountProvider) {
// todo: done
return accountProvider.getAccount()
.orElseThrow(() -> new AccountNotFoundException("No Account provided!"));
}
/**
* Retrieves a {@link BigDecimal} credit balance using null-safe mappings.
*
* @param accountProvider
* @return optional credit balance
*/
public static Optional<BigDecimal> retrieveCreditBalance(CreditAccountProvider accountProvider) {
// todo: done
return accountProvider.getAccount()
.flatMap(CreditAccount::getCreditBalance);
}
/**
* Retrieves an {@link Account} with gmail email using {@link AccountProvider}. If no account is provided or it gets
* not gmail then returns {@link Optional#empty()}
*
* @param accountProvider
* @return optional gmail account
*/
public static Optional<Account> retrieveAccountGmail(AccountProvider accountProvider) {
// todo: done
return accountProvider.getAccount()
.filter(account -> account.getEmail().split("@")[1].equals("gmail.com"))
// .or(Optional::empty)
;
}
/**
* Retrieves account using {@link AccountProvider} and fallbackProvider. In case main provider does not provide an
* {@link Account} then account should ge retrieved from fallbackProvider. In case both provider returns no account
* then {@link java.util.NoSuchElementException} should be thrown.
*
* @param accountProvider
* @param fallbackProvider
* @return account got from either accountProvider or fallbackProvider
*/
public static Account getAccountWithFallback(AccountProvider accountProvider, AccountProvider fallbackProvider) {
// todo: done
return accountProvider.getAccount()
.or(fallbackProvider::getAccount)
.orElseThrow();
}
/**
* Returns an {@link Accounts} with the highest balance. Throws {@link java.util.NoSuchElementException} if input
* list is empty
*
* @param accounts
* @return account with the highest balance
*/
public static Account getAccountWithMaxBalance(List<Account> accounts) {
// todo: done
return accounts.stream()
.max(Comparator.comparing(Account::getBalance))
.orElseThrow(NoSuchElementException::new);
}
/**
* Returns the lowest balance values or empty if accounts list is empty
*
* @param accounts
* @return the lowest balance values
*/
public static OptionalDouble findMinBalanceValue(List<Account> accounts) {
// todo: done
return accounts.stream()
.map(Account::getBalance)
.mapToDouble(BigDecimal::doubleValue)
.min();
}
/**
* Finds an {@link Account} with max balance and processes it using {@link AccountService#processAccount(Account)}
*
* @param accounts
* @param accountService
*/
public static void processAccountWithMaxBalance(List<Account> accounts, AccountService accountService) {
// todo: done
// accountService.processAccount(accounts.stream()
// .max(Comparator.comparing(Account::getBalance))
// .get());
accounts.stream()
.max(Comparator.comparing(Account::getBalance))
.ifPresent(accountService::processAccount);
}
/**
* Calculates a sum of {@link CreditAccount#getCreditBalance()} of all accounts
*
* @param accounts
* @return total credit balance
*/
public static double calculateTotalCreditBalance(List<CreditAccount> accounts) {
// todo: done
return accounts.stream()
.map(CreditAccount::getCreditBalance)
.flatMap(Optional::stream)
.mapToDouble(BigDecimal::doubleValue)
.sum();
}
}