returnedSha512;
16 |
17 | private final MessageDigest md;
18 |
19 | static {
20 | Sha512 Sha512 = new Sha512();
21 | returnedSha512 = Sha512.md == null ?
22 | null : new Returned.ReturnValue<>(Sha512);
23 | }
24 |
25 | private Sha512() {
26 | MessageDigest md;
27 | try {
28 | md = MessageDigest.getInstance("SHA-512");
29 | } catch (NoSuchAlgorithmException e) {
30 | md = null;
31 | }
32 | this.md = md;
33 | }
34 |
35 | @Override
36 | public byte[] digest(byte[] message) {
37 | return md.digest(message);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/text/review/songor/Island.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, songor. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.text.review.songor;
7 |
8 | public class Island {
9 | public static void main(String[] args) {
10 | String html = """
11 |
12 |
13 |
14 |
15 | No man is an island,
16 | Entire of itself,
17 | Every man is a piece of the continent,
18 | A part of the main.
19 |
20 |
21 |
22 | """;
23 | System.out.println(html);
24 |
25 | String text = """
26 | No man is an island,
27 | Entire of itself,
28 | Every man is a piece of the continent,
29 | A part of the main.
30 | """;
31 | System.out.println(text);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/flow/reactive/Destination.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.flow.reactive;
7 |
8 | import java.util.concurrent.Flow;
9 | import java.util.function.Consumer;
10 |
11 | public class Destination implements Flow.Subscriber{
12 | private Flow.Subscription subscription;
13 |
14 | private final Consumer consumer;
15 |
16 | public Destination(Consumer consumer) {
17 | this.consumer = consumer;
18 | }
19 |
20 | @Override
21 | public void onSubscribe(Flow.Subscription subscription) {
22 | this.subscription = subscription;
23 | subscription.request(1);
24 | }
25 |
26 | @Override
27 | public void onNext(T item) {
28 | subscription.request(1);
29 | consumer.accept(item);
30 | }
31 |
32 | @Override
33 | public void onError(Throwable throwable) {
34 | throwable.printStackTrace();
35 | }
36 |
37 | @Override
38 | public void onComplete() {
39 | System.out.println("Done");
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/record/explicit/Circle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.record.explicit;
7 |
8 | import java.util.Objects;
9 |
10 | public record Circle(double radius) implements Shape {
11 | public Circle(double radius) {
12 | this.radius = radius;
13 | }
14 |
15 | @Override
16 | public double area() {
17 | return Math.PI * radius * radius;
18 | }
19 |
20 | @Override
21 | public boolean equals(Object o) {
22 | if (this == o) {
23 | return true;
24 | }
25 |
26 | if (o instanceof Circle other) {
27 | return other.radius == this.radius;
28 | }
29 |
30 | return false;
31 | }
32 |
33 | @Override
34 | public int hashCode() {
35 | return Objects.hash(radius);
36 | }
37 |
38 | @Override
39 | public String toString() {
40 | return String.format("Circle[radius=%f]", radius);
41 | }
42 |
43 | @Override
44 | public double radius() {
45 | return this.radius;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/stack/review/xuelei/UseCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.stack.review.xuelei;
7 |
8 | public class UseCase {
9 | public static void main(String[] args) {
10 | Returned rt = Digest.of("SHA-128");
11 | switch (rt) {
12 | case Returned.ReturnValue rv -> {
13 | Digest d = (Digest) rv.returnValue();
14 | d.digest("Hello, world!".getBytes());
15 | }
16 | case Returned.ErrorCode ec -> {
17 | if (ec.errorCode() == -1) {
18 | System.getLogger("co.ivi.jus.stack.union")
19 | .log(System.Logger.Level.INFO,
20 | "Unlikedly to happen");
21 | } else {
22 | System.getLogger("co.ivi.jus.stack.union")
23 | .log(System.Logger.Level.INFO,
24 | "SHA-218 is not supported");
25 | }
26 | }
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/pattern/modern/UpdatedUseCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.pattern.modern;
7 |
8 | import co.ivi.jus.pattern.extension.Shape;
9 |
10 | public class UpdatedUseCase {
11 | public static void main(String[] args) {
12 | System.out.println("Is null a square? " +
13 | UpdatedUseCase.isSquare(null));
14 | System.out.println("Is a circle instance a square? " +
15 | UpdatedUseCase.isSquare(new Shape.Circle(10)));
16 | System.out.println("Is a square instance a square? " +
17 | UpdatedUseCase.isSquare(new Shape.Square(10)));
18 | System.out.println("Is a rectangle instance a square? " +
19 | UpdatedUseCase.isSquare(new Shape.Rectangle(10, 10)));
20 | }
21 |
22 | public static boolean isSquare(Shape shape) {
23 | return switch (shape) {
24 | case null, Shape.Circle c -> false;
25 | case Shape.Square s -> true;
26 | case Shape.Rectangle r -> r.length() == r.width();
27 | };
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/error/union/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.error.union;
7 |
8 | import co.ivi.jus.error.union.Returned.ReturnValue;
9 | import co.ivi.jus.error.union.Returned.ErrorCode;
10 |
11 | public sealed abstract class Digest {
12 | private static final class SHA256 extends Digest {
13 | @Override
14 | public byte[] digest(byte[] message) {
15 | // snipped
16 | return new byte[0];
17 | }
18 | }
19 |
20 | private static final class SHA512 extends Digest {
21 | @Override
22 | public byte[] digest(byte[] message) {
23 | // snipped
24 | return new byte[0];
25 | }
26 | }
27 |
28 | public static Returned of(String algorithm) {
29 | return switch (algorithm) {
30 | case "SHA-256" -> new ReturnValue(new SHA256());
31 | case "SHA-512" -> new ReturnValue(new SHA512());
32 | case null, default -> new ErrorCode(-1);
33 | };
34 | }
35 |
36 | public abstract byte[] digest(byte[] message);
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/error/former/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.error.former;
7 |
8 | import java.security.NoSuchAlgorithmException;
9 |
10 | public sealed abstract class Digest {
11 | private static final class SHA256 extends Digest {
12 | @Override
13 | public byte[] digest(byte[] message) {
14 | // snipped
15 | return new byte[0];
16 | }
17 | }
18 |
19 | private static final class SHA512 extends Digest {
20 | @Override
21 | public byte[] digest(byte[] message) {
22 | // snipped
23 | return new byte[0];
24 | }
25 | }
26 |
27 | public static Digest of(String algorithm) throws NoSuchAlgorithmException {
28 | return switch (algorithm) {
29 | case "SHA-256" -> new SHA256();
30 | case "SHA-512" -> new SHA512();
31 | case null, default -> throw new NoSuchAlgorithmException(
32 | "Unsupported digest algorithm " + algorithm);
33 | };
34 | }
35 |
36 | public abstract byte[] digest(byte[] message);
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/stack/former/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.stack.former;
7 |
8 | import java.security.NoSuchAlgorithmException;
9 |
10 | public sealed abstract class Digest {
11 | private static final class SHA256 extends Digest {
12 | @Override
13 | public byte[] digest(byte[] message) {
14 | // snipped
15 | return new byte[0];
16 | }
17 | }
18 |
19 | private static final class SHA512 extends Digest {
20 | @Override
21 | public byte[] digest(byte[] message) {
22 | // snipped
23 | return new byte[0];
24 | }
25 | }
26 |
27 | public static Digest of(String algorithm) throws NoSuchAlgorithmException {
28 | return switch (algorithm) {
29 | case "SHA-256" -> new SHA256();
30 | case "SHA-512" -> new SHA512();
31 | case null, default -> throw new NoSuchAlgorithmException(
32 | "Unsupported digest algorithm " + algorithm);
33 | };
34 | }
35 |
36 | public abstract byte[] digest(byte[] message);
37 | }
38 |
--------------------------------------------------------------------------------
/src/test/java/co/ivi/jus/sealed/ModernUseCases.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.sealed;
7 |
8 | import co.ivi.jus.sealed.modern.Circle;
9 | import co.ivi.jus.sealed.modern.Rectangle;
10 | import co.ivi.jus.sealed.modern.Shape;
11 | import co.ivi.jus.sealed.modern.Square;
12 |
13 | public class ModernUseCases {
14 | public static void main(String[] args) {
15 | System.out.println("Is it a circle? " +
16 | ModernUseCases.isSquare(null));
17 | System.out.println("Is it a square? " +
18 | ModernUseCases.isSquare(null));
19 | System.out.println("Is it a square? " +
20 | ModernUseCases.isSquare(new Square("circle", 3)));
21 | }
22 |
23 | public static boolean isCircle(Shape shape) {
24 | return (shape instanceof Circle);
25 | }
26 |
27 | public static boolean isSquare(Shape shape) {
28 | return switch (shape) {
29 | case null -> false;
30 | case Square s -> true;
31 | case Rectangle r -> r.length == r.width;
32 | default -> false;
33 | };
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/flow/reactive/Transform.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.flow.reactive;
7 |
8 | import java.util.concurrent.Flow;
9 | import java.util.concurrent.SubmissionPublisher;
10 | import java.util.function.Function;
11 |
12 | public class Transform extends SubmissionPublisher
13 | implements Flow.Processor {
14 |
15 | private Function transform;
16 | private Flow.Subscription subscription;
17 |
18 | public Transform(Function transform) {
19 | super();
20 | this.transform = transform;
21 | }
22 |
23 | @Override
24 | public void onSubscribe(Flow.Subscription subscription) {
25 | this.subscription = subscription;
26 | subscription.request(1);
27 | }
28 |
29 | @Override
30 | public void onNext(T item) {
31 | submit(transform.apply(item));
32 | subscription.request(1);
33 | }
34 |
35 | @Override
36 | public void onError(Throwable throwable) {
37 | closeExceptionally(throwable);
38 | }
39 |
40 | @Override
41 | public void onComplete() {
42 | close();
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/record/review/songor/SocialSecurityNumber.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, songor. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.record.review.songor;
7 |
8 | import java.nio.charset.StandardCharsets;
9 | import java.security.MessageDigest;
10 | import java.util.Arrays;
11 |
12 | record SocialSecurityNumber(byte[] ssn) {
13 | public SocialSecurityNumber {
14 | if (ssn == null || ssn.length <= 0) {
15 | throw new IllegalArgumentException("The ssn should not be empty");
16 | }
17 | }
18 |
19 | public String show() {
20 | String str = new String(ssn, StandardCharsets.UTF_8);
21 | String regex = String.format("(?<=\\w{%d})\\w(?=\\w{%d})", 6, 4);
22 | return str.replaceAll(regex, "*");
23 | }
24 |
25 | @Override
26 | public boolean equals(Object o) {
27 | if (this == o) {
28 | return true;
29 | }
30 | if (!(o instanceof SocialSecurityNumber other)) {
31 | return false;
32 | }
33 | return MessageDigest.isEqual(ssn, other.ssn);
34 | }
35 |
36 | @Override
37 | public int hashCode() {
38 | return Arrays.hashCode(ssn);
39 | }
40 | }
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/pattern/review/xuling/UseCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Ling Xu. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.pattern.review.xuling;
7 |
8 | import co.ivi.jus.pattern.extension.Shape;
9 |
10 | /**
11 | * pattern match homework
12 | *
13 | * @author jack
14 | * @since 2021/11/30
15 | **/
16 | public class UseCase {
17 |
18 | public static void main(String[] args) {
19 | System.out.println("Is null a square? " + isSquare(null));
20 | System.out.println("Is a circle instance a square? " + isSquare(new Shape.Circle(10)));
21 | System.out.println("Is a square instance a square? " + isSquare(new Shape.Square(10)));
22 | System.out.println("Is a rectangle(10, 10) instance a square? " + isSquare(new Shape.Rectangle(10, 10)));
23 | System.out.println("Is a rectangle(10, 5) instance a square? " + isSquare(new Shape.Rectangle(10, 5)));
24 | }
25 |
26 | public static boolean isSquare(Shape shape) {
27 | return switch (shape) {
28 | case null, Shape.Circle c -> false;
29 | case Shape.Square s -> true;
30 | case Shape.Rectangle r -> r.length() == r.width();
31 | };
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/text/modern/TextBlocks.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.text.modern;
7 |
8 | public class TextBlocks {
9 | public static void main(String[] args) {
10 | String stringBlock =
11 | "\n" +
12 | "\n" +
13 | " \n" +
14 | " \"Hello World!\"
\n" +
15 | " \n" +
16 | "\n";
17 | System.out.println("Here is the regular string:\n" + stringBlock);
18 |
19 | String textBlock = """
20 |
21 |
22 |
23 | "Hello World!"
24 |
25 |
26 | """;
27 | System.out.println("Here is the text block:\n" + textBlock);
28 |
29 | System.out.println("Does the text block equal to the regular string? " +
30 | stringBlock.equals(textBlock));
31 | System.out.println("Does the text block refer to the regular string? " +
32 | (stringBlock == textBlock));
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/pattern/review/xuelei/UseCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.pattern.review.xuelei;
7 |
8 | import co.ivi.jus.pattern.extension.Shape;
9 |
10 | public class UseCase {
11 | public static void main(String[] args) {
12 | System.out.println("Is null a square? " +
13 | UseCase.isSquare(null));
14 | System.out.println("Is a circle instance a square? " +
15 | UseCase.isSquare(new Shape.Circle(10)));
16 | System.out.println("Is a square instance a square? " +
17 | UseCase.isSquare(new Shape.Square(10)));
18 | System.out.println("Is a special rectangle instance a square? " +
19 | UseCase.isSquare(new Shape.Rectangle(10, 10)));
20 | System.out.println("Is a rectangle instance a square? " +
21 | UseCase.isSquare(new Shape.Rectangle(10, 20)));
22 | }
23 |
24 | public static boolean isSquare(Shape shape) {
25 | return switch (shape) {
26 | case null, Shape.Circle c -> false;
27 | case Shape.Square s -> true;
28 | case Shape.Rectangle r && r.length() == r.width() -> true;
29 | case Shape.Rectangle r -> false;
30 | };
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/swexpr/review/songor/DaysInMonth.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, songor. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.swexpr.review.songor;
7 |
8 | import java.time.Month;
9 | import java.util.Calendar;
10 |
11 | class DaysInMonth {
12 | public static void main(String[] args) {
13 | Calendar today = Calendar.getInstance();
14 | Month month = Month.of(today.get(Calendar.MONTH) + 1);
15 | int year = today.get(Calendar.YEAR);
16 |
17 | int daysInMonth = switch (month) {
18 | case JANUARY,
19 | MARCH,
20 | MAY,
21 | JULY,
22 | AUGUST,
23 | OCTOBER,
24 | DECEMBER -> 31;
25 | case APRIL,
26 | JUNE,
27 | SEPTEMBER,
28 | NOVEMBER -> 30;
29 | case FEBRUARY -> {
30 | if (((year % 4 == 0) && !(year % 100 == 0))
31 | || (year % 400 == 0)) {
32 | yield 29;
33 | } else {
34 | yield 28;
35 | }
36 | }
37 | };
38 |
39 | System.out.println("There are " + daysInMonth + " days in this month.");
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/jus.crypto.use/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 | java-up
12 | co.ivi
13 | 01
14 |
15 | 4.0.0
16 |
17 | jus.crypto.use
18 |
19 |
20 | co.ivi
21 | jus.crypto
22 | 01
23 | compile
24 |
25 |
26 | co.ivi
27 | jus.crypto.impl
28 | 01
29 | compile
30 |
31 |
32 |
33 |
34 | 17
35 | 17
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/nullp/returned/UseCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.nullp.returned;
7 |
8 | public class UseCase {
9 | public static void main(String[] args) {
10 | FullName jack = new FullName("Jack", "", "Brown");
11 | FullName rose = new FullName("Rose", null, "Brown");
12 | FullName nova = new FullName("Nova", "Diane", "Brown");
13 | System.out.println(
14 | "Is Jack's middle name Diane? " +
15 | hasMiddleName(jack, "Diane"));
16 | System.out.println(
17 | "Is Rose's middle name Diane? " +
18 | hasMiddleName(rose, "Diane"));
19 | System.out.println(
20 | "Is Nova's middle name Diane? " +
21 | hasMiddleName(nova, "Diane"));
22 | }
23 |
24 | private static boolean hasMiddleName(FullName fullName, String middleName) {
25 | return switch (fullName.middleName()) {
26 | case Returned.Undefined undefined -> false;
27 | case Returned.ReturnValue rv -> {
28 | String returnedMiddleName = (String)rv.returnValue();
29 | yield returnedMiddleName.equals(middleName);
30 | }
31 | };
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/instance/modern/Shadow.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.instance.modern;
7 |
8 | import co.ivi.jus.instance.modern.Shape.Rectangle;
9 | import co.ivi.jus.instance.modern.Shape.Circle;
10 |
11 | public final class Shadow {
12 | private static final Rectangle rect = null;
13 |
14 | public static void main(String[] args) {
15 | Shape shape = new Rectangle(10, 10);
16 | System.out.println("It should be ture that " + shape +
17 | " is a square: " + isSquare(shape));
18 |
19 | System.out.println();
20 |
21 | shape = new Circle(10);
22 | System.out.println("It cannot be ture that " + shape +
23 | " is a square: " + isSquare(shape));
24 | }
25 |
26 | public static boolean isSquare(Shape shape) {
27 | if (shape instanceof Rectangle rect) {
28 | // Field rect is shadowed, local rect is in scope
29 | System.out.println("This should be the local rect: " + rect);
30 | return rect.length() == rect.width();
31 | }
32 |
33 | // Field rect is in scope, local rect is not in scope here
34 | System.out.println("This should be the field rect: " + rect);
35 | return shape instanceof Shape.Square;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/instance/modern/Shadowed.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.instance.modern;
7 |
8 | import co.ivi.jus.instance.modern.Shape.Circle;
9 | import co.ivi.jus.instance.modern.Shape.Rectangle;
10 |
11 | public final class Shadowed {
12 | private static final Rectangle rect = null;
13 |
14 | public static void main(String[] args) {
15 | Shape shape = new Rectangle(10, 10);
16 | System.out.println("It should be ture that " + shape +
17 | " is a square: " + isSquare(shape));
18 |
19 | System.out.println();
20 |
21 | shape = new Circle(10);
22 | System.out.println("It cannot be ture that " + shape +
23 | " is a square: " + isSquare(shape));
24 | }
25 |
26 | public static boolean isSquare(Shape shape) {
27 | if (!(shape instanceof Rectangle rect)) {
28 | // Field rect is in scope, local rect is not in scope here
29 | System.out.println("This should be the field rect: " + rect);
30 | return shape instanceof Shape.Square;
31 | }
32 |
33 | // Field rect is shadowed, local rect is in scope
34 | System.out.println("This should be the local rect: " + rect);
35 | return rect.length() == rect.width();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/nullp/former/UseCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.nullp.former;
7 |
8 | public class UseCase {
9 | public static void main(String[] args) {
10 | FullName jack = new FullName("Jack", "", "Brown");
11 | FullName rose = new FullName("Rose", null, "Brown");
12 | FullName nova = new FullName("Nova", "Diane", "Brown");
13 | System.out.println(
14 | "Is Jack's middle name Diane? " +
15 | hasMiddleName(jack, "Diane"));
16 | System.out.println(
17 | "Is Rose's middle name Diane? " +
18 | hasMiddleName(rose, "Diane"));
19 | System.out.println(
20 | "Is Nova's middle name Diane? " +
21 | hasMiddleName(nova, "Diane"));
22 | }
23 |
24 | private static boolean hasMiddleNameImplA(
25 | FullName fullName, String middleName) {
26 | if (fullName.middleName() != null) {
27 | return fullName.middleName().equals(middleName);
28 | }
29 |
30 | return middleName == null;
31 | }
32 |
33 | // private static boolean hasMiddleNameImplB(
34 | private static boolean hasMiddleName(
35 | FullName fullName, String middleName) {
36 | return fullName.middleName().equals(middleName);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/stack/review/calvinit/UseCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.stack.review.calvinit;
7 |
8 | public class UseCase {
9 | public static void main(String[] args) {
10 | Returned rt = Digest.of("SHA-128");
11 | switch (rt) {
12 | case Returned.ReturnValue rv -> {
13 | Digest d = (Digest) rv.returnValue();
14 | d.digest("Hello, world!".getBytes());
15 | }
16 | case Returned.ErrorCode ec -> {
17 | var errorCode = switch (ec) {
18 | case NULL_ILLEGAL -> {
19 | System.getLogger("co.ivi.jus.stack.union")
20 | .log(System.Logger.Level.INFO, "Unlikely to happen");
21 | yield ec.errorCode;
22 | }
23 | case NOT_SUPPORTED -> {
24 | System.getLogger("co.ivi.jus.stack.union")
25 | .log(System.Logger.Level.INFO, "SHA-218 is not supported");
26 | yield ec.errorCode;
27 | }
28 | };
29 | System.getLogger("co.ivi.jus.stack.union")
30 | .log(System.Logger.Level.INFO, "errorCode is " + errorCode);
31 | }
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/swexpr/legacy/DaysInMonth.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.swexpr.legacy;
7 |
8 | import java.util.Calendar;
9 |
10 | class DaysInMonth {
11 | public static void main(String[] args) {
12 | Calendar today = Calendar.getInstance();
13 | int month = today.get(Calendar.MONTH);
14 | int year = today.get(Calendar.YEAR);
15 |
16 | int daysInMonth = switch (month) {
17 | case Calendar.JANUARY:
18 | case Calendar.MARCH:
19 | case Calendar.MAY:
20 | case Calendar.JULY:
21 | case Calendar.AUGUST:
22 | case Calendar.OCTOBER:
23 | case Calendar.DECEMBER:
24 | yield 31;
25 | case Calendar.APRIL:
26 | case Calendar.JUNE:
27 | case Calendar.SEPTEMBER:
28 | case Calendar.NOVEMBER:
29 | yield 30;
30 | case Calendar.FEBRUARY:
31 | if (((year % 4 == 0) && !(year % 100 == 0))
32 | || (year % 400 == 0)) {
33 | yield 29;
34 | } else {
35 | yield 28;
36 | }
37 | default:
38 | throw new RuntimeException(
39 | "Calendar in JDK does not work");
40 | };
41 |
42 | System.out.println("There are " + daysInMonth + " days in this month.");
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/stack/former/UseCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.stack.former;
7 |
8 | import java.security.NoSuchAlgorithmException;
9 |
10 | public class UseCase {
11 | public static void main(String[] args) {
12 | String[] algorithms = {"SHA-128", "SHA-192"};
13 |
14 | String availableAlgorithm = null;
15 | for (String algorithm : algorithms) {
16 | Digest md;
17 | try {
18 | md = Digest.of(algorithm);
19 | } catch (NoSuchAlgorithmException ex) {
20 | // ignore, continue to use the next algorithm.
21 | continue;
22 | }
23 |
24 | try {
25 | md.digest("Hello, world!".getBytes());
26 | } catch (Exception ex) {
27 | System.getLogger("co.ivi.jus.stack.former")
28 | .log(System.Logger.Level.WARNING,
29 | algorithm + " does not work",
30 | ex);
31 | continue;
32 | }
33 |
34 | availableAlgorithm = algorithm;
35 | }
36 |
37 | if (availableAlgorithm != null) {
38 | System.out.println(availableAlgorithm + " is available");
39 | } else {
40 | throw new RuntimeException("No available hash algorithm");
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/swexpr/modern/DaysInMonth.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.swexpr.modern;
7 |
8 | import java.util.Calendar;
9 |
10 | class DaysInMonth {
11 | public static void main(String[] args) {
12 | Calendar today = Calendar.getInstance();
13 | int month = today.get(Calendar.MONTH);
14 | int year = today.get(Calendar.YEAR);
15 |
16 | int daysInMonth = switch (month) {
17 | case Calendar.JANUARY,
18 | Calendar.MARCH,
19 | Calendar.MAY,
20 | Calendar.JULY,
21 | Calendar.AUGUST,
22 | Calendar.OCTOBER,
23 | Calendar.DECEMBER -> 31;
24 | case Calendar.APRIL,
25 | Calendar.JUNE,
26 | Calendar.SEPTEMBER,
27 | Calendar.NOVEMBER -> 30;
28 | case Calendar.FEBRUARY -> {
29 | if (((year % 4 == 0) && !(year % 100 == 0))
30 | || (year % 400 == 0)) {
31 | yield 29;
32 | } else {
33 | yield 28;
34 | }
35 | }
36 | default -> throw new RuntimeException(
37 | "Calendar in JDK does not work");
38 | };
39 |
40 | System.out.println("There are " + daysInMonth + " days in this month.");
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
24 |
25 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/retire/review/xuelei/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.retire.review.xuelei;
7 |
8 | public abstract sealed class Shape {
9 | public final String id;
10 |
11 | public Shape(String id) {
12 | this.id = id;
13 | }
14 |
15 | public abstract double area();
16 |
17 | public static final class Circle extends Shape {
18 | public final double radius;
19 |
20 | public Circle(String id, double radius) {
21 | super(id);
22 | this.radius = radius;
23 | }
24 |
25 | @Override
26 | public double area() {
27 | return Math.PI * radius * radius;
28 | }
29 | }
30 |
31 | public static final class Square extends Shape {
32 | public final double side;
33 |
34 | public Square(String id, double side) {
35 | super(id);
36 | this.side = side;
37 | }
38 |
39 | @Override
40 | public double area() {
41 | return side * side;
42 | }
43 | }
44 |
45 | // Here is your code for Rectangle.
46 |
47 | // Here is the test for circle.
48 | public static boolean isCircle(Shape shape) {
49 | // Here goes your update.
50 | return (shape instanceof Circle);
51 | }
52 |
53 | // Here is the code to run your test.
54 | public static void main(String[] args) {
55 | // Here is your code.
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/sealed/review/xuelei/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.sealed.review.xuelei;
7 |
8 | public abstract sealed class Shape {
9 | public final String id;
10 |
11 | public Shape(String id) {
12 | this.id = id;
13 | }
14 |
15 | public abstract double area();
16 |
17 | public static final class Circle extends Shape {
18 | public final double radius;
19 |
20 | public Circle(String id, double radius) {
21 | super(id);
22 | this.radius = radius;
23 | }
24 |
25 | @Override
26 | public double area() {
27 | return Math.PI * radius * radius;
28 | }
29 | }
30 |
31 | public static final class Square extends Shape {
32 | public final double side;
33 |
34 | public Square(String id, double side) {
35 | super(id);
36 | this.side = side;
37 | }
38 |
39 | @Override
40 | public double area() {
41 | return side * side;
42 | }
43 | }
44 |
45 | // Here is your code for Rectangle.
46 |
47 | // Here is the test for circle.
48 | public static boolean isCircle(Shape shape) {
49 | // Here goes your update.
50 | return (shape instanceof Circle);
51 | }
52 |
53 | // Here is the code to run your test.
54 | public static void main(String[] args) {
55 | // Here is your code.
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/flow/former/ThroughputBench.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.flow.former;
7 |
8 | import co.ivi.jus.error.former.Digest;
9 | import org.openjdk.jmh.annotations.*;
10 | import org.openjdk.jmh.runner.Runner;
11 | import org.openjdk.jmh.runner.RunnerException;
12 | import org.openjdk.jmh.runner.options.Options;
13 | import org.openjdk.jmh.runner.options.OptionsBuilder;
14 |
15 | import java.security.NoSuchAlgorithmException;
16 | import java.util.concurrent.TimeUnit;
17 |
18 | @BenchmarkMode(Mode.Throughput)
19 | @OutputTimeUnit(TimeUnit.SECONDS)
20 | @State(Scope.Thread)
21 | @Fork(3)
22 | @Warmup(iterations = 5)
23 | @Measurement(iterations = 5)
24 | public class ThroughputBench {
25 | private static Digest messageDigest;
26 |
27 | static {
28 | try {
29 | messageDigest = Digest.of("SHA-256");
30 | } catch (NoSuchAlgorithmException e) {
31 | // unlikely.
32 | }
33 | }
34 |
35 | public static void main(String[] args) throws RunnerException {
36 | Options options = new OptionsBuilder()
37 | .include(ThroughputBench.class.getSimpleName())
38 | .forks(3)
39 | .warmupIterations(5)
40 | .measurementIterations(5)
41 | .build();
42 |
43 | new Runner(options).run();
44 | }
45 |
46 | @Benchmark
47 | public void simpleDigest() {
48 | messageDigest.digest("Hello, world!".getBytes());
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/swexpr/former/DaysInMonth.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.swexpr.former;
7 |
8 | import java.util.Calendar;
9 |
10 | class DaysInMonth {
11 | public static void main(String[] args) {
12 | Calendar today = Calendar.getInstance();
13 | int month = today.get(Calendar.MONTH);
14 | int year = today.get(Calendar.YEAR);
15 |
16 | int daysInMonth = 0;
17 | switch (month) {
18 | case Calendar.JANUARY:
19 | case Calendar.MARCH:
20 | case Calendar.MAY:
21 | case Calendar.JULY:
22 | case Calendar.AUGUST:
23 | case Calendar.OCTOBER:
24 | case Calendar.DECEMBER:
25 | daysInMonth = 31;
26 | break;
27 | case Calendar.APRIL:
28 | case Calendar.JUNE:
29 | case Calendar.SEPTEMBER:
30 | case Calendar.NOVEMBER:
31 | daysInMonth = 30;
32 | break;
33 | case Calendar.FEBRUARY:
34 | if (((year % 4 == 0) && !(year % 100 == 0))
35 | || (year % 400 == 0)) {
36 | daysInMonth = 29;
37 | } else {
38 | daysInMonth = 28;
39 | }
40 | break;
41 | default:
42 | throw new RuntimeException("Calendar in JDK does not work");
43 | }
44 |
45 | System.out.println("There are " + daysInMonth + " days in this month.");
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/swexpr/breakout/DaysInMonth.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.swexpr.breakout;
7 |
8 | import java.util.Calendar;
9 |
10 | class DaysInMonth {
11 | public static void main(String[] args) {
12 | System.out.println("There are " +
13 | daysInMonth(Integer.parseInt(args[0]), Integer.parseInt(args[1])) +
14 | " days");
15 | }
16 |
17 | private static int daysInMonth(int year, int month) {
18 | int daysInMonth = 0;
19 | switch (month) {
20 | case Calendar.JANUARY,
21 | Calendar.MARCH,
22 | Calendar.MAY,
23 | Calendar.JULY,
24 | Calendar.AUGUST,
25 | Calendar.OCTOBER,
26 | Calendar.DECEMBER ->
27 | daysInMonth = 31;
28 | case Calendar.APRIL,
29 | Calendar.JUNE,
30 | Calendar.SEPTEMBER,
31 | Calendar.NOVEMBER ->
32 | daysInMonth = 30;
33 | case Calendar.FEBRUARY -> {
34 | if (((year % 4 == 0) && !(year % 100 == 0))
35 | || (year % 400 == 0)) {
36 | daysInMonth = 29;
37 | break;
38 | }
39 |
40 | daysInMonth = 28;
41 | }
42 | // default -> throw new RuntimeException(
43 | // "Calendar in JDK does not work");
44 | }
45 |
46 | return daysInMonth;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/sealed/improved/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.sealed.improved;
7 |
8 | public abstract sealed class Shape {
9 | // permits Shape.Circle, Shape.Rectangle, Shape.Square {
10 | public final String id;
11 |
12 | public Shape(String id) {
13 | this.id = id;
14 | }
15 |
16 | public abstract double area();
17 |
18 | public static final class Circle extends Shape {
19 | public final double radius;
20 |
21 | public Circle(String id, double radius) {
22 | super(id);
23 | this.radius = radius;
24 | }
25 |
26 | @Override
27 | public double area() {
28 | return Math.PI * radius * radius;
29 | }
30 | }
31 |
32 | public static final class Rectangle extends Shape {
33 | public final double length;
34 | public final double width;
35 |
36 | public Rectangle(String id, double length, double width) {
37 | super(id);
38 | this.length = length;
39 | this.width = width;
40 | }
41 |
42 | @Override
43 | public double area() {
44 | return length * width;
45 | }
46 | }
47 |
48 | public static final class Square extends Shape {
49 | public final double side;
50 |
51 | public Square(String id, double side) {
52 | super(id);
53 | this.side = side;
54 | }
55 |
56 | @Override
57 | public double area() {
58 | return side * side;
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/instance/review/xuelei/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.instance.review.xuelei;
7 |
8 | public sealed interface Shape
9 | permits Shape.Circle, Shape.Rectangle, Shape.Square {
10 | Shape.Rectangle rect = null; // field variable
11 |
12 | record Circle(double radius) implements Shape {
13 | // blank
14 | }
15 |
16 | record Square(double side) implements Shape {
17 | // blank
18 | }
19 |
20 | record Rectangle(double length, double width) implements Shape {
21 | // blank
22 | }
23 |
24 | static void main(String[] args) {
25 | Shape shape = new Shape.Rectangle(10, 10);
26 | System.out.println("It should be ture that " + shape +
27 | " is a square: " + isSquare(shape));
28 |
29 | System.out.println();
30 |
31 | shape = new Shape.Circle(10);
32 | System.out.println("It cannot be ture that " + shape +
33 | " is a square: " + (!isSquare(shape)));
34 | }
35 |
36 | static boolean isSquare(Shape shape) {
37 | if (shape instanceof Rectangle rect) {
38 | // Field rect is shadowed, local rect is in scope
39 | System.out.println(
40 | "This should be the local rect: " + rect.equals(shape));
41 | return (rect.length == rect.width);
42 | }
43 |
44 | // Field rect is in scope, local rect is not in scope here
45 | System.out.println(
46 | "This should be the field rect: " + (rect == null));
47 | return (shape instanceof Square);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/swexpr/review/xuelei/DaysInMonth.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.swexpr.review.xuelei;
7 |
8 | import java.util.Calendar;
9 |
10 | class DaysInMonth {
11 | public static void main(String[] args) {
12 | Calendar today = Calendar.getInstance();
13 | int month = today.get(Calendar.MONTH);
14 | int year = today.get(Calendar.YEAR);
15 |
16 | // Hints: could we replace the integer month
17 | // with an exhaustive enumeration?
18 | int daysInMonth = switch (month) {
19 | case Calendar.JANUARY,
20 | Calendar.MARCH,
21 | Calendar.MAY,
22 | Calendar.JULY,
23 | Calendar.AUGUST,
24 | Calendar.OCTOBER,
25 | Calendar.DECEMBER -> 31;
26 | case Calendar.APRIL,
27 | Calendar.JUNE,
28 | Calendar.SEPTEMBER,
29 | Calendar.NOVEMBER -> 30;
30 | case Calendar.FEBRUARY -> {
31 | if (((year % 4 == 0) && !(year % 100 == 0))
32 | || (year % 400 == 0)) {
33 | yield 29;
34 | } else {
35 | yield 28;
36 | }
37 | }
38 | // Hints: Are we able to replace the default case by
39 | // enumerating all cases with case clause above?
40 | default -> throw new RuntimeException(
41 | "Calendar in JDK does not work");
42 | };
43 |
44 | System.out.println("There are " + daysInMonth + " days in this month.");
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/flow/callback/ThroughputBench.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.flow.callback;
7 |
8 | import org.openjdk.jmh.annotations.*;
9 | import org.openjdk.jmh.runner.Runner;
10 | import org.openjdk.jmh.runner.RunnerException;
11 | import org.openjdk.jmh.runner.options.Options;
12 | import org.openjdk.jmh.runner.options.OptionsBuilder;
13 |
14 | import java.util.concurrent.TimeUnit;
15 |
16 | @BenchmarkMode(Mode.Throughput)
17 | @OutputTimeUnit(TimeUnit.SECONDS)
18 | @State(Scope.Thread)
19 | @Fork(3)
20 | @Warmup(iterations = 5)
21 | @Measurement(iterations = 5)
22 | public class ThroughputBench {
23 | private static Digest messageDigest;
24 |
25 | static {
26 | Digest.of("SHA-256",
27 | md -> messageDigest = md,
28 | errorCode -> {
29 | // blank, unlikely
30 | }
31 | );
32 | }
33 |
34 | public static void main(String[] args) throws RunnerException {
35 | Options options = new OptionsBuilder()
36 | .include(ThroughputBench.class.getSimpleName())
37 | .forks(3)
38 | .warmupIterations(5)
39 | .measurementIterations(5)
40 | .build();
41 |
42 | new Runner(options).run();
43 | }
44 |
45 | @Benchmark
46 | public void simpleDigest() {
47 | messageDigest.digest("Hello, world!".getBytes(),
48 | values -> {
49 | // blank, no use
50 | },
51 | errorCode -> {
52 | // blank, unlikely
53 | });
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/vector/former/LinearEquation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.vector.former;
7 |
8 | import co.ivi.jus.flow.reactive.Returned;
9 |
10 | public class LinearEquation {
11 | private static final float[] a = new float[] {0.6F, 0.7F, 0.8F, 0.9F};
12 | private static final float[] x = new float[] {1.0F, 2.0F, 3.0F, 4.0F};
13 |
14 | private static Returned sumInScalar(float[] a, float[] x) {
15 | if (a == null || x == null || a.length != x.length) {
16 | return new Returned.ErrorCode(-1);
17 | }
18 |
19 | float[] y = new float[a.length];
20 | for (int i = 0; i < a.length; i++) {
21 | y[i] = a[i] * x[i];
22 | }
23 |
24 | float r = 0F;
25 | for (int i = 0; i < y.length; i++) {
26 | r += y[i];
27 | }
28 |
29 | return new Returned.ReturnValue<>(r);
30 | }
31 |
32 | public static void main(String[] args) {
33 | Returned rt = LinearEquation.sumInScalar(a, x);
34 | switch (rt) {
35 | case Returned.ReturnValue rv -> {
36 | // Get the returned value
37 | if (rv.returnValue() instanceof Float result) {
38 | System.out.println("The summary is: " + result);
39 | } else { // unlikely
40 | System.out.println(
41 | "Implementation error: LinearEquation.sumInScalar");
42 | }
43 | }
44 | case Returned.ErrorCode ec ->
45 | System.out.println("Invalid input parameters");
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/stack/union/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.stack.union;
7 |
8 | public sealed abstract class Digest {
9 | private static final class SHA256 extends Digest {
10 | @Override
11 | public byte[] digest(byte[] message) {
12 | // snipped
13 | return new byte[0];
14 | }
15 | }
16 |
17 | private static final class SHA512 extends Digest {
18 | @Override
19 | public byte[] digest(byte[] message) {
20 | // snipped
21 | return new byte[0];
22 | }
23 | }
24 |
25 | public static Returned of(String algorithm) {
26 | return switch (algorithm) {
27 | case "SHA-256" -> new Returned.ReturnValue(new SHA256());
28 | case "SHA-512" -> new Returned.ReturnValue(new SHA512());
29 | case null -> {
30 | System.getLogger("co.ivi.jus.stack.union")
31 | .log(System.Logger.Level.WARNING,
32 | "No algorithm is specified",
33 | new Throwable("the calling stack"));
34 | yield new Returned.ErrorCode(-1);
35 | }
36 | default -> {
37 | System.getLogger("co.ivi.jus.stack.union")
38 | .log(System.Logger.Level.INFO,
39 | "Unknown algorithm is specified " + algorithm,
40 | new Throwable("the calling stack"));
41 | yield new Returned.ErrorCode(-1);
42 | }
43 | };
44 | }
45 |
46 | public abstract byte[] digest(byte[] message);
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/stack/review/xuelei/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.stack.review.xuelei;
7 |
8 | public sealed abstract class Digest {
9 | private static final class SHA256 extends Digest {
10 | @Override
11 | public byte[] digest(byte[] message) {
12 | // snipped
13 | return new byte[0];
14 | }
15 | }
16 |
17 | private static final class SHA512 extends Digest {
18 | @Override
19 | public byte[] digest(byte[] message) {
20 | // snipped
21 | return new byte[0];
22 | }
23 | }
24 |
25 | public static Returned of(String algorithm) {
26 | return switch (algorithm) {
27 | case "SHA-256" -> new Returned.ReturnValue(new SHA256());
28 | case "SHA-512" -> new Returned.ReturnValue(new SHA512());
29 | case null -> {
30 | System.getLogger("co.ivi.jus.stack.union")
31 | .log(System.Logger.Level.WARNING,
32 | "No algorithm is specified",
33 | new Throwable("the calling stack"));
34 | yield new Returned.ErrorCode(-1);
35 | }
36 | default -> {
37 | System.getLogger("co.ivi.jus.stack.union")
38 | .log(System.Logger.Level.INFO,
39 | "Unknown algorithm is specified " + algorithm,
40 | new Throwable("the calling stack"));
41 | yield new Returned.ErrorCode(-2);
42 | }
43 | };
44 | }
45 |
46 | public abstract byte[] digest(byte[] message);
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/stack/review/calvinit/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.stack.review.calvinit;
7 |
8 | public sealed abstract class Digest {
9 | private static final class SHA256 extends Digest {
10 | @Override
11 | public byte[] digest(byte[] message) {
12 | // snipped
13 | return new byte[0];
14 | }
15 | }
16 |
17 | private static final class SHA512 extends Digest {
18 | @Override
19 | public byte[] digest(byte[] message) {
20 | // snipped
21 | return new byte[0];
22 | }
23 | }
24 |
25 | public static Returned of(String algorithm) {
26 | return switch (algorithm) {
27 | case "SHA-256" -> new Returned.ReturnValue<>(new SHA256());
28 | case "SHA-512" -> new Returned.ReturnValue<>(new SHA512());
29 | case null -> {
30 | System.getLogger("co.ivi.jus.stack.union")
31 | .log(System.Logger.Level.WARNING,
32 | "No algorithm is specified",
33 | new Throwable("the calling stack"));
34 | yield Returned.ErrorCode.NULL_ILLEGAL;
35 | }
36 | default -> {
37 | System.getLogger("co.ivi.jus.stack.union")
38 | .log(System.Logger.Level.INFO,
39 | "Unknown algorithm is specified " + algorithm,
40 | new Throwable("the calling stack"));
41 | yield Returned.ErrorCode.NOT_SUPPORTED;
42 | }
43 | };
44 | }
45 |
46 | public abstract byte[] digest(byte[] message);
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/error/coded/CodedBench.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.error.coded;
7 |
8 | import org.openjdk.jmh.annotations.*;
9 | import org.openjdk.jmh.runner.Runner;
10 | import org.openjdk.jmh.runner.RunnerException;
11 | import org.openjdk.jmh.runner.options.Options;
12 | import org.openjdk.jmh.runner.options.OptionsBuilder;
13 |
14 | import java.util.concurrent.TimeUnit;
15 |
16 | // Benchmark Mode Cnt Score Error Units
17 | // CodedBench.noErrorCode thrpt 15 1294683223.721 ± 52780816.883 ops/s
18 | // CodedBench.withErrorCode thrpt 15 1020513332.336 ± 5557838.495 ops/s
19 |
20 | // Benchmark Mode Cnt Score Error Units
21 | // CodedBench.noErrorCode thrpt 15 1320977784.955 ± 7487395.023 ops/s
22 | // CodedBench.withErrorCode thrpt 15 1068513642.240 ± 69527558.874 ops/s
23 |
24 | @BenchmarkMode(Mode.Throughput)
25 | @OutputTimeUnit(TimeUnit.SECONDS)
26 | @State(Scope.Thread)
27 | @Fork(3)
28 | @Warmup(iterations = 5)
29 | @Measurement(iterations = 5)
30 | public class CodedBench {
31 | public static void main(String[] args) throws RunnerException {
32 | Options options = new OptionsBuilder()
33 | .include(CodedBench.class.getSimpleName())
34 | .forks(3)
35 | .warmupIterations(5)
36 | .measurementIterations(5)
37 | .build();
38 |
39 | new Runner(options).run();
40 | }
41 |
42 | @Benchmark
43 | public void withErrorCode() {
44 | Digest.of("SHA-128");
45 | }
46 |
47 | @Benchmark
48 | public void noErrorCode() {
49 | Digest.of("SHA-256");
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/swexpr/statement/DaysInMonth.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.swexpr.statement;
7 |
8 | import java.util.Calendar;
9 |
10 | class DaysInMonth {
11 | private static int DAYS_IN_JAN = 31;
12 | private static int DAYS_IN_APR = 30;
13 |
14 | public static void main(String[] args) {
15 | Calendar today = Calendar.getInstance();
16 | System.out.println("There are " +
17 | daysInMonth(today.get(Calendar.YEAR),
18 | today.get(Calendar.MONTH)) +
19 | " days in this month.");
20 | }
21 |
22 | private static int daysInMonth(int year, int month) {
23 | switch (month) {
24 | case Calendar.JANUARY,
25 | Calendar.MARCH,
26 | Calendar.MAY,
27 | Calendar.JULY,
28 | Calendar.AUGUST,
29 | Calendar.OCTOBER,
30 | Calendar.DECEMBER -> {
31 | return DAYS_IN_JAN;
32 | }
33 | case Calendar.APRIL,
34 | Calendar.JUNE,
35 | Calendar.SEPTEMBER,
36 | Calendar.NOVEMBER -> {
37 | return DAYS_IN_APR;
38 | }
39 | case Calendar.FEBRUARY -> {
40 | return daysInFec(year);
41 | }
42 | default -> throw new RuntimeException(
43 | "Calendar in JDK does not work");
44 | }
45 | }
46 |
47 | private static int daysInFec(int year) {
48 | if (((year % 4 == 0) && !(year % 100 == 0))
49 | || (year % 400 == 0)) {
50 | return 29;
51 | }
52 |
53 | return 28;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/nullp/optional/FullName.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.nullp.optional;
7 |
8 | import java.util.Objects;
9 | import java.util.Optional;
10 |
11 | public final class FullName {
12 | private final String firstName;
13 | private final String middleName;
14 | private final String lastName;
15 |
16 | public FullName(String firstName, String middleName, String lastName) {
17 | this.firstName = firstName;
18 | this.middleName = middleName;
19 | this.lastName = lastName;
20 | }
21 |
22 | public Optional firstName() {
23 | return Optional.ofNullable(firstName);
24 | }
25 |
26 | public Optional middleName() {
27 | return Optional.ofNullable(middleName);
28 | }
29 |
30 | public Optional lastName() {
31 | return Optional.ofNullable(lastName);
32 | }
33 |
34 | @Override
35 | public boolean equals(Object obj) {
36 | if (obj == this) {
37 | return true;
38 | }
39 |
40 | if (!(obj instanceof FullName that)) {
41 | return false;
42 | }
43 |
44 | return Objects.equals(this.firstName, that.firstName) &&
45 | Objects.equals(this.middleName, that.middleName) &&
46 | Objects.equals(this.lastName, that.lastName);
47 | }
48 |
49 | @Override
50 | public int hashCode() {
51 | return Objects.hash(firstName, middleName, lastName);
52 | }
53 |
54 | @Override
55 | public String toString() {
56 | return "FullName[" +
57 | "firstName=" + firstName + ", " +
58 | "middleName=" + middleName + ", " +
59 | "lastName=" + lastName + ']';
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/text/spaces/TextBlocks.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.text.spaces;
7 |
8 | public class TextBlocks {
9 | public static void main(String[] args) {
10 | String stringBlock =
11 | "\n" +
12 | "\n" +
13 | " \n" +
14 | " \"Hello World!\"
\n" +
15 | " \n" +
16 | "\n";
17 | System.out.println("Here is the regular string:\n" + stringBlock);
18 |
19 | String textBlock = """
20 |
21 |
22 |
23 | "Hello World!"
24 |
25 |
26 | """;
27 | System.out.println("Here is the text block:\n" + textBlock);
28 |
29 | System.out.println("Does the text block equal to the regular string? " +
30 | stringBlock.equals(textBlock));
31 | System.out.println("Does the text block refer to the regular string? " +
32 | (stringBlock == textBlock));
33 |
34 | textBlock = """
35 | \s
36 | \s
37 |
38 | "Hello World!"
39 |
40 |
41 | """;
42 | System.out.println("Here is the text block:\n" + textBlock);
43 | System.out.println("Does the text block equal to the regular string? " +
44 | stringBlock.equals(textBlock));
45 | System.out.println("Does the text block refer to the regular string? " +
46 | (stringBlock == textBlock));
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/text/terminator/TextBlocks.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.text.terminator;
7 |
8 | public class TextBlocks {
9 | public static void main(String[] args) {
10 | String stringBlock =
11 | "\n" +
12 | "\n" +
13 | " \n" +
14 | " \"Hello World!\"
\n" +
15 | " \n" +
16 | "\n";
17 | System.out.println("Here is the regular string:\n" + stringBlock);
18 |
19 | String textBlock = """
20 |
21 |
22 |
23 | "Hello World!"
24 |
25 |
26 | """;
27 | System.out.println("Here is the text block:\n" + textBlock);
28 |
29 | System.out.println("Does the text block equal to the regular string? " +
30 | stringBlock.equals(textBlock));
31 | System.out.println("Does the text block refer to the regular string? " +
32 | (stringBlock == textBlock));
33 |
34 | textBlock = """
35 |
36 |
37 |
38 | "Hello \
39 | World!"
40 |
41 |
42 | """;
43 | System.out.println("Here is the text block:\n" + textBlock);
44 | System.out.println("Does the text block equal to the regular string? " +
45 | stringBlock.equals(textBlock));
46 | System.out.println("Does the text block refer to the regular string? " +
47 | (stringBlock == textBlock));
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/error/former/OutOfBoundsBench.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.error.former;
7 |
8 | import org.openjdk.jmh.annotations.*;
9 | import org.openjdk.jmh.runner.Runner;
10 | import org.openjdk.jmh.runner.RunnerException;
11 | import org.openjdk.jmh.runner.options.Options;
12 | import org.openjdk.jmh.runner.options.OptionsBuilder;
13 |
14 | import java.util.concurrent.TimeUnit;
15 |
16 | // Benchmark Mode Cnt Score Error Units
17 | // OutOfBoundsBench.noException thrpt 15 531217144.697 ± 88633850.663 ops/s
18 | // OutOfBoundsBench.withException thrpt 15 561648.779 ± 28430.340 ops/s
19 |
20 | @BenchmarkMode(Mode.Throughput)
21 | @OutputTimeUnit(TimeUnit.SECONDS)
22 | @State(Scope.Thread)
23 | @Fork(3)
24 | @Warmup(iterations = 5)
25 | @Measurement(iterations = 5)
26 | public class OutOfBoundsBench {
27 | private static String s = "Hello, world!"; // s.length() == 13.
28 |
29 | public static void main(String[] args) throws RunnerException {
30 | Options options = new OptionsBuilder()
31 | .include(OutOfBoundsBench.class.getSimpleName())
32 | .forks(3)
33 | .warmupIterations(5)
34 | .measurementIterations(5)
35 | .build();
36 |
37 | new Runner(options).run();
38 | }
39 |
40 | @Benchmark
41 | public void withException() {
42 | try {
43 | s.substring(14);
44 | } catch (RuntimeException re) {
45 | // blank line, ignore the exception.
46 | }
47 | }
48 |
49 | @Benchmark
50 | public void noException() {
51 | try {
52 | s.substring(13);
53 | } catch (RuntimeException re) {
54 | // blank line, ignore the exception.
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/instance/review/puruidong/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Ruidong Pu. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | public sealed interface Shape
7 | permits Shape.Circle, Shape.Rectangle, Shape.Square {
8 | // field variable
9 | Shape.Rectangle rect = null;
10 |
11 | record Circle(double radius) implements Shape {
12 | // blank
13 | }
14 |
15 | record Square(double side) implements Shape {
16 | // blank
17 | }
18 |
19 | record Rectangle(double length, double width) implements Shape {
20 | // blank
21 | }
22 |
23 | static void main(String[] args) {
24 | Shape shape = new Shape.Rectangle(10, 10);
25 | System.out.println("It should be ture that " + shape +
26 | " is a square: " + isSquare(shape));
27 | System.out.println(isSquareA(shape));
28 | System.out.println(isSquareF(shape));
29 |
30 | System.out.println();
31 |
32 | shape = new Shape.Circle(10);
33 | System.out.println("It cannot be ture that " + shape +
34 | " is a square: " + (!isSquare(shape)));
35 | System.out.println(!isSquareA(shape));
36 | System.out.println(!isSquareF(shape));
37 | }
38 |
39 | static boolean isSquareA(Shape shape) {
40 | if (shape instanceof Rectangle rect) {
41 | return rect.length() == rect.width();
42 | }
43 | return shape instanceof Square;
44 | }
45 |
46 | static boolean isSquareF(Shape shape) {
47 | if (shape instanceof Rectangle rect) {
48 | return rect.length() == rect.width();
49 | }
50 | return shape instanceof Shape.Square;
51 | }
52 |
53 | // nice.
54 | static boolean isSquare(Shape shape) {
55 | return shape instanceof Square || shape instanceof Rectangle rect && rect.length == rect.width;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/swexpr/review/xuling/DaysInMonth.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Ling Xu. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.swexpr.review.xuling;
7 |
8 | import java.time.Instant;
9 | import java.time.LocalDate;
10 | import java.time.Month;
11 | import java.time.temporal.TemporalField;
12 | import java.util.Calendar;
13 |
14 | /**
15 | * homework for 05
16 | *
17 | * @author jack
18 | * @since 2021/11/30
19 | **/
20 | public class DaysInMonth {
21 |
22 | private static boolean isLeapYear(int year) {
23 | return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
24 | }
25 |
26 | public static void main(String[] args) {
27 |
28 | // use the same date lib
29 | LocalDate today = LocalDate.now();
30 | Month month = today.getMonth();
31 |
32 | /**
33 | Calendar today = Calendar.getInstance();
34 | // this will throw exception , is a runtime error
35 | Month month = Month.of(today.get(Calendar.MONTH) + 1);
36 | boolean isLeapYear = isLeapYear(today.get(Calendar.YEAR));
37 | **/
38 |
39 | // Hints: could we replace the integer month
40 | // with an exhaustive enumeration?
41 | int daysInMonth = switch (month) {
42 | case JANUARY,
43 | MARCH,
44 | MAY,
45 | JULY,
46 | AUGUST,
47 | OCTOBER,
48 | DECEMBER -> 31;
49 | case APRIL,
50 | JUNE,
51 | SEPTEMBER,
52 | NOVEMBER -> 30;
53 | case FEBRUARY -> {
54 | if(isLeapYear(today.getYear())) {
55 | yield 29;
56 | }
57 | yield 28;
58 | }
59 | };
60 |
61 | System.out.println("There are " + daysInMonth + " days in this month.");
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/vector/simd/LinearEquation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.vector.simd;
7 |
8 | import co.ivi.jus.flow.reactive.Returned;
9 | import jdk.incubator.vector.FloatVector;
10 |
11 | public class LinearEquation {
12 | private static final float[] a = new float[] {0.6F, 0.7F, 0.8F, 0.9F};
13 | private static final float[] x = new float[] {1.0F, 2.0F, 3.0F, 4.0F};
14 |
15 | private static final FloatVector va =
16 | FloatVector.fromArray(FloatVector.SPECIES_128, a, 0);
17 | private static final FloatVector vx =
18 | FloatVector.fromArray(FloatVector.SPECIES_128, x, 0);
19 |
20 | private static Returned sumInVector(FloatVector va, FloatVector vx) {
21 | if (va == null || vx == null || va.length() != vx.length()) {
22 | return new Returned.ErrorCode(-1);
23 | }
24 |
25 | // FloatVector vy = va.mul(vx);
26 | float[] y = va.mul(vx).toArray();
27 |
28 | float r = 0F;
29 | for (int i = 0; i < y.length; i++) {
30 | r += y[i];
31 | }
32 |
33 | return new Returned.ReturnValue<>(r);
34 | }
35 |
36 | public static void main(String[] args) {
37 | Returned rt = LinearEquation.sumInVector(va, vx);
38 | switch (rt) {
39 | case Returned.ReturnValue rv -> {
40 | // Get the returned value
41 | if (rv.returnValue() instanceof Float result) {
42 | System.out.println("The summary is: " + result);
43 | } else { // unlikely
44 | System.out.println(
45 | "Implementation error: LinearEquation.sumInScalar");
46 | }
47 | }
48 | case Returned.ErrorCode ec ->
49 | System.out.println("Invalid input parameters");
50 | }
51 | }
52 | }
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/instance/modern/Scope.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.instance.modern;
7 |
8 | import co.ivi.jus.instance.modern.Shape.Square;
9 | import co.ivi.jus.instance.modern.Shape.Rectangle;
10 |
11 | public final class Scope {
12 | public static boolean isSquareImplA(Shape shape) {
13 | if (shape instanceof Rectangle rect) {
14 | // rect is in scope
15 | return rect.length() == rect.width();
16 | }
17 |
18 | // rect is not in scope here
19 | return shape instanceof Square;
20 | }
21 |
22 | public static boolean isSquareImplB(Shape shape) {
23 | if (!(shape instanceof Rectangle rect)) {
24 | // rect is not in scope here
25 | return shape instanceof Square;
26 | }
27 |
28 | // rect is in scope
29 | return rect.length() == rect.width();
30 | }
31 |
32 | public static boolean isSquareImplC(Shape shape) {
33 | return shape instanceof Square || // rect is not in scope here
34 | (shape instanceof Rectangle rect &&
35 | rect.length() == rect.width()); // rect is in scope here
36 | }
37 |
38 | /*
39 | // Comment this method out as it is not compilable.
40 | public static boolean isSquareImplD(Shape shape) {
41 | return shape instanceof Square || // rect is not in scope here
42 | (shape instanceof Rectangle rect ||
43 | rect.length() == rect.width()); // rect is not in scope here
44 | }
45 |
46 | // Comment this method out as it is not compilable.
47 | public static boolean isSquareImplE(Shape shape) {
48 | return shape instanceof Square | // rect is not in scope here
49 | (shape instanceof Rectangle rect &
50 | rect.length() == rect.width()); // rect is in scope here
51 | }
52 | */
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/flow/reactive/UseCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.flow.reactive;
7 |
8 | import co.ivi.jus.shared.Utilities;
9 |
10 | import java.util.concurrent.SubmissionPublisher;
11 | import java.util.function.Function;
12 |
13 | public class UseCase {
14 | public static void main(String[] args) throws InterruptedException {
15 | Returned rt = Digest.of("SHA-256");
16 | switch (rt) {
17 | case Returned.ReturnValue rv -> {
18 | // Get the returned value
19 | if (rv.returnValue() instanceof Digest d) {
20 | // Call the transform method for the message digest.
21 | transform("Hello, World!".getBytes(), d::digest);
22 |
23 | // Wait for completion
24 | Thread.sleep(20000);
25 | } else { // unlikely
26 | System.out.println("Implementation error: SHA-256");
27 | }
28 | }
29 | case Returned.ErrorCode ec ->
30 | System.out.println("Unsupported algorithm: SHA-256");
31 | }
32 | }
33 |
34 | private static void transform(byte[] message,
35 | Function transformFunction) {
36 | SubmissionPublisher publisher =
37 | new SubmissionPublisher<>();
38 |
39 | // Create the transform processor
40 | Transform messageDigest =
41 | new Transform<>(transformFunction);
42 |
43 | // Create subscriber for the processor
44 | Destination subscriber = new Destination<>(
45 | values -> System.out.println(
46 | "Got it: " + Utilities.toHexString(values)));
47 |
48 | // Chain processor and subscriber
49 | publisher.subscribe(messageDigest);
50 | messageDigest.subscribe(subscriber);
51 |
52 | publisher.submit(message);
53 | publisher.close();
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/nullp/returned/FullName.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.nullp.returned;
7 |
8 | import java.util.Objects;
9 |
10 | public final class FullName {
11 | private final String firstName;
12 | private final String middleName;
13 | private final String lastName;
14 |
15 | public FullName(String firstName, String middleName, String lastName) {
16 | this.firstName = firstName;
17 | this.middleName = middleName;
18 | this.lastName = lastName;
19 | }
20 |
21 | public Returned firstName() {
22 | if (firstName == null) {
23 | return new Returned.Undefined();
24 | }
25 |
26 | return new Returned.ReturnValue<>(firstName);
27 | }
28 |
29 | public Returned middleName() {
30 | if (middleName == null) {
31 | return Returned.UNDEFINED;
32 | }
33 |
34 | return new Returned.ReturnValue<>(middleName);
35 | }
36 |
37 | public Returned lastName() {
38 | if (lastName == null) {
39 | return new Returned.Undefined();
40 | }
41 |
42 | return new Returned.ReturnValue<>(lastName);
43 | }
44 |
45 | @Override
46 | public boolean equals(Object obj) {
47 | if (obj == this) {
48 | return true;
49 | }
50 |
51 | if (!(obj instanceof FullName that)) {
52 | return false;
53 | }
54 |
55 | return Objects.equals(this.firstName, that.firstName) &&
56 | Objects.equals(this.middleName, that.middleName) &&
57 | Objects.equals(this.lastName, that.lastName);
58 | }
59 |
60 | @Override
61 | public int hashCode() {
62 | return Objects.hash(firstName, middleName, lastName);
63 | }
64 |
65 | @Override
66 | public String toString() {
67 | return "FullName[" +
68 | "firstName=" + firstName + ", " +
69 | "middleName=" + middleName + ", " +
70 | "lastName=" + lastName + ']';
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/error/former/ExceptionBench.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.error.former;
7 |
8 | import org.openjdk.jmh.annotations.*;
9 | import org.openjdk.jmh.runner.Runner;
10 | import org.openjdk.jmh.runner.RunnerException;
11 | import org.openjdk.jmh.runner.options.Options;
12 | import org.openjdk.jmh.runner.options.OptionsBuilder;
13 |
14 | import java.security.NoSuchAlgorithmException;
15 | import java.util.concurrent.TimeUnit;
16 |
17 | // Benchmark Mode Cnt Score Error Units
18 | // ExceptionBench.noException thrpt 15 1168332489.381 ± 99596462.126 ops/s
19 | // ExceptionBench.withException thrpt 15 698594.835 ± 20852.034 ops/s
20 |
21 | // Benchmark Mode Cnt Score Error Units
22 | // ExceptionBench.noException thrpt 15 1318854854.577 ± 14522418.634 ops/s
23 | // ExceptionBench.withException thrpt 15 713057.511 ± 16631.048 ops/s
24 |
25 | @BenchmarkMode(Mode.Throughput)
26 | @OutputTimeUnit(TimeUnit.SECONDS)
27 | @State(Scope.Thread)
28 | @Fork(3)
29 | @Warmup(iterations = 5)
30 | @Measurement(iterations = 5)
31 | public class ExceptionBench {
32 | public static void main(String[] args) throws RunnerException {
33 | Options options = new OptionsBuilder()
34 | .include(ExceptionBench.class.getSimpleName())
35 | .forks(3)
36 | .warmupIterations(5)
37 | .measurementIterations(5)
38 | .build();
39 |
40 | new Runner(options).run();
41 | }
42 |
43 | @Benchmark
44 | public void withException() {
45 | try {
46 | Digest.of("SHA-128");
47 | } catch (NoSuchAlgorithmException nsae) {
48 | // blank line, ignore the exception.
49 | }
50 | }
51 |
52 | @Benchmark
53 | public void noException() {
54 | try {
55 | Digest.of("SHA-256");
56 | } catch (NoSuchAlgorithmException nsae) {
57 | // blank line, ignore the exception.
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/sealed/propagate/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.sealed.propagate;
7 |
8 | public abstract sealed class Shape {
9 | // permits Shape.Circle, Shape.Rectangle, Shape.Square {
10 | public final String id;
11 |
12 | public Shape(String id) {
13 | this.id = id;
14 | }
15 |
16 | public abstract double area();
17 |
18 | public static non-sealed class Circle extends Shape {
19 | public final double radius;
20 |
21 | public Circle(String id, double radius) {
22 | super(id);
23 | this.radius = radius;
24 | }
25 |
26 | @Override
27 | public double area() {
28 | return Math.PI * radius * radius;
29 | }
30 | }
31 |
32 | public static final class Rectangle extends Shape {
33 | public final double length;
34 | public final double width;
35 |
36 | public Rectangle(String id, double length, double width) {
37 | super(id);
38 | this.length = length;
39 | this.width = width;
40 | }
41 |
42 | @Override
43 | public double area() {
44 | return length * width;
45 | }
46 | }
47 |
48 | public static sealed class Square extends Shape {
49 | public final double side;
50 |
51 | public Square(String id, double side) {
52 | super(id);
53 | this.side = side;
54 | }
55 |
56 | @Override
57 | public double area() {
58 | return side * side;
59 | }
60 | }
61 |
62 | public static final class ColoredSquare extends Square {
63 | public final int rgb;
64 |
65 | public ColoredSquare(String id, double side, int rgb) {
66 | super(id, side);
67 | this.rgb = rgb;
68 | }
69 | }
70 |
71 | public static class ColoredCircle extends Circle {
72 | public final int rgb;
73 |
74 | public ColoredCircle(String id, double radius, int rgb) {
75 | super(id, radius);
76 | this.rgb = rgb;
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/text/methods/TextBlocks.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.text.methods;
7 |
8 | public class TextBlocks {
9 | public static void main(String[] args) {
10 | String stringBlock =
11 | "\n" +
12 | "\n" +
13 | " \n" +
14 | " \"Hello World!\"
\n" +
15 | " \n" +
16 | "\n";
17 | System.out.println("Here is the regular string:\n" + stringBlock);
18 |
19 | String textBlock = """
20 |
21 |
22 |
23 | "Hello World!"
24 |
25 |
26 | """;
27 | System.out.println("Here is the text block:\n" + textBlock);
28 |
29 | System.out.println("Does the text block equal to the regular string? " +
30 | stringBlock.equals(textBlock));
31 | System.out.println("Does the text block refer to the regular string? " +
32 | (stringBlock == textBlock));
33 |
34 | int size = """
35 |
36 |
37 |
38 | "Hello World!"
39 |
40 |
41 | """.length();
42 | System.out.println("The size of the text block code is: " + size);
43 |
44 | textBlock = """
45 |
46 |
47 |
48 | "%s"
49 |
50 |
51 | """.formatted("Hello World!");
52 | System.out.println("Here is the text block:\n" + textBlock);
53 | System.out.println("Does the text block equal to the regular string? " +
54 | stringBlock.equals(textBlock));
55 | System.out.println("Does the text block refer to the regular string? " +
56 | (stringBlock == textBlock));
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/swexpr/review/puruidong/DaysInMonth.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Ruidong pu. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.swexpr.review.puruidong;
7 |
8 | import java.time.LocalDate;
9 | import java.time.Month;
10 | import java.util.Calendar;
11 |
12 | public class DaysInMonth {
13 |
14 | public static int monthLength(Month month,boolean leapYear) {
15 | return switch (month) {
16 | case FEBRUARY -> (leapYear ? 29 : 28);
17 | case APRIL,
18 | JUNE,
19 | SEPTEMBER,
20 | NOVEMBER -> 30;
21 | default -> 31;
22 | };
23 | }
24 |
25 | public static void main(String[] args) {
26 |
27 | var localDate = LocalDate.now();
28 | var isleapYear = localDate.isLeapYear();
29 | System.out.println(monthLength(localDate.getMonth(),isleapYear));
30 | System.out.println(localDate.getMonth().length(isleapYear));
31 |
32 | Calendar today = Calendar.getInstance();
33 | int month = today.get(Calendar.MONTH);
34 | int year = today.get(Calendar.YEAR);
35 |
36 | int daysInMonth = switch (month) {
37 | case Calendar.JANUARY,
38 | Calendar.MARCH,
39 | Calendar.MAY,
40 | Calendar.JULY,
41 | Calendar.AUGUST,
42 | Calendar.OCTOBER,
43 | Calendar.DECEMBER -> 31;
44 | case Calendar.APRIL,
45 | Calendar.JUNE,
46 | Calendar.SEPTEMBER,
47 | Calendar.NOVEMBER -> 30;
48 | case Calendar.FEBRUARY -> {
49 | if (((year % 4 == 0) && !(year % 100 == 0))
50 | || (year % 400 == 0)) {
51 | yield 29;
52 | } else {
53 | yield 28;
54 | }
55 | }
56 | default -> throw new RuntimeException(
57 | "Calendar in JDK does not work");
58 | };
59 |
60 | System.out.println(
61 | "There are " + daysInMonth + " days in this month.");
62 | }
63 |
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/nullp/optional/UseCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.nullp.optional;
7 |
8 | import java.util.Optional;
9 | import java.util.concurrent.atomic.AtomicBoolean;
10 |
11 | public class UseCase {
12 | public static void main(String[] args) {
13 | FullName jack = new FullName("Jack", "", "Brown");
14 | FullName rose = new FullName("Rose", null, "Brown");
15 | FullName nova = new FullName("Nova", "Diane", "Brown");
16 | System.out.println(
17 | "Is Jack's middle name Diane? " +
18 | hasMiddleName(jack, "Diane"));
19 | System.out.println(
20 | "Is Rose's middle name Diane? " +
21 | hasMiddleName(rose, "Diane"));
22 | System.out.println(
23 | "Is Nova's middle name Diane? " +
24 | hasMiddleName(nova, "Diane"));
25 | }
26 |
27 | private static boolean hasMiddleName(FullName fullName, String middleName) {
28 | return fullName.middleName().get().equals(middleName);
29 | }
30 |
31 | private static boolean hasMiddleNameImplB(FullName fullName, String middleName) {
32 | return fullName.middleName().equals(Optional.ofNullable(middleName));
33 | }
34 |
35 | private static boolean hasMiddleNameImplC(FullName fullName, String middleName) {
36 | return fullName.middleName().isPresent() ?
37 | fullName.middleName().get().equals(middleName) :
38 | middleName == null;
39 | }
40 |
41 | private static boolean hasMiddleNameImplD(FullName fullName, String middleName) {
42 | if (fullName.middleName().isPresent()) {
43 | return fullName.middleName().get().equals(middleName);
44 | }
45 |
46 | return middleName == null;
47 | }
48 |
49 | private static boolean hasMiddleNameImplE(FullName fullName, String middleName) {
50 | AtomicBoolean isMatched = new AtomicBoolean(false);
51 | fullName.middleName().ifPresentOrElse(
52 | v -> isMatched.set(v.equals(middleName)),
53 | () -> isMatched.set(middleName == null)
54 | );
55 |
56 | return isMatched.get();
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/sealed/review/puruidong/Shape.java:
--------------------------------------------------------------------------------
1 | package cn.bckf.sealed;
2 |
3 | public abstract sealed class Shape {
4 | public final String id;
5 |
6 | public Shape(String id) {
7 | this.id = id;
8 | }
9 |
10 | public abstract double area();
11 |
12 | public static final class Circle extends Shape {
13 | public final double radius;
14 |
15 | public Circle(String id, double radius) {
16 | super(id);
17 | this.radius = radius;
18 | }
19 |
20 | @Override
21 | public double area() {
22 | return Math.PI * radius * radius;
23 | }
24 | }
25 |
26 | public static final class Square extends Shape {
27 | public final double side;
28 |
29 | public Square(String id, double side) {
30 | super(id);
31 | this.side = side;
32 | }
33 |
34 | @Override
35 | public double area() {
36 | return side * side;
37 | }
38 | }
39 |
40 | public static final class Rectangle extends Shape {
41 | public final double width;
42 | public final double length;
43 |
44 | public Rectangle(String id, double length, double width) {
45 | super(id);
46 | this.length = length;
47 | this.width = width;
48 | }
49 |
50 | @Override
51 | public double area() {
52 | return length * width;
53 | }
54 | }
55 |
56 | public static String getInstanceName(Shape shape) {
57 | /// reference: https://openjdk.java.net/jeps/406
58 | return switch (shape) {
59 | case Circle circle -> circle.getClass().getName();
60 | case Square square -> square.getClass().getName();
61 | case Rectangle rectangle -> rectangle.getClass().getName();
62 | default -> "";
63 | };
64 | }
65 |
66 | public static void main(String[] args) {
67 | Shape circle = new Circle("1", 6.0);
68 | System.out.println(getInstanceName(circle));
69 | Circle circle1 = new Circle("2", 7.5);
70 | System.out.println(circle1.getClass().getName());
71 | Shape square = new Square("2", 5.0);
72 | System.out.println(getInstanceName(square));
73 | Shape rectAngele = new Rectangle("3", 7.0,5.0);
74 | System.out.println(getInstanceName(rectAngele));
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/text/indent/TextBlocks.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.text.indent;
7 |
8 | public class TextBlocks {
9 | public static void main(String[] args) {
10 | String stringBlock =
11 | "\n" +
12 | "\n" +
13 | " \n" +
14 | " \"Hello World!\"
\n" +
15 | " \n" +
16 | "\n";
17 | System.out.println("Here is the regular string:\n" + stringBlock);
18 |
19 | String textBlock = """
20 |
21 |
22 |
23 | "Hello World!"
24 |
25 |
26 | """;
27 | System.out.println("Here is the text block:\n" + textBlock);
28 |
29 | System.out.println("Does the text block equal to the regular string? " +
30 | stringBlock.equals(textBlock));
31 | System.out.println("Does the text block refer to the regular string? " +
32 | (stringBlock == textBlock));
33 |
34 | textBlock = """
35 |
36 |
37 |
38 | "Hello World!"
39 |
40 |
41 | """;
42 | System.out.println("Here is the text block:\n" + textBlock);
43 | System.out.println("Does the text block equal to the regular string? " +
44 | stringBlock.equals(textBlock));
45 | System.out.println("Does the text block refer to the regular string? " +
46 | (stringBlock == textBlock));
47 |
48 | textBlock = """
49 |
50 |
51 |
52 | "Hello World!"
53 |
54 |
55 | """;
56 | System.out.println("Here is the text block:\n" + textBlock);
57 | System.out.println("Does the text block equal to the regular string? " +
58 | stringBlock.equals(textBlock));
59 | System.out.println("Does the text block refer to the regular string? " +
60 | (stringBlock == textBlock));
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/sealed/review/songor/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, songor. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.sealed.review.songor;
7 |
8 | public abstract sealed class Shape {
9 | public final String id;
10 |
11 | public Shape(String id) {
12 | this.id = id;
13 | }
14 |
15 | public abstract double area();
16 |
17 | public static final class Circle extends Shape {
18 | public final double radius;
19 |
20 | public Circle(String id, double radius) {
21 | super(id);
22 | this.radius = radius;
23 | }
24 |
25 | @Override
26 | public double area() {
27 | return Math.PI * radius * radius;
28 | }
29 | }
30 |
31 | public static final class Square extends Shape {
32 | public final double side;
33 |
34 | public Square(String id, double side) {
35 | super(id);
36 | this.side = side;
37 | }
38 |
39 | @Override
40 | public double area() {
41 | return side * side;
42 | }
43 | }
44 |
45 | public static final class Rectangle extends Shape {
46 | public final double length;
47 |
48 | public final double width;
49 |
50 | public Rectangle(String id, double length, double width) {
51 | super(id);
52 | this.length = length;
53 | this.width = width;
54 | }
55 |
56 | @Override
57 | public double area() {
58 | return length * width;
59 | }
60 | }
61 |
62 | public static boolean isCircle(Shape shape) {
63 | return (shape instanceof Circle);
64 | }
65 |
66 | public static boolean isSquare(Shape shape) {
67 | return shape instanceof Square || shape instanceof Rectangle rectangle && rectangle.length == rectangle.width;
68 | }
69 |
70 | public static void main(String[] args) {
71 | Rectangle rectangle = new Rectangle("1", 1.0D, 1.0D);
72 | System.out.println("rectangle is special square (same length and width)? " + Shape.isSquare(rectangle));
73 | System.out.println("rectangle is circle? " + Shape.isCircle(rectangle));
74 |
75 | Square square = new Square("2", 1.0D);
76 | System.out.println("square is square? " + Shape.isSquare(square));
77 | System.out.println("square is circle? " + Shape.isCircle(square));
78 |
79 | System.out.println("circle is circle? " + Shape.isCircle(new Circle("3", 1.0D)));
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/flow/former/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.flow.former;
7 |
8 | import java.security.MessageDigest;
9 | import java.security.NoSuchAlgorithmException;
10 |
11 | public sealed abstract class Digest {
12 | private static final Sha256 sha256 = new Sha256();
13 | private static final Sha512 sha512 = new Sha512();
14 |
15 | private static final class Sha256 extends Digest {
16 | private final MessageDigest md;
17 |
18 | private Sha256() {
19 | MessageDigest md;
20 | try {
21 | md = MessageDigest.getInstance("SHA-256");
22 | } catch (NoSuchAlgorithmException e) {
23 | md = null;
24 | }
25 | this.md = md;
26 | }
27 |
28 | @Override
29 | public byte[] digest(byte[] message) {
30 | return md.digest(message);
31 | }
32 | }
33 |
34 | private static final class Sha512 extends Digest {
35 | private final MessageDigest md;
36 |
37 | private Sha512() {
38 | MessageDigest md;
39 | try {
40 | md = MessageDigest.getInstance("SHA-512");
41 | } catch (NoSuchAlgorithmException e) {
42 | md = null;
43 | }
44 | this.md = md;
45 | }
46 |
47 | @Override
48 | public byte[] digest(byte[] message) {
49 | return md.digest(message);
50 | }
51 | }
52 |
53 | public static Digest of(String algorithm) throws NoSuchAlgorithmException {
54 | return switch (algorithm) {
55 | case "SHA-256" -> {
56 | if (sha256.md == null) {
57 | throw new NoSuchAlgorithmException(
58 | "Unsupported digest algorithm SHA-256");
59 | } else {
60 | yield sha256;
61 | }
62 | }
63 | case "SHA-512" -> {
64 | if (sha512.md == null) {
65 | throw new NoSuchAlgorithmException(
66 | "Unsupported digest algorithm SHA-512");
67 | } else {
68 | yield sha512;
69 | }
70 | }
71 | case null, default -> throw new NoSuchAlgorithmException(
72 | "Unsupported digest algorithm " + algorithm);
73 | };
74 | }
75 |
76 | public abstract byte[] digest(byte[] message);
77 | }
78 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/flow/reactive/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.flow.reactive;
7 |
8 | import java.security.MessageDigest;
9 | import java.security.NoSuchAlgorithmException;
10 |
11 | public sealed interface Digest {
12 | Returned.ErrorCode noSuchAlgorithmErrorCode = new Returned.ErrorCode(-1);
13 |
14 | final class Sha256 implements Digest {
15 | private static final Returned.ReturnValue returnedSha256;
16 |
17 | private final MessageDigest md;
18 |
19 | static {
20 | Sha256 sha256 = new Sha256();
21 | returnedSha256 = sha256.md == null ?
22 | null : new Returned.ReturnValue<>(sha256);
23 | }
24 |
25 | private Sha256() {
26 | MessageDigest md;
27 | try {
28 | md = MessageDigest.getInstance("SHA-256");
29 | } catch (NoSuchAlgorithmException e) {
30 | md = null;
31 | }
32 | this.md = md;
33 | }
34 |
35 | @Override
36 | public byte[] digest(byte[] message) {
37 | return md.digest(message);
38 | }
39 | }
40 |
41 | final class Sha512 implements Digest {
42 | private static final Returned.ReturnValue returnedSha512;
43 | private final MessageDigest md;
44 |
45 | static {
46 | Sha512 sha512 = new Sha512();
47 | returnedSha512 = sha512.md == null ?
48 | null : new Returned.ReturnValue<>(sha512);
49 | }
50 |
51 | private Sha512() {
52 | MessageDigest md;
53 | try {
54 | md = MessageDigest.getInstance("SHA-512");
55 | } catch (NoSuchAlgorithmException e) {
56 | md = null;
57 | }
58 | this.md = md;
59 | }
60 |
61 | @Override
62 | public byte[] digest(byte[] message) {
63 | return md.digest(message);
64 | }
65 | }
66 |
67 | static Returned of(String algorithm) {
68 | return switch (algorithm) {
69 | case "SHA-256" -> (Sha256.returnedSha256 != null) ?
70 | Sha256.returnedSha256 : noSuchAlgorithmErrorCode;
71 | case "SHA-512" -> Sha512.returnedSha512 != null ?
72 | Sha512.returnedSha512 : noSuchAlgorithmErrorCode;
73 | case null, default -> noSuchAlgorithmErrorCode;
74 | };
75 | }
76 |
77 | byte[] digest(byte[] message);
78 | }
79 |
80 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/retire/former/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.retire.former;
7 |
8 | import java.security.MessageDigest;
9 | import java.security.NoSuchAlgorithmException;
10 |
11 | public sealed abstract class Digest {
12 |
13 | private static final class Sha256 extends Digest {
14 | private static final Sha256 sha256 = new Sha256();
15 |
16 | private final MessageDigest md;
17 |
18 | private Sha256() {
19 | MessageDigest md;
20 | try {
21 | md = MessageDigest.getInstance("SHA-256");
22 | } catch (NoSuchAlgorithmException e) {
23 | md = null;
24 | }
25 | this.md = md;
26 | }
27 |
28 | @Override
29 | public byte[] digest(byte[] message) {
30 | return md.digest(message);
31 | }
32 | }
33 |
34 | private static final class Sha512 extends Digest {
35 | private static final Sha512 sha512 = new Sha512();
36 |
37 | private final MessageDigest md;
38 |
39 | private Sha512() {
40 | MessageDigest md;
41 | try {
42 | md = MessageDigest.getInstance("SHA-512");
43 | } catch (NoSuchAlgorithmException e) {
44 | md = null;
45 | }
46 | this.md = md;
47 | }
48 |
49 | @Override
50 | public byte[] digest(byte[] message) {
51 | return md.digest(message);
52 | }
53 | }
54 |
55 | public static Digest of(String algorithm) throws NoSuchAlgorithmException {
56 | return switch (algorithm) {
57 | case "SHA-256" -> {
58 | if (Sha256.sha256.md == null) {
59 | throw new NoSuchAlgorithmException(
60 | "Unsupported digest algorithm SHA-256");
61 | } else {
62 | yield Sha256.sha256;
63 | }
64 | }
65 | case "SHA-512" -> {
66 | if (Sha512.sha512.md == null) {
67 | throw new NoSuchAlgorithmException(
68 | "Unsupported digest algorithm SHA-512");
69 | } else {
70 | yield Sha512.sha512;
71 | }
72 | }
73 | case null, default -> throw new NoSuchAlgorithmException(
74 | "Unsupported digest algorithm " + algorithm);
75 | };
76 | }
77 |
78 | public abstract byte[] digest(byte[] message);
79 | }
80 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/flow/callback/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.flow.callback;
7 |
8 | import java.security.MessageDigest;
9 | import java.security.NoSuchAlgorithmException;
10 | import java.util.function.Consumer;
11 |
12 | public sealed abstract class Digest {
13 | private static final Sha256 sha256 = new Sha256();
14 | private static final Sha512 sha512 = new Sha512();
15 |
16 | private static final class Sha256 extends Digest {
17 | private final MessageDigest md;
18 |
19 | private Sha256() {
20 | MessageDigest md;
21 | try {
22 | md = MessageDigest.getInstance("SHA-256");
23 | } catch (NoSuchAlgorithmException e) {
24 | md = null;
25 | }
26 | this.md = md;
27 | }
28 |
29 | @Override
30 | public void digest(byte[] message,
31 | Consumer onSuccess, Consumer onFailure) {
32 | onSuccess.accept(md.digest(message));
33 | }
34 | }
35 |
36 | private static final class Sha512 extends Digest {
37 | private final MessageDigest md;
38 |
39 | private Sha512() {
40 | MessageDigest md;
41 | try {
42 | md = MessageDigest.getInstance("SHA-512");
43 | } catch (NoSuchAlgorithmException e) {
44 | md = null;
45 | }
46 | this.md = md;
47 | }
48 |
49 | @Override
50 | public void digest(byte[] message,
51 | Consumer onSuccess, Consumer onFailure) {
52 | onSuccess.accept(md.digest(message));
53 | }
54 | }
55 |
56 | public static void of(String algorithm,
57 | Consumer onSuccess, Consumer onFailure) {
58 | switch (algorithm) {
59 | case "SHA-256" -> {
60 | if (sha256.md != null) {
61 | onSuccess.accept(sha256);
62 | } else {
63 | onFailure.accept(-1);
64 | }
65 | }
66 | case "SHA-512" -> {
67 | if (sha512.md != null) {
68 | onSuccess.accept(sha512);
69 | } else {
70 | onFailure.accept(-1);
71 | }
72 | }
73 | case null, default ->
74 | onFailure.accept(-1);
75 | };
76 | }
77 |
78 | public abstract void digest(byte[] message,
79 | Consumer onSuccess, Consumer onFailure);
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/flow/reactive/ThroughputBench.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.flow.reactive;
7 |
8 | import co.ivi.jus.shared.Utilities;
9 | import org.openjdk.jmh.annotations.*;
10 | import org.openjdk.jmh.runner.Runner;
11 | import org.openjdk.jmh.runner.RunnerException;
12 | import org.openjdk.jmh.runner.options.Options;
13 | import org.openjdk.jmh.runner.options.OptionsBuilder;
14 |
15 | import java.util.concurrent.SubmissionPublisher;
16 | import java.util.concurrent.TimeUnit;
17 |
18 | @BenchmarkMode(Mode.Throughput)
19 | @OutputTimeUnit(TimeUnit.SECONDS)
20 | @State(Scope.Thread)
21 | @Fork(3)
22 | @Warmup(iterations = 5)
23 | @Measurement(iterations = 5)
24 | public class ThroughputBench {
25 | private static Digest messageDigest;
26 | private static SubmissionPublisher publisher = new SubmissionPublisher<>();
27 |
28 | static {
29 | Returned rt = Digest.of("SHA-256");
30 | switch (rt) {
31 | case Returned.ReturnValue rv -> {
32 | // Get the returned value
33 | if (rv.returnValue() instanceof Digest d) {
34 | messageDigest = d;
35 | // Create the transformation processor - the hash function
36 | Transform messageDigest =
37 | new Transform<>(d::digest);
38 |
39 | // Create subscriber for the processor
40 | Destination subscriber = new Destination<>(
41 | digest -> System.out.println(
42 | "Got message digest: " +
43 | Utilities.toHexString(digest)));
44 |
45 | // Chain processor and subscriber
46 | publisher.subscribe(messageDigest);
47 | messageDigest.subscribe(subscriber);
48 | }
49 | }
50 | case Returned.ErrorCode ec -> {
51 | // blank, unlikely
52 | }
53 | };
54 | }
55 |
56 | public static void main(String[] args) throws RunnerException {
57 | Options options = new OptionsBuilder()
58 | .include(ThroughputBench.class.getSimpleName())
59 | .forks(3)
60 | .warmupIterations(5)
61 | .measurementIterations(5)
62 | .build();
63 |
64 | new Runner(options).run();
65 | }
66 |
67 | @Benchmark
68 | public void simpleDigest() {
69 | publisher.submit("Hello, World!".getBytes());
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | co.ivi
8 | java-up
9 | pom
10 | 01
11 |
12 | jus.crypto
13 | jus.crypto.impl
14 | jus.crypto.use
15 |
16 |
17 |
18 |
19 | org.openjdk.jmh
20 | jmh-core
21 | ${jmh.version}
22 |
23 |
24 | org.openjdk.jmh
25 | jmh-generator-annprocess
26 | ${jmh.version}
27 | provided
28 |
29 |
30 | co.ivi
31 | jus.crypto
32 | 01
33 | compile
34 |
35 |
36 |
37 |
38 | 17
39 | 17
40 | 1.33
41 |
42 |
43 |
44 |
45 |
46 | org.apache.maven.plugins
47 | maven-compiler-plugin
48 | 3.8.1
49 |
50 | 17
51 | true
52 |
53 | --enable-preview
54 | --add-modules=jdk.incubator.foreign,jdk.incubator.vector
55 |
56 | 17
57 | 17
58 |
59 |
60 |
61 |
62 | org.apache.maven.plugins
63 | maven-surefire-plugin
64 | 3.0.0-M5
65 |
66 | --enable-preview --add-modules=jdk.incubator.foreign,jdk.incubator.vector
67 |
68 |
69 |
70 |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/instance/review/xuling/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Ling Xu. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.instance.review.xuling;
7 |
8 | /**
9 | * pattern match work
10 | *
11 | * @author jack
12 | * @since 2021/11/24
13 | **/
14 | public sealed interface Shape
15 | permits Shape.Circle, Shape.Rectangle, Shape.Square {
16 | co.ivi.jus.instance.review.xuelei.Shape.Rectangle rect = null; // field variable
17 |
18 | record Circle(double radius) implements Shape {
19 | // blank
20 | }
21 |
22 | record Square(double side) implements Shape {
23 | // blank
24 | }
25 |
26 | record Rectangle(double length, double width) implements Shape {
27 | // blank
28 | }
29 |
30 | static void main(String[] args) {
31 | Shape shape = new Shape.Rectangle(10, 10);
32 | System.out.println("It should be ture that " + shape +
33 | " is a square: " + isSquare(shape));
34 | System.out.println("It can't be ture that " + shape +
35 | " is a circle: " + !isCircle(shape));
36 |
37 | System.out.println();
38 |
39 | shape = new Shape.Circle(10);
40 | System.out.println("It cannot be ture that " + shape +
41 | " is a square: " + (!isSquare(shape)));
42 | System.out.println("It should be ture that " + shape +
43 | " is a circle: " + isCircle(shape));
44 | }
45 |
46 | static boolean isSquare(Shape shape) {
47 | if (!(shape instanceof Shape.Rectangle rect)) {
48 | // Field rect is in scope, local rect is not in scope here
49 | System.out.println(
50 | "This should be the field rect: " + (rect == null));
51 | } else {
52 | // Field rect is shadowed, local rect is in scope
53 | System.out.println(
54 | "This should be the local rect: " + rect.equals(shape));
55 | return (rect.length == rect.width);
56 | }
57 |
58 | // Field rect is in scope, local rect is not in scope here, this case is not mentioned.
59 | System.out.println(
60 | "This should be the field rect: " + (rect == null));
61 | return (shape instanceof Shape.Square);
62 | }
63 |
64 | static boolean isCircle(Shape shape) {
65 | if (!(shape instanceof Shape.Rectangle rect)) {
66 | // Field rect is in scope, local rect is not in scope here
67 | System.out.println(
68 | "This should be the field rect: " + (rect == null));
69 | return (shape instanceof Shape.Circle);
70 | }
71 |
72 | // Field rect is shadowed, local rect is in scope
73 | System.out.println(
74 | "This should be the field rect: " + (rect.equals(shape)));
75 | return false;
76 | }
77 | }
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/retire/removal/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.retire.removal;
7 |
8 | import java.security.MessageDigest;
9 | import java.security.NoSuchAlgorithmException;
10 |
11 | public sealed abstract class Digest {
12 | private static final class Sha256 extends Digest {
13 | private static final Sha256 sha256 = new Sha256();
14 |
15 | private final MessageDigest md;
16 |
17 | private Sha256() {
18 | MessageDigest md;
19 | try {
20 | md = MessageDigest.getInstance("SHA-256");
21 | } catch (NoSuchAlgorithmException e) {
22 | md = null;
23 | }
24 | this.md = md;
25 | }
26 |
27 | @SuppressWarnings("removal")
28 | @Override
29 | public byte[] digest(byte[] message) {
30 | return md.digest(message);
31 | }
32 | }
33 |
34 | private static final class Sha512 extends Digest {
35 | private static final Sha512 sha512 = new Sha512();
36 |
37 | private final MessageDigest md;
38 |
39 | private Sha512() {
40 | MessageDigest md;
41 | try {
42 | md = MessageDigest.getInstance("SHA-512");
43 | } catch (NoSuchAlgorithmException e) {
44 | md = null;
45 | }
46 | this.md = md;
47 | }
48 |
49 | @SuppressWarnings("removal")
50 | @Override
51 | public byte[] digest(byte[] message) {
52 | return md.digest(message);
53 | }
54 | }
55 |
56 | public static Digest of(String algorithm) throws NoSuchAlgorithmException {
57 | return switch (algorithm) {
58 | case "SHA-256" -> {
59 | if (Sha256.sha256.md == null) {
60 | throw new NoSuchAlgorithmException(
61 | "Unsupported digest algorithm SHA-256");
62 | } else {
63 | yield Sha256.sha256;
64 | }
65 | }
66 | case "SHA-512" -> {
67 | if (Sha512.sha512.md == null) {
68 | throw new NoSuchAlgorithmException(
69 | "Unsupported digest algorithm SHA-512");
70 | } else {
71 | yield Sha512.sha512;
72 | }
73 | }
74 | case null, default -> throw new NoSuchAlgorithmException(
75 | "Unsupported digest algorithm " + algorithm);
76 | };
77 | }
78 |
79 | @Deprecated(since = "1.4", forRemoval = true)
80 | public abstract byte[] digest(byte[] message);
81 |
82 | public void digest(byte[] message, byte[] digestValue) {
83 | byte[] digest = digest(message);
84 | System.arraycopy(digest, 0, digestValue, 0, digest.length);
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/sealed/review/xuling/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Ling Xu. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.sealed.review.xuling;
7 |
8 | /**
9 | * sealed shape demo
10 | *
11 | * @author jack
12 | * @since 2021/11/22
13 | **/
14 | public abstract sealed class Shape permits Shape.Circle, Shape.Square, Shape.Rectangle {
15 | public final String id;
16 |
17 | public Shape(String id) {
18 | this.id = id;
19 | }
20 |
21 | public abstract double area();
22 |
23 | public static final class Circle extends Shape {
24 | public final double radius;
25 |
26 | public Circle(String id, double radius) {
27 | super(id);
28 | this.radius = radius;
29 | }
30 |
31 | @Override
32 | public double area() {
33 | return Math.PI * radius * radius;
34 | }
35 | }
36 |
37 | public static final class Square extends Shape {
38 | public final double side;
39 |
40 | public Square(String id, double side) {
41 | super(id);
42 | this.side = side;
43 | }
44 |
45 | @Override
46 | public double area() {
47 | return side * side;
48 | }
49 | }
50 |
51 | // Here is your code for Rectangle.
52 | public static final class Rectangle extends Shape {
53 | public final double length;
54 | public final double width;
55 |
56 | public Rectangle(String id, double length, double width) {
57 | super(id);
58 | this.length = length;
59 | this.width = width;
60 | }
61 |
62 | @Override
63 | public double area() {
64 | return length * width;
65 | }
66 | }
67 |
68 | // Here is the test for circle.
69 | public static boolean isCircle(Shape shape) {
70 | // Here goes your update.
71 | return (shape instanceof Circle);
72 | }
73 |
74 | // Here is the test for square.
75 | public static boolean isSquare(Shape shape) {
76 | if (shape instanceof Rectangle rect) {
77 | return (rect.length == rect.width);
78 | }
79 | return (shape instanceof Square);
80 | }
81 |
82 | // Here is the code to run your test.
83 | public static void main(String[] args) {
84 | // Here is your code.
85 | Circle circle = new Circle("1", 2.0);
86 | Square square = new Square("2", 2.0);
87 | Rectangle rectangle = new Rectangle("3", 1.0, 2.0);
88 | Rectangle square2 = new Rectangle("3", 2.0, 2.0);
89 | System.out.println("circle is square: " + Shape.isSquare(circle));
90 | System.out.println("square is square: " + Shape.isSquare(square));
91 | System.out.println("rectangle is square: " + Shape.isSquare(rectangle));
92 | System.out.println("square2 is square: " + Shape.isSquare(square2));
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/src/main/java/co/ivi/jus/retire/deprecated/Digest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, Xuelei Fan. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | */
5 |
6 | package co.ivi.jus.retire.deprecated;
7 |
8 | import java.security.MessageDigest;
9 | import java.security.NoSuchAlgorithmException;
10 |
11 | public sealed abstract class Digest {
12 | private static final class Sha256 extends Digest {
13 | private static final Sha256 sha256 = new Sha256();
14 |
15 | private final MessageDigest md;
16 |
17 | private Sha256() {
18 | MessageDigest md;
19 | try {
20 | md = MessageDigest.getInstance("SHA-256");
21 | } catch (NoSuchAlgorithmException e) {
22 | md = null;
23 | }
24 | this.md = md;
25 | }
26 |
27 | @Override
28 | public byte[] digest(byte[] message) {
29 | return md.digest(message);
30 | }
31 | }
32 |
33 | private static final class Sha512 extends Digest {
34 | private static final Sha512 sha512 = new Sha512();
35 |
36 | private final MessageDigest md;
37 |
38 | private Sha512() {
39 | MessageDigest md;
40 | try {
41 | md = MessageDigest.getInstance("SHA-512");
42 | } catch (NoSuchAlgorithmException e) {
43 | md = null;
44 | }
45 | this.md = md;
46 | }
47 |
48 | @Override
49 | public byte[] digest(byte[] message) {
50 | return md.digest(message);
51 | }
52 | }
53 |
54 | public static Digest of(String algorithm) throws NoSuchAlgorithmException {
55 | return switch (algorithm) {
56 | case "SHA-256" -> {
57 | if (Sha256.sha256.md == null) {
58 | throw new NoSuchAlgorithmException(
59 | "Unsupported digest algorithm SHA-256");
60 | } else {
61 | yield Sha256.sha256;
62 | }
63 | }
64 | case "SHA-512" -> {
65 | if (Sha512.sha512.md == null) {
66 | throw new NoSuchAlgorithmException(
67 | "Unsupported digest algorithm SHA-512");
68 | } else {
69 | yield Sha512.sha512;
70 | }
71 | }
72 | case null, default -> throw new NoSuchAlgorithmException(
73 | "Unsupported digest algorithm " + algorithm);
74 | };
75 | }
76 |
77 | /**
78 | * -- snipped
79 | *
80 | * @deprecated This method is not performance friendly. Use
81 | * {@link #digest(byte[], byte[]) instead.
82 | */
83 | @Deprecated
84 | public abstract byte[] digest(byte[] message);
85 |
86 | public void digest(byte[] message, byte[] digestValue) {
87 | byte[] digest = digest(message);
88 | System.arraycopy(digest, 0, digestValue, 0, digest.length);
89 | }
90 | }
91 |
--------------------------------------------------------------------------------