-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
functional programming for java 8 (#7)
Co-authored-by: Qiqiang Guan <qiqguan@paypal.com>
- Loading branch information
1 parent
6029388
commit 66f76f8
Showing
25 changed files
with
704 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Jave Basic Tech | ||
1. update to java8 | ||
2. some basic tec in java8 | ||
3. add lambda expression | ||
3. add lambda expression | ||
4. test uploading. |
21 changes: 21 additions & 0 deletions
21
basic-java8-core/src/main/java/edu/gqq/algorithm/Fibonacci.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package edu.gqq.algorithm; | ||
|
||
public class Fibonacci { | ||
|
||
public static void main(String[] args) { | ||
// output the result of Fibonacci. 1 -> 1, 2 -> 1, 3 -> 2, 4->3, 5->5, 6->8, 7-> 13 ... | ||
int n = 10; | ||
int fibo = getFiboByN(n); | ||
System.out.println(String.format("fibo(%d) is %d", n, fibo)); | ||
} | ||
|
||
private static int getFiboByN(int n) { | ||
int x = 1, y = 1; | ||
for (int i = 3; i <= n; i++) { | ||
int temp = x + y; | ||
x = y; | ||
y = temp; | ||
} | ||
return y; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
basic-java8-core/src/main/java/edu/gqq/algorithm/ReverseArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package edu.gqq.algorithm; | ||
|
||
public class ReverseArray { | ||
|
||
public static void main(String[] args) { | ||
reverse(); | ||
reverse2(); | ||
} | ||
|
||
private static void reverse2() { | ||
int[] arr = {3, 5, 23, 343, 2342, 2, 1}; | ||
int len = arr.length; | ||
for (int i = 0; i < len / 2; i++) { | ||
int temp = arr[i]; | ||
arr[i] = arr[len - 1 - i]; | ||
arr[len - 1 - i] = temp; | ||
} | ||
System.out.print("{"); | ||
for (int ele : arr) { | ||
System.out.print(ele + ","); | ||
} | ||
System.out.println("}"); | ||
} | ||
|
||
private static void reverse() { | ||
int[] arr = {3, 5, 23, 343, 2342, 2, 1}; | ||
for (int i = 0, j = arr.length - 1; i < j; i++, j--) { | ||
int temp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = temp; | ||
} | ||
System.out.print("{"); | ||
for (int ele : arr) { | ||
System.out.print(ele + ","); | ||
} | ||
System.out.println("}"); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
basic-java8-core/src/main/java/edu/gqq/funcinterface/ConsumerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package edu.gqq.funcinterface; | ||
|
||
import java.math.BigInteger; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Stream; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ConsumerTest { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ConsumerTest.class); | ||
|
||
public static void main(String[] args) { | ||
// how to use consumer string | ||
Consumer<String> printConsumer = str -> System.out.println(str); | ||
printConsumer.accept("test str");//test str | ||
|
||
Stream<String> cities = Stream.of("Sydney", "Dhaka", "New York", "London"); | ||
// why param in foreach is Consumer<? super T> action? | ||
// because in the foreach method, it must call action.accept(T type). | ||
// so, the Consumer action must be the superclass of T. | ||
cities.forEach(printConsumer); | ||
|
||
// Consumer is lambda expression. You can imagine that's a method. | ||
// this method can be called with params | ||
Consumer<Integer> integerConsumer = integer -> { | ||
System.out.println(integer + 3); | ||
}; | ||
integerConsumer.accept(5);//8 | ||
|
||
// 3. test getConsumer | ||
// 3.1 Integer | ||
Consumer<Integer> consumer = getConsumer("string1", 3); | ||
consumer.accept(5); // 5string13 | ||
// 3.2 String | ||
getConsumer("second", "third").accept("first"); | ||
// 3.3 object | ||
getConsumer(new Object(), new Object()).accept(new Object()); | ||
|
||
// 4. test super | ||
// why param in exec is Consumer<? super T> action? | ||
// because in the exec method, it must call action.accept(T type). | ||
// so, the Consumer action must be the superclass of T. | ||
// If this Consumer is <? super T>, that means it can accept T type as its parameter. | ||
// 4.1 FileExec String | ||
Consumer<Object> printBookInfo = _identifier -> { | ||
if (_identifier instanceof String) { | ||
System.out.println("ISBN is " + _identifier); | ||
} else if (_identifier instanceof BigInteger) { | ||
System.out.println("book id is " + ((BigInteger) _identifier).intValue()); | ||
} else { | ||
System.out.println("UNKNOWN type"); | ||
} | ||
}; | ||
|
||
System.out.println("---------------------- 4.1 FileExec String ----------------------"); | ||
FileExec<String> bookIsbn = new FileExec<>(); | ||
FileExec<BigInteger> bookId = new FileExec<>(); | ||
bookIsbn._identifier = "BSK2342034234"; | ||
bookIsbn.exec("Super hero", printBookInfo); | ||
|
||
System.out.println("---------------------- 4.2 FileExec BigInteger ----------------------"); | ||
bookId._identifier = BigInteger.valueOf(134397934L); | ||
bookId.exec("Find the world", printBookInfo); | ||
|
||
// 5. test andThen | ||
testAndThen(); | ||
} | ||
|
||
private static void testAndThen() { | ||
System.out.println("----------------------test andThen----------------------"); | ||
|
||
List<String> cities = Arrays.asList("Sydney", "Dhaka", "New York", "London"); | ||
Consumer<List<String>> upperCaseConsumer = list -> { | ||
// please find a stream way to change a list into Upper Case. | ||
// List<String> collect = list.stream().map(s -> s.toUpperCase()).collect(Collectors.toList()); | ||
// System.out.println(list); | ||
// System.out.println(collect); | ||
list.replaceAll(x -> x.toUpperCase()); | ||
|
||
// for (int i = 0; i < list.size(); i++) { | ||
// list.set(i, list.get(i).toUpperCase()); | ||
// } | ||
}; | ||
Consumer<List<String>> printConsumer = list -> list.stream().forEach(System.out::println); | ||
upperCaseConsumer.andThen(printConsumer).accept(cities); | ||
} | ||
|
||
public static <T> Consumer<T> getConsumer(Object obj, T val) { | ||
// T tObj = (T) obj; | ||
return t -> { | ||
System.out.printf("%s_%s_%s%n", t.toString(), obj.toString(), val.toString()); | ||
}; | ||
} | ||
} | ||
|
||
class FileExec<T> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(FileExec.class); | ||
T _identifier; | ||
|
||
public void exec(String fileName, Consumer<? super T> action) { | ||
logger.info("fileName is " + fileName); | ||
action.accept(_identifier); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/ActionBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package edu.gqq.funcinterface.actiontest; | ||
|
||
import java.math.BigInteger; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class ActionBase { | ||
|
||
private String actionName; | ||
private int actionID; | ||
|
||
public ActionBase(int actId, String name) { | ||
this.actionID = actId; | ||
this.actionName = name; | ||
} | ||
|
||
public String getActionName() { | ||
return actionName; | ||
} | ||
|
||
public void setActionName(String actionName) { | ||
this.actionName = actionName; | ||
} | ||
|
||
public int getActionID() { | ||
return actionID; | ||
} | ||
|
||
public void setActionID(int actionID) { | ||
this.actionID = actionID; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ActionBase{" + | ||
"actionName='" + actionName + '\'' + | ||
", actionID=" + actionID + | ||
'}'; | ||
} | ||
} | ||
|
||
class BlockAction extends ActionBase { | ||
|
||
private final String blockReason; | ||
|
||
public BlockAction(int actId, String name, String reason) { | ||
super(actId, name); | ||
this.blockReason = StringUtils.isBlank(reason) ? "UNKNOWN" : reason; | ||
} | ||
|
||
public String getBlockReason() { | ||
return blockReason; | ||
} | ||
} | ||
|
||
class SendSMSAction extends ActionBase { | ||
|
||
private final String smsMsg; | ||
private final BigInteger accountNumber; | ||
|
||
public String getSmsMsg() { | ||
return smsMsg; | ||
} | ||
|
||
public BigInteger getAccountNumber() { | ||
return accountNumber; | ||
} | ||
|
||
public SendSMSAction(int actId, String name, String accNo, String smsMsg) { | ||
super(actId, name); | ||
this.smsMsg = smsMsg; | ||
this.accountNumber = new BigInteger(accNo); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/BasicActionProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package edu.gqq.funcinterface.actiontest; | ||
|
||
|
||
import edu.gqq.funcinterface.actiontest.Decision.DecisionStatus; | ||
import java.security.cert.CertPathValidatorException.BasicReason; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import org.apache.commons.collections4.MapUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class BasicActionProcessor<T extends ActionBase> implements IActionProcessor<T> { | ||
|
||
private static Logger logger = LoggerFactory.getLogger(BasicActionProcessor.class); | ||
|
||
@Override | ||
public Consumer<Decision> getProcessActionConsumer(Map<String, ActionBase> map, T action) { | ||
return (Decision x) -> { | ||
if (x == null || action == null || MapUtils.isEmpty(map)) { | ||
logger.debug("decision, action or map is null"); | ||
return; | ||
} | ||
|
||
if (DecisionStatus.Disabled.equals(x.getStatus())) { | ||
logger.debug("decision status is disabled"); | ||
return; | ||
} | ||
|
||
logger.debug(map.toString()); | ||
logger.debug(action.toString()); | ||
logger.debug(x.toString()); | ||
}; | ||
} | ||
|
||
public static void main(String[] args) { | ||
BasicActionProcessor<ActionBase> basicAction = new BasicActionProcessor<>(); | ||
ActionBase action1 = new ActionBase(101, "basicAction1"); | ||
ActionBase action2 = new ActionBase(102, "basicAction2"); | ||
Map<String, ActionBase> map = new HashMap<>(); | ||
map.put("101", action1); | ||
map.put("102", action2); | ||
|
||
basicAction.getProcessActionConsumer(null, action1). | ||
accept(new Decision(DecisionStatus.Disabled, BasicReason.EXPIRED)); | ||
basicAction.getProcessActionConsumer(map, null). | ||
accept(new Decision(DecisionStatus.Disabled, BasicReason.EXPIRED)); | ||
basicAction.getProcessActionConsumer(map, null). | ||
accept(null); | ||
basicAction.getProcessActionConsumer(map, action1). | ||
accept(new Decision(DecisionStatus.Disabled, BasicReason.EXPIRED)); | ||
basicAction.getProcessActionConsumer(map, action1). | ||
accept(new Decision(DecisionStatus.Enabled, BasicReason.EXPIRED)); | ||
} | ||
} |
29 changes: 19 additions & 10 deletions
29
...rc/main/java/edu/gqq/stream/Decision.java → ...qq/funcinterface/actiontest/Decision.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,42 @@ | ||
package edu.gqq.stream; | ||
package edu.gqq.funcinterface.actiontest; | ||
|
||
import java.security.cert.CertPathValidatorException; | ||
|
||
public class Decision { | ||
|
||
public String getStatus() { | ||
return status; | ||
public Decision(DecisionStatus status, CertPathValidatorException.Reason reason) { | ||
this.status = status; | ||
this.reason = reason; | ||
} | ||
|
||
public void setStatus(String status) { | ||
this.status = status; | ||
public DecisionStatus getStatus() { | ||
return status; | ||
} | ||
|
||
public CertPathValidatorException.Reason getReason() { | ||
return reason; | ||
} | ||
|
||
public void setReason(CertPathValidatorException.Reason reason) { | ||
this.reason = reason; | ||
} | ||
|
||
/** | ||
* 3DS decision status code | ||
*/ | ||
private String status = null; | ||
private DecisionStatus status = null; | ||
|
||
/** | ||
* Reason of the risk evaluation by issuer for inroamtional purposes | ||
*/ | ||
|
||
private CertPathValidatorException.Reason reason = null; | ||
|
||
@Override | ||
public String toString() { | ||
return "Decision{" + | ||
"status=" + status.toString() + | ||
", reason=" + reason.toString() + | ||
'}'; | ||
} | ||
|
||
public enum DecisionStatus { | ||
Enabled, Disabled | ||
} | ||
} |
Oops, something went wrong.