From 2dc953dcadfb9a6dcda08d371967dbdc7ac6bac7 Mon Sep 17 00:00:00 2001 From: Michael Olayemi Date: Wed, 27 Sep 2023 16:32:18 +0000 Subject: [PATCH] Code Snippets in Java API Documentation (#14792) --- .../snippettag/GreetingsExternalSnippet.java | 18 ++++++++++ .../snippettag/GreetingsHighlightTag.java | 35 +++++++++++++++++++ .../snippettag/GreetingsInlineSnippet.java | 17 +++++++++ .../GreetingsReplaceAndLinkTag.java | 24 +++++++++++++ .../main/java/snippet-files/BinarySearch.java | 27 ++++++++++++++ .../java/snippet-files/application.properties | 4 +++ 6 files changed, 125 insertions(+) create mode 100644 core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsExternalSnippet.java create mode 100644 core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsHighlightTag.java create mode 100644 core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsInlineSnippet.java create mode 100644 core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsReplaceAndLinkTag.java create mode 100644 core-java-modules/core-java-documentation/src/main/java/snippet-files/BinarySearch.java create mode 100644 core-java-modules/core-java-documentation/src/main/java/snippet-files/application.properties diff --git a/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsExternalSnippet.java b/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsExternalSnippet.java new file mode 100644 index 0000000000..6cf27b7acc --- /dev/null +++ b/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsExternalSnippet.java @@ -0,0 +1,18 @@ +package com.baeldung.snippettag; + +/** + * + * External code snippet showing the loop process in binary search method. + * {@snippet class="BinarySearch" region="binary"} + * + * Time Zone + * {@snippet file="application.properties" region="zone"} + * + */ + +public class GreetingsExternalSnippet { + public void helloBinarySearch() { + System.out.println("Hi, it's great knowing that binary search uses a loop under the hood"); + } + +} diff --git a/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsHighlightTag.java b/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsHighlightTag.java new file mode 100644 index 0000000000..03f30e1ad2 --- /dev/null +++ b/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsHighlightTag.java @@ -0,0 +1,35 @@ +package com.baeldung.snippettag; + + +/** + * The code below shows a full highlighted line + * {@snippet : + * public void helloBaeldung() { + * System.out.println("Hello From Team Baeldung"); // @highlight + * } + * } + * + * highlighting a substring + * {@snippet : + * public void helloBaeldung() { + * System.out.println("Hello From Team Baeldung"); // @highlight substring="println" + * } + * } + * + * highlighting texts on multiple lines + * {@snippet : + * public void helloBaeldung() { + * System.out.println("Hello From Team Baeldung"); // @highlight region substring="println" + * String country = "USA"; + * System.out.println("Hello From Team " + country); // @end + * } + * } + * + */ + +public class GreetingsHighlightTag { + public void helloBaeldung() { + System.out.println("Hello From Team Baeldung"); + } + +} diff --git a/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsInlineSnippet.java b/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsInlineSnippet.java new file mode 100644 index 0000000000..71876eecf6 --- /dev/null +++ b/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsInlineSnippet.java @@ -0,0 +1,17 @@ +package com.baeldung.snippettag; + +/** + * The code below shows the content of {@code helloBaeldung()} method + * {@snippet : + * public void helloBaeldung() { + * System.out.println("Hello From Team Baeldung"); + * } + * } + */ + +public class GreetingsInlineSnippet { + public void helloBaeldung() { + System.out.println("Hello From Team Baeldung"); + } + +} diff --git a/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsReplaceAndLinkTag.java b/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsReplaceAndLinkTag.java new file mode 100644 index 0000000000..7f6f1b72f7 --- /dev/null +++ b/core-java-modules/core-java-documentation/src/main/java/com/baeldung/snippettag/GreetingsReplaceAndLinkTag.java @@ -0,0 +1,24 @@ +package com.baeldung.snippettag; + +/** + * + * Using the replace tag + * {@snippet : + * public void helloBaeldung() { + * System.out.println("Hello From Team Baeldung"); // @replace regex='".*"' replacement="..." + * } + * } + * Using the link tag + * {@snippet : + * public void helloBaeldung() { + * System.out.println("Hello From Team Baeldung"); // @link substring="System.out" target="System#out" + * } + * } + * + */ + +public class GreetingsReplaceAndLinkTag { + public void helloBaeldung() { + System.out.println("Hello From Team Baeldung"); + } +} diff --git a/core-java-modules/core-java-documentation/src/main/java/snippet-files/BinarySearch.java b/core-java-modules/core-java-documentation/src/main/java/snippet-files/BinarySearch.java new file mode 100644 index 0000000000..a5f6e565ac --- /dev/null +++ b/core-java-modules/core-java-documentation/src/main/java/snippet-files/BinarySearch.java @@ -0,0 +1,27 @@ + +public class BinarySearch { + + public int search(int[] list, int item) { + int index = Integer.MAX_VALUE; + int low = 0; + int high = list.length - 1; + // @start region="binary" + while (low <= high) { + int mid = high - low; + int guess = list[mid]; + if (guess == item) { + index = mid; + break; + } else if (guess > item) { + low = mid - 1; + } else { + low = mid + 1; + } + low++; + } + // @end region="binary" + + return index; + } + +} diff --git a/core-java-modules/core-java-documentation/src/main/java/snippet-files/application.properties b/core-java-modules/core-java-documentation/src/main/java/snippet-files/application.properties new file mode 100644 index 0000000000..a2aeefdb89 --- /dev/null +++ b/core-java-modules/core-java-documentation/src/main/java/snippet-files/application.properties @@ -0,0 +1,4 @@ +# @start region="zone" +local.timezone = GMT+1 +local.zip = 94123 +# @end region="zone" \ No newline at end of file