diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/Factory/FactoryAndStrategy.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/Factory/FactoryAndStrategy.java index 221137d..245040e 100644 --- a/ForJdk17/src/main/java/cn/whaifree/designPattern/Factory/FactoryAndStrategy.java +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/Factory/FactoryAndStrategy.java @@ -54,5 +54,6 @@ public class FactoryAndStrategy { } } - } + + diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/AbstractFactoryPattern/AbstractFactoryPattern.java b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/AbstractFactoryPattern/AbstractFactoryPattern.java index d19e417..1427ce4 100644 --- a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/AbstractFactoryPattern/AbstractFactoryPattern.java +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/AbstractFactoryPattern/AbstractFactoryPattern.java @@ -2,6 +2,11 @@ package cn.whaifree.designPattern.kama.CreateType.AbstractFactoryPattern; public class AbstractFactoryPattern { + /** + * AbstractFactory--> ModernFactory --| + * --> ClassicFactory --|-->Sofa OR Chair + * @param args + */ // https://kamacoder.com/problempage.php?pid=1077 public static void main(String[] args) { diff --git a/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/工厂模式的几种类型,简单工厂、工厂方法、抽象工厂.md b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/工厂模式的几种类型,简单工厂、工厂方法、抽象工厂.md new file mode 100644 index 0000000..e6619a9 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/designPattern/kama/CreateType/工厂模式的几种类型,简单工厂、工厂方法、抽象工厂.md @@ -0,0 +1,57 @@ +### 简单工厂 + +一个工厂生产多种产品 + +```java +public static Product createProduct(String type) { + if ("A".equalsIgnoreCase(type)) { + return new ProductA(); + } else if ("B".equalsIgnoreCase(type)) { + return new ProductB(); + } + throw new IllegalArgumentException("Unknown product type"); +} +``` + +### 工厂方法 + +对工厂抽象 + +```java +// 抽象工厂接口 +interface Factory { + Product createProduct(); +} + +// 具体工厂A +class FactoryA implements Factory { + @Override + public Product createProduct() { + return new ProductA(); + } +} + +public static void main(String[] args) { + Factory factory = new FactoryA(); + Product product = factory.createProduct(); + product.use(); // 输出: Using Product A +} +``` + +### 抽象工厂 + +对产品再次抽象出不同属性 + +```java +// 产品族A接口 +interface AbstractProductA { + void featureA(); +} +// 具体产品A1 +class ProductA1 implements AbstractProductA { + @Override + public void featureA() { + System.out.println("Feature A1"); + } +} +``` diff --git a/ForJdk17/src/main/java/cn/whaifree/interview/PA/P1.java b/ForJdk17/src/main/java/cn/whaifree/interview/PA/P1.java new file mode 100644 index 0000000..ae9450c --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/interview/PA/P1.java @@ -0,0 +1,79 @@ +package cn.whaifree.interview.PA; + +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/30 17:38 + * @注释 + */ +public class P1 { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + // 注意 hasNext 和 hasNextLine 的区别 + while (in.hasNextInt()) { // 注意 while 处理多个 case + int n = in.nextInt(); + int m = in.nextInt(); + + /** + * 0 1 2 + * 0 0 0 0 + * 1 1 1 1 + * 2 1 2 3 + * 3 1 3 6 + */ + int[] dp = new int[m + 1]; + dp[0] = 1; + for (int i = 0; i < n; i++) { + for (int j = 1; j <= m; j++) { + dp[j] = dp[j] + dp[j - 1]; + } + } + System.out.println(dp[m]); + + + } + } + + + +} + +class p2{ + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + String str = scanner.next(); + + int res = 0; + for (int right = 0; right <= str.length(); right++) { + for (int left = 0; left < right; left++) { + String leftStr = str.substring(0, left); + String rightStr = str.substring(right, str.length()); + String concat = leftStr + rightStr; + if (!concat.isEmpty() && huiwen(concat)) { + System.out.println(concat); + res++; + } + } + } + + System.out.println(res); + + } + + public static boolean huiwen(String str){ + int left = 0; + int right = str.length() - 1; + while (left < right) { + if (str.charAt(left) != str.charAt(right)) { + return false; + } + left++; + right--; + } + return true; + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode002.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode002.java index 574d511..19212a3 100644 --- a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode002.java +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode002.java @@ -1,5 +1,7 @@ package cn.whaifree.leetCode; +import cn.whaifree.leetCode.model.ListNode; + import java.util.ArrayList; /** @@ -109,13 +111,13 @@ public class LeetCode002 { } -class ListNode { - int val; - ListNode next; - ListNode() {} - ListNode(int val) { this.val = val; } - ListNode(int val, ListNode next) { this.val = val; this.next = next; } -} +//class ListNode { +// int val; +// ListNode next; +// ListNode() {} +// ListNode(int val) { this.val = val; } +// ListNode(int val, ListNode next) { this.val = val; this.next = next; } +//} class RefCountGC{ // 这个成员属性的唯一作用就是占用一点内存 diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode148.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode148.java new file mode 100644 index 0000000..b454542 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode148.java @@ -0,0 +1,116 @@ +package cn.whaifree.leetCode; + +import org.junit.Test; +import cn.whaifree.leetCode.model.ListNode; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/29 14:35 + * @注释 + */ +public class LeetCode148 { + + @Test + public void test() { + ListNode listNode = ListNode.listNodeFromArray(new int[]{-1,5,3,4,0}); + ListNode listNode1 = new Solution().sortList(listNode); + listNode1.printList(); + + } + + class Solution { + /** + * 冒泡 + * @param head + * @return + */ + public ListNode sortList(ListNode head) { + ListNode resList = new ListNode(-1); + + ListNode tmpHead = new ListNode(-1, head); + ListNode pre = tmpHead; + + + ListNode index = pre; // 用于对比index.next和index.next的指针 + ListNode markMaxPre = pre; // 用于标记一次循环的最大值 + while (index.next != null) { + // 一次循环找到最大的一个 + while (index.next != null) { + if (markMaxPre.next.val < index.next.val) { + markMaxPre = index; + } + index = index.next; + } + ListNode thisMax = markMaxPre.next; + + markMaxPre.next = markMaxPre.next.next; + addToHead(resList, thisMax); + index = pre; + markMaxPre = pre; + } + + return resList.next; + } + + public void addToHead(ListNode pre, ListNode newNode) { + if (newNode == null) { + return; + } + ListNode next = pre.next; + pre.next = newNode; + newNode.next = next; + } + } + + @Test + public void test2() { + ListNode listNode = ListNode.listNodeFromArray(new int[]{4,2,1,3}); + ListNode listNode1 = new Solution2().sortList(listNode); + listNode1.printList(); + } + + + /** + * 归并排序 + */ + + class Solution2 { + // 我们使用 fast,slow 快慢双指针法,奇数个节点找到中点,偶数个节点找到中心左边的节点。 + public ListNode sortList(ListNode head) { + if (head == null || head.next == null) { + return head; + } + ListNode fast = head.next; // 注意不要一起开始 + ListNode slow = head; + while (fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + } + ListNode BHead = slow.next; + slow.next = null; + ListNode sortAfterA = sortList(head); + ListNode sortAfterB = sortList(BHead); + + return merge(sortAfterA, sortAfterB); + } + + public ListNode merge(ListNode A, ListNode B) { + ListNode dummy = new ListNode(-1); + ListNode cur = dummy; + while (A != null && B != null) { + if (A.val < B.val) { + cur.next = A; + A = A.next; + } else { + cur.next = B; + B = B.next; + } + cur = cur.next; + } + cur.next = A != null ? A : B; + return dummy.next; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode16.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode16.java new file mode 100644 index 0000000..f78d2d6 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode16.java @@ -0,0 +1,51 @@ +package cn.whaifree.leetCode; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/28 14:50 + * @注释 + */ +public class LeetCode16 { + + @Test + public void test() { + int[] nums = {4, 0, 5, -5, 3, 3, 0, -4, -5}; + System.out.println(new Solution().threeSumClosest(nums, -2)); + + } + + class Solution { + public int threeSumClosest(int[] nums, int target) { + Arrays.sort(nums); + int closeV = Integer.MAX_VALUE; + for (int left = 0; left < nums.length; left++) { + int mid = left + 1; + int right = nums.length - 1; + while (mid < right) { + int mv = nums[mid] + nums[left] + nums[right]; + + if (Math.abs(mv - target) < Math.abs(closeV - target)) { + closeV = mv; + } + + if (mv > target) { + right = right - 1; + } else if (mv < target) { + mid = mid + 1; + } else { + return target; + } + } + } + + return closeV; + } + } + + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode43.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode43.java new file mode 100644 index 0000000..5deb3cb --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode43.java @@ -0,0 +1,65 @@ +package cn.whaifree.leetCode; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/28 15:23 + * @注释 + */ +public class LeetCode43 { + + @Test + public void test() { + String num1 = "12"; + String num2 = "12"; + Solution solution = new Solution(); + String result = solution.multiply(num1, num2); + System.out.println(result); + } + + class Solution { + /** + * 12345 + * 678 + * + * + * @param num1 + * @param num2 + * @return + */ + public String multiply(String num1, String num2) { + if (num1.equals("0") || num2.equals("0")) { + return "0"; + } + char[] char1 = num1.toCharArray(); + char[] char2 = num2.toCharArray(); + + int[] ans = new int[char1.length + char2.length]; + + for (int i = char1.length - 1; i >= 0; i--) { + int V1 = char1[i] - '0'; + for (int j = char2.length - 1; j >= 0; j--) { + int V2 = char2[j] - '0'; + int product = V1 * V2; + int sum = ans[i + j + 1] + product; + int pre = sum / 10; + int retail = sum % 10; + ans[i + j + 1] = retail; + ans[i + j] = ans[i + j] + pre; + } + } + + StringBuilder sb=new StringBuilder(); + for (int i = 0; i < ans.length; i++) { + if (i == 0 && ans[i] == 0) { + continue; + } + sb.append(ans[i]); + } + return sb.toString(); + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode740.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode740.java new file mode 100644 index 0000000..b159516 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode740.java @@ -0,0 +1,55 @@ +package cn.whaifree.leetCode; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/30 16:21 + * @注释 + */ +public class LeetCode740 { + @Test + public void test() { + int[] nums = new int[]{2, 2, 3, 3, 3, 4}; + int res = new Solution().deleteAndEarn(nums); + System.out.println(res); + } + + class Solution { + /** + * + * 打家劫舍 + + * @param nums + * @return + */ + public int deleteAndEarn(int[] nums) { + + int max = Arrays.stream(nums).max().getAsInt(); + + Map map = new HashMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + int[] sum = new int[max + 1]; // sum[i]表示选择i进行打劫的收益 + // 每个数字出现的总点数,能获取的总收益 + map.forEach( + (k, v) -> sum[k] = v * k + ); + + int[] dp = new int[max + 1]; + dp[0] = 0; + dp[1] = sum[1]; + for (int i = 2; i <= max; i++) { + // dp[i-1] 不抢,抢 dp[i-2] + 本节点 + dp[i] = Math.max(dp[i - 1], dp[i - 2] + sum[i]); + } + return dp[max]; + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/model/ListNode.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/model/ListNode.java index ca2e6f8..a27f6ca 100644 --- a/ForJdk17/src/main/java/cn/whaifree/leetCode/model/ListNode.java +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/model/ListNode.java @@ -44,12 +44,15 @@ public class ListNode { System.out.println("null!"); } ListNode curr = head; + StringBuilder sb = new StringBuilder(); while (curr != null) { - System.out.print(curr.val + " "); + sb.append(curr.val).append("->"); curr = curr.next; } + System.out.println(sb); } + public void printList() { printList(this); } diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode274.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode274.java new file mode 100644 index 0000000..2f3e8f1 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode274.java @@ -0,0 +1,42 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/29 12:25 + * @注释 + */ +public class LeetCode274 { + + @Test + public void test() { + int[] citations = {2}; + int res = new Solution().hIndex(citations); + System.out.println(res); + } + class Solution { + /** + * 0 1 3 5 6 + * @param citations + * @return + */ + public int hIndex(int[] citations) { + + Arrays.sort(citations); + + + for (int i = 0; i < citations.length; i++) { + if (citations[i] >= citations.length - i) { + return citations.length - i; + } + } + + return 0; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode322.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode322.java new file mode 100644 index 0000000..2cc7c76 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode322.java @@ -0,0 +1,51 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/30 15:12 + * @注释 + */ +public class LeetCode322 { + + @Test + public void test() { + int[] coins = {2, 5}; + int amount = 12; + Solution solution = new Solution(); + int result = solution.coinChange(coins, amount); + System.out.println(result); + } + + class Solution { + /** + * 无穷背包 + * 0 1 2 3 4 5 6 7 8 9 10 11 + * 1 1 1 2 3 4 5 6 7 8 9 10 11 + * 2 1 1 1 2 + * 5 + * + * @param coins + * @param amount + * @return + */ + public int coinChange(int[] coins, int amount) { + int[] dp = new int[amount + 1]; + Arrays.fill(dp, Integer.MAX_VALUE); + dp[0] = 0; + for (int j = 1; j <= amount; j++) { + for (int i = 0; i < coins.length; i++) { + if (j >= coins[i] && dp[j - coins[i]] != Integer.MAX_VALUE) { + dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1); + } + } + } + return dp[amount] == Integer.MAX_VALUE? -1 : dp[amount]; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode33.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode33.java new file mode 100644 index 0000000..8e8e4ed --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode33.java @@ -0,0 +1,80 @@ +package cn.whaifree.redo.redo_all_241016; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/30 14:11 + * @注释 + */ +public class LeetCode33 { + + class Solution { + /** + * + * + * + * @param nums + * @param target + * @return + */ + public int search(int[] nums, int target) { + int left = 0, right = nums.length - 1; + + while (left <= right) { + int mid = (left + right) / 2; + if (nums[mid] == target) { + return mid; + } + if (nums[left] <= nums[mid]) { + // |4 5 6 7 8 | 0 1 + // 目标值在4-6,那么就右边指针mid-1 ,否则不在这个指针,去另一个乱序区间找 + // 【 】mid【 k 】 + if (target < nums[mid] && target >= nums[left]) { + right = mid - 1; + } else { + left = mid + 1; + } + } else { + // 4 5 |0 1 2 3| + if (target > nums[mid] && target <= nums[right]) { + left = mid + 1; + } else { + right = mid - 1; + } + } + } + + return -1; + } + } + + + class Solution1 { + public int search(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + int mid = (left + right) / 2; + if (nums[mid] == target) { + return mid; + } + if (nums[mid] >= nums[left]) { + // | mid k | + if (nums[left] <= target && target < nums[mid]) { + right = mid - 1; + }else { + left = mid + 1; + } + }else { + // | k mid | + if (nums[right] >= target && target > nums[mid]) { + left = mid + 1; + }else { + right = mid - 1; + } + } + } + return -1; + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode377.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode377.java new file mode 100644 index 0000000..d258bde --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode377.java @@ -0,0 +1,59 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/29 14:04 + * @注释 + */ +public class LeetCode377 { + @Test + public void test() { + int[] nums = {1, 2, 3, 4}; + int target = 4; + int result = new Solution().combinationSum4(nums, target); + System.out.println(result); + } + + class Solution { + /** + * 背包容量 target+1 + * 无穷背包 + * + * 0-i 中任选,装满背包的组合数 + * + * 组合数,有排序 + * + * 0 1 2 3 4 5 + * 1 1 1 1 1 1 1 + * 2 1 1 2 2 3 3 dp[j]+dp[j-nums[i]] // 没有排序 + * 3 1 1 2 3 4 5 + * + * @param nums + * @param target + * @return + */ + public int combinationSum4(int[] nums, int target) { + + /** + * 先遍历背包,就可以让之前的背包都获得所有物品放入的可能性, + * + * 计算dp[4]的时候,结果集只有 {1,3} 这样的集合,忽略了{3,1} + */ + + int[] dp = new int[target + 1]; + dp[0] = 1; + for (int j = 1 ; j <= target; j++) { // 有顺序,先遍历背包 + for (int i = 0; i < nums.length; i++) { + if (nums[i] <= j) { + dp[j] = dp[j] + dp[j - nums[i]]; + } + } + } + return dp[target]; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode474.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode474.java new file mode 100644 index 0000000..d1abafa --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode474.java @@ -0,0 +1,61 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/30 15:42 + * @注释 + */ +public class LeetCode474 { + + @Test + public void test() { + String[] strs = {"10", "0001", "1", "0"}; + int m = 5; + int n = 3; + Solution solution = new Solution(); + int result = solution.findMaxForm(strs, m, n); + System.out.println(result); + } + + class Solution { + /** + * + * 从str 0-i中任意取,满足j个0和k个1的最大长度 + * + * @param strs + * @param m + * @param n + * @return + */ + public int findMaxForm(String[] strs, int m, int n) { + int[][] dp = new int[m + 1][n + 1]; + for (int i = 0; i < strs.length; i++) { + String str = strs[i]; + int zeroCount = countZero(str); + int oneCount = str.length() - zeroCount; + for (int j = m; j >= 0; j--) { + for (int k = n; k >= 0; k--) { + if (j >= zeroCount && k >= oneCount) { + dp[j][k] = Math.max(dp[j][k], dp[j - zeroCount][k - oneCount] + 1); + } + } + } + } + return dp[m][n]; + } + + public int countZero(String str) { + int count = 0; + for (int i = 0; i < str.length(); i++) { + if (str.charAt(i) == '0') { + count++; + } + } + return count; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode84.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode84.java new file mode 100644 index 0000000..7068fb8 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode84.java @@ -0,0 +1,98 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/29 11:15 + * @注释 + */ +public class LeetCode84 { + + @Test + public void test() { + int[] heights = {1,1}; + Solution solution = new Solution(); + int i = solution.largestRectangleArea(heights); + System.out.println(i); + } + + class Solution { + /** + * 15 + * + * + * + * @param heights + * @return + */ + public int largestRectangleArea(int[] heights) { + int[] left = new int[heights.length]; + int[] right = new int[heights.length]; + + // 对每个元素找到左边第一个比他小的 + left[0] = -1; + for (int i = 0; i < heights.length; i++) { + int index = i - 1; + while (index >= 0 && heights[index] >= heights[i]) { + index = left[index]; + } + left[i] = index; + } + // 对每个元素找到右边第一个比他小的 + right[heights.length - 1] = heights.length; + for (int i = heights.length - 1; i >= 0; i--) { + int index = i + 1; + while (index <= heights.length - 1 && heights[index] >= heights[i]) { + index = right[index]; + } + right[i] = index; + } + + int maxArea = 0; + for (int i = 0; i < right.length; i++) { + maxArea = Math.max(maxArea, heights[i] * (right[i] - left[i] - 1)); + } + return maxArea; + } + } + + class Solution1 { + /** + * 29 + * + * + * + * @param heights + * @return + */ + public int largestRectangleArea(int[] heights) { + int[] h = new int[heights.length + 2]; + System.arraycopy(heights, 0, h, 1, heights.length); + heights = h; + + Deque stack = new LinkedList<>(); + stack.push(0); + + int res = 0; + for (int i = 1; i < heights.length; i++) { + while (!stack.isEmpty() && heights[i] < heights[stack.peek()]) { + Integer pop = stack.pop(); + if (!stack.isEmpty()) { + int right = i; + int left = pop; + int region = (right - left - 1) * heights[pop]; + res = Math.max(res, region); + } + } + stack.push(i); + } + return res; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/tech/protocolDemo/protocolDemo.java b/ForJdk17/src/main/java/cn/whaifree/tech/protocolDemo/protocolDemo.java new file mode 100644 index 0000000..7372aa3 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/tech/protocolDemo/protocolDemo.java @@ -0,0 +1,10 @@ +package cn.whaifree.tech.protocolDemo; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/28 20:10 + * @注释 + */ +public class protocolDemo { +} diff --git a/ForJdk17/src/main/resources/protocol/PersonProtocol.proto b/ForJdk17/src/main/resources/protocol/PersonProtocol.proto new file mode 100644 index 0000000..3c0c32f --- /dev/null +++ b/ForJdk17/src/main/resources/protocol/PersonProtocol.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +option java_package = "com.example.protobufdemo"; +option java_outer_classname = "PersonProto"; + +message Person { + int32 id = 1; + string name = 2; + string email = 3; +} + + +/* + + + com.google.protobuf + protobuf-java + 3.21.9 + + + */ diff --git a/SpringCloud/ServiceA/pom.xml b/SpringCloud/ServiceA/pom.xml new file mode 100644 index 0000000..07d89bc --- /dev/null +++ b/SpringCloud/ServiceA/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + com.whai.springcloud + SpringCloud + 0.0.1-SNAPSHOT + + + com.whai.springcloud + ServiceA + 0.0.1-SNAPSHOT + ServiceA + ServiceA + + + + + + + + + + + + + + + 17 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/ServiceAApplication.java b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/ServiceAApplication.java new file mode 100644 index 0000000..89ddac0 --- /dev/null +++ b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/ServiceAApplication.java @@ -0,0 +1,17 @@ +package com.whai.springcloud.servicea; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients +public class ServiceAApplication { + + public static void main(String[] args) { + SpringApplication.run(ServiceAApplication.class, args); + } + +} diff --git a/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/config/LoadBalanceConfig.java b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/config/LoadBalanceConfig.java new file mode 100644 index 0000000..14535fc --- /dev/null +++ b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/config/LoadBalanceConfig.java @@ -0,0 +1,13 @@ +package com.whai.springcloud.servicea.config; + +import org.springframework.context.annotation.Configuration; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/29 18:31 + * @注释 + */ +@Configuration +public class LoadBalanceConfig { +} diff --git a/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/config/RestConfig.java b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/config/RestConfig.java new file mode 100644 index 0000000..ef59628 --- /dev/null +++ b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/config/RestConfig.java @@ -0,0 +1,22 @@ +package com.whai.springcloud.servicea.config; + +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/29 18:29 + * @注释 + */ +@Configuration +public class RestConfig { + @Bean + @LoadBalanced + public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) { + return restTemplateBuilder.build(); + } +} diff --git a/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/controller/AProviderController.java b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/controller/AProviderController.java new file mode 100644 index 0000000..d68b100 --- /dev/null +++ b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/controller/AProviderController.java @@ -0,0 +1,43 @@ +package com.whai.springcloud.servicea.controller; + +import com.whai.springcloud.servicea.service.BService; +import jakarta.annotation.Resource; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/29 17:58 + * @注释 + */ +@RestController +public class AProviderController { + + @Resource + private BService bService; + + + @GetMapping("/hello") + public String hello(String msg) { + System.out.println("A---->B"); + return bService.getB(msg); + } + + @Resource + private DiscoveryClient discoveryClient; + @Resource + private RestTemplate restTemplate; + @GetMapping("/getBDiscovey") + public String getBDiscovey(){ + List instances = discoveryClient.getInstances("service-b"); + ServiceInstance serviceInstance = instances.get(0); + return restTemplate.getForObject("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/getB?msg=hello", String.class); + } +} + diff --git a/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/controller/DynamicThreadPoolController.java b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/controller/DynamicThreadPoolController.java new file mode 100644 index 0000000..2f8a908 --- /dev/null +++ b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/controller/DynamicThreadPoolController.java @@ -0,0 +1,96 @@ +package com.whai.springcloud.servicea.controller; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.Resource; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.cloud.context.environment.EnvironmentChangeEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/29 21:51 + * @注释 + */ +@RestController +public class DynamicThreadPoolController { + + static ThreadPoolExecutor executor ; + @Resource + private ConfigurableEnvironment environment; + + @PostConstruct + public void init() { + + + executor = new ThreadPoolExecutor( + Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors() * 4, 10, TimeUnit.SECONDS, + new ArrayBlockingQueue<>(100), + new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + thread.setName("DynamicThreadPoolController"); + return thread; + } + }, + new ThreadPoolExecutor.AbortPolicy() + ); + } + + + @GetMapping("/dynamicCoreSize") + public String dynamicCoreSize() { + return "Dynamic Core Size: " + executor.getCorePoolSize(); + } + +} + +@Configuration +@RefreshScope +@EnableConfigurationProperties(ThreadConfigProperties.class) // 自动引入Properties,会自动加入Context +class DynamicThreadPoolConfig implements ApplicationListener { + + + @Resource + ThreadConfigProperties threadConfigProperties; + + @Override + public void onApplicationEvent(EnvironmentChangeEvent event) { + if (event.getKeys().contains("dynamic.coreSize")) { + // 获取值 + Integer coreSize = threadConfigProperties.getCoreSize(); + // 更新线程池 + DynamicThreadPoolController.executor.setCorePoolSize(coreSize); + } + } +} + +@ConfigurationProperties(prefix = "dynamic") +class ThreadConfigProperties { + int coreSize; + int maxSize; + public int getMaxSize() { + return maxSize; + } + public void setMaxSize(int maxSize) { + this.maxSize = maxSize; + } + public int getCoreSize() { + return coreSize; + } + public void setCoreSize(int coreSize) { + this.coreSize = coreSize; + } +} diff --git a/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/service/BService.java b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/service/BService.java new file mode 100644 index 0000000..8ccd2d2 --- /dev/null +++ b/SpringCloud/ServiceA/src/main/java/com/whai/springcloud/servicea/service/BService.java @@ -0,0 +1,23 @@ +package com.whai.springcloud.servicea.service; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/29 17:58 + * @注释 + */ +@Service +@FeignClient("ServiceB") +public interface BService { + + @GetMapping("/getB") + String getB(@RequestParam("msg") String msg); + + + +} diff --git a/SpringCloud/ServiceA/src/main/resources/application.yaml b/SpringCloud/ServiceA/src/main/resources/application.yaml new file mode 100644 index 0000000..c41bc47 --- /dev/null +++ b/SpringCloud/ServiceA/src/main/resources/application.yaml @@ -0,0 +1,40 @@ +server: + port: 12120 +spring: + application: + name: ServiceA + cloud: + nacos: + username: nacos + password: nacos + discovery: + namespace: 97ff159f-6177-4aab-b735-bd75458949d4 + group: DEFAULT_GROUP + server-addr: localhost:8848 + config: + # 配置所属命名空间的id,我们配置名称为dev的id,在命名空间列表查看id的值 + namespace: 97ff159f-6177-4aab-b735-bd75458949d4 + # 文件名,如果没有配置则默认为 ${spring.application.name} + prefix: springboot3-nacos + # 配置所属分组 + group: DEFAULT_GROUP + # 后缀名,只支持 properties 和 yaml 类型 + file-extension: yaml + # nacos服务器地址 + server-addr: localhost:8848 + # 配置自动刷新 + refresh-enabled: true + + # 启用远程同步配置 + enable-remote-sync-config: true + config: + import: + - optional:nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension} + +# Logger Config +logging: + level: + com.hexadecimal: debug + +hexadecimal: + name: whai diff --git a/SpringCloud/ServiceA/src/test/java/com/whai/springcloud/servicea/ServiceAApplicationTests.java b/SpringCloud/ServiceA/src/test/java/com/whai/springcloud/servicea/ServiceAApplicationTests.java new file mode 100644 index 0000000..a870bf9 --- /dev/null +++ b/SpringCloud/ServiceA/src/test/java/com/whai/springcloud/servicea/ServiceAApplicationTests.java @@ -0,0 +1,13 @@ +package com.whai.springcloud.servicea; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ServiceAApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/SpringCloud/ServiceA/src/test/java/com/whai/springcloud/springcloud/SpringCloudApplicationTests.java b/SpringCloud/ServiceA/src/test/java/com/whai/springcloud/springcloud/SpringCloudApplicationTests.java new file mode 100644 index 0000000..b417cfc --- /dev/null +++ b/SpringCloud/ServiceA/src/test/java/com/whai/springcloud/springcloud/SpringCloudApplicationTests.java @@ -0,0 +1,13 @@ +package com.whai.springcloud.springcloud; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringCloudApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/SpringCloud/ServiceB/pom.xml b/SpringCloud/ServiceB/pom.xml new file mode 100644 index 0000000..3e7e1c3 --- /dev/null +++ b/SpringCloud/ServiceB/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + com.whai.springcloud + SpringCloud + 0.0.1-SNAPSHOT + + + com.whai.springcloud + ServiceB + 0.0.1-SNAPSHOT + ServiceB + ServiceB + + + + + + + + + + + + + + + 17 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/SpringCloud/ServiceB/src/main/java/com/whai/springcloud/serviceb/ServiceBApplication.java b/SpringCloud/ServiceB/src/main/java/com/whai/springcloud/serviceb/ServiceBApplication.java new file mode 100644 index 0000000..d162e10 --- /dev/null +++ b/SpringCloud/ServiceB/src/main/java/com/whai/springcloud/serviceb/ServiceBApplication.java @@ -0,0 +1,17 @@ +package com.whai.springcloud.serviceb; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients +public class ServiceBApplication { + + public static void main(String[] args) { + SpringApplication.run(ServiceBApplication.class, args); + } + +} diff --git a/SpringCloud/ServiceB/src/main/java/com/whai/springcloud/serviceb/controller/BProviderController.java b/SpringCloud/ServiceB/src/main/java/com/whai/springcloud/serviceb/controller/BProviderController.java new file mode 100644 index 0000000..00a369c --- /dev/null +++ b/SpringCloud/ServiceB/src/main/java/com/whai/springcloud/serviceb/controller/BProviderController.java @@ -0,0 +1,31 @@ +package com.whai.springcloud.serviceb.controller; + +import cn.hutool.core.util.StrUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/29 17:58 + * @注释 + */ +@RestController +public class BProviderController { + + private final Environment environment; + + @Autowired + public BProviderController(Environment environment) { + this.environment = environment; + } + // 提供服务 + @RequestMapping("/getB") + public String getB(String msg) { + return StrUtil.format("BProviderController.getB, msg: {}, port: {}", msg, environment.getProperty("server.port")); + } + + +} diff --git a/SpringCloud/ServiceB/src/main/java/com/whai/springcloud/serviceb/service/AService.java b/SpringCloud/ServiceB/src/main/java/com/whai/springcloud/serviceb/service/AService.java new file mode 100644 index 0000000..a5f1c03 --- /dev/null +++ b/SpringCloud/ServiceB/src/main/java/com/whai/springcloud/serviceb/service/AService.java @@ -0,0 +1,10 @@ +package com.whai.springcloud.serviceb.service; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/29 17:58 + * @注释 + */ +public interface AService { +} diff --git a/SpringCloud/ServiceB/src/main/resources/application.yaml b/SpringCloud/ServiceB/src/main/resources/application.yaml new file mode 100644 index 0000000..b2aed2f --- /dev/null +++ b/SpringCloud/ServiceB/src/main/resources/application.yaml @@ -0,0 +1,40 @@ + +spring: + + application: + name: ServiceB + cloud: + nacos: + username: nacos + password: nacos + discovery: + namespace: 97ff159f-6177-4aab-b735-bd75458949d4 + group: DEFAULT_GROUP + server-addr: localhost:8848 + config: + # ?????????id????????dev?id??????????id?? + namespace: 97ff159f-6177-4aab-b735-bd75458949d4 + # ?????????????? ${spring.application.name} + # prefix: springboot3-nacos + # ?????? + group: DEFAULT_GROUP + # ??????? properties ? yaml ?? + file-extension: yaml + # nacos????? + server-addr: localhost:8848 + # ?????? + refresh-enabled: true + # ???????? + enable-remote-sync-config: true + config: + import: + - optional:nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension} + +# Logger Config +logging: + level: + com.hexadecimal: debug + +hexadecimal: + name: whai +# --server.port=12139 diff --git a/SpringCloud/ServiceB/src/test/java/com/whai/springcloud/serviceb/ServiceBApplicationTests.java b/SpringCloud/ServiceB/src/test/java/com/whai/springcloud/serviceb/ServiceBApplicationTests.java new file mode 100644 index 0000000..04e689f --- /dev/null +++ b/SpringCloud/ServiceB/src/test/java/com/whai/springcloud/serviceb/ServiceBApplicationTests.java @@ -0,0 +1,13 @@ +package com.whai.springcloud.serviceb; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ServiceBApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/SpringCloud/ServiceB/src/test/java/com/whai/springcloud/springcloud/SpringCloudApplicationTests.java b/SpringCloud/ServiceB/src/test/java/com/whai/springcloud/springcloud/SpringCloudApplicationTests.java new file mode 100644 index 0000000..b417cfc --- /dev/null +++ b/SpringCloud/ServiceB/src/test/java/com/whai/springcloud/springcloud/SpringCloudApplicationTests.java @@ -0,0 +1,13 @@ +package com.whai.springcloud.springcloud; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringCloudApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/SpringCloud/pom.xml b/SpringCloud/pom.xml new file mode 100644 index 0000000..6584809 --- /dev/null +++ b/SpringCloud/pom.xml @@ -0,0 +1,103 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.5 + + + com.whai.springcloud + SpringCloud + 0.0.1-SNAPSHOT + SpringCloud + SpringCloud + + + + + + + + + + + + + + + 17 + 2023.0.3 + + + + + cn.hutool + hutool-all + 5.8.14 + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + 2023.0.1.3 + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + 2023.0.1.3 + + + + + + + + + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/springDemo/.gitignore b/SpringDemo/.gitignore similarity index 100% rename from springDemo/.gitignore rename to SpringDemo/.gitignore diff --git a/springDemo/mvnw b/SpringDemo/mvnw similarity index 100% rename from springDemo/mvnw rename to SpringDemo/mvnw diff --git a/springDemo/mvnw.cmd b/SpringDemo/mvnw.cmd similarity index 100% rename from springDemo/mvnw.cmd rename to SpringDemo/mvnw.cmd diff --git a/springDemo/pom.xml b/SpringDemo/pom.xml similarity index 98% rename from springDemo/pom.xml rename to SpringDemo/pom.xml index 729d4fc..beca947 100644 --- a/springDemo/pom.xml +++ b/SpringDemo/pom.xml @@ -9,10 +9,10 @@ cn.whaifree - springDemo + SpringDemo 0.0.1-SNAPSHOT - springDemo - springDemo + SpringDemo + SpringDemo diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/SpringDemoApplication.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/SpringDemoApplication.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/SpringDemoApplication.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/SpringDemoApplication.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/aspect/RateLimitAspect.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/aspect/RateLimitAspect.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/aspect/RateLimitAspect.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/aspect/RateLimitAspect.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/aspect/annotation/RateLimiter.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/aspect/annotation/RateLimiter.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/aspect/annotation/RateLimiter.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/aspect/annotation/RateLimiter.java diff --git a/SpringDemo/src/main/java/cn/whaifree/springdemo/config/CacheConfig.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/config/CacheConfig.java new file mode 100644 index 0000000..efe5369 --- /dev/null +++ b/SpringDemo/src/main/java/cn/whaifree/springdemo/config/CacheConfig.java @@ -0,0 +1,44 @@ +//package cn.whaifree.springdemo.config; +// +//import org.springframework.cache.Cache; +//import org.springframework.cache.CacheManager; +//import org.springframework.cache.annotation.EnableCaching; +//import org.springframework.cache.concurrent.ConcurrentMapCache; +//import org.springframework.cache.transaction.TransactionAwareCacheDecorator; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +//import org.springframework.transaction.annotation.EnableTransactionManagement; +// +//import java.util.Arrays; +//import java.util.Collection; +// +///** +// * @version 1.0 +// * @Author whai文海 +// * @Date 2024/10/25 22:44 +// * @注释 +// */ +//@Configuration +//@EnableCaching() +//@EnableTransactionManagement() +//public class CacheConfig { +// @Bean +// public CacheManager cacheManager() { +// return new CustomCacheManager(); +// } +// +// // 自定义的 CacheManager,装饰每个缓存为 TransactionAwareCacheDecorator +// static class CustomCacheManager implements CacheManager { +// @Override +// public Cache getCache(String name) { +// Cache cache = new ConcurrentMapCache(name); // 使用 ConcurrentMapCache 实现缓存 +// return new TransactionAwareCacheDecorator(cache); // 包装为 TransactionAwareCacheDecorator +// } +// +// @Override +// public Collection getCacheNames() { +// return Arrays.asList("myCache"); // 设定缓存的名称 +// } +// } +// +//} diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/config/FilterConfig.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/config/FilterConfig.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/config/FilterConfig.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/config/FilterConfig.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/config/GlobalExceptionHandler.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/config/GlobalExceptionHandler.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/config/GlobalExceptionHandler.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/config/GlobalExceptionHandler.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/config/MyAutoConfiguration.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/config/MyAutoConfiguration.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/config/MyAutoConfiguration.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/config/MyAutoConfiguration.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/config/RedisConfig.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/config/RedisConfig.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/config/RedisConfig.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/config/RedisConfig.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/config/RedissonConfig.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/config/RedissonConfig.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/config/RedissonConfig.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/config/RedissonConfig.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/constant/CacheConstants.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/constant/CacheConstants.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/constant/CacheConstants.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/constant/CacheConstants.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/constant/LimitType.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/constant/LimitType.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/constant/LimitType.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/constant/LimitType.java diff --git a/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/CacheDecoratorDemo/CacheDecoratorController.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/CacheDecoratorDemo/CacheDecoratorController.java new file mode 100644 index 0000000..9ed5299 --- /dev/null +++ b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/CacheDecoratorDemo/CacheDecoratorController.java @@ -0,0 +1,67 @@ +//package cn.whaifree.springdemo.controller.CacheDecoratorDemo; +// +//import cn.hutool.extra.spring.SpringUtil; +//import jakarta.annotation.Resource; +//import org.springframework.cache.Cache; +//import org.springframework.cache.annotation.CachePut; +//import org.springframework.cache.annotation.Cacheable; +//import org.springframework.data.redis.cache.RedisCacheManager; +//import org.springframework.transaction.annotation.Transactional; +//import org.springframework.web.bind.annotation.RequestMapping; +//import org.springframework.web.bind.annotation.RestController; +// +///** +// * @version 1.0 +// * @Author whai文海 +// * @Date 2024/10/25 22:43 +// * @注释 +// */ +//@RestController +//public class CacheDecoratorController { +// +// @Resource +// private final RedisCacheManager cacheManager; +// +// +// public CacheDecoratorController(RedisCacheManager cacheManager) { +// this.cacheManager = cacheManager; +// } +// +// @RequestMapping("/getData") +// public String getData(Long id) { +// // 先从缓存中获取数据 +// String data = SpringUtil.getBean(CacheDecoratorController.class).getDataById(id); +// return data; +// } +// +// @RequestMapping("/update") +// public void update(Long id, String newValue) { +// // 更新数据 +// SpringUtil.getBean(CacheDecoratorController.class).updateData(id, newValue); +// } +// +// @Cacheable(value = "myCache", key = "#id") +// public String getDataById(Long id) { +// // 模拟获取数据操作 +// return "Data for ID " + id; +// } +// +// @Transactional +// @CachePut(value = "myCache", key = "#id", condition = "#result != null") +// public String updateData(Long id, String newValue) { +// // 更新数据库操作 +// // ... +// // 返回的新值会在事务提交后自动更新到缓存中 +// // return newValue; +// +// +// // 或者手动操作缓存 +// Cache myCache = cacheManager.getCache("myCache"); +// if (myCache != null) { +// myCache.put(id, newValue); +// return null; +// } +// return null; +// } +// +//} diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/controller/SSE/SSEEmitter.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/SSE/SSEEmitter.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/controller/SSE/SSEEmitter.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/controller/SSE/SSEEmitter.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/controller/TestController.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/TestController.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/controller/TestController.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/controller/TestController.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/controller/WhiteListController.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/WhiteListController.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/controller/WhiteListController.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/controller/WhiteListController.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/controller/cacheComparator/CacheComparatorDemo.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/cacheComparator/CacheComparatorDemo.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/controller/cacheComparator/CacheComparatorDemo.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/controller/cacheComparator/CacheComparatorDemo.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/controller/idempotence/IdempotenceController.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/idempotence/IdempotenceController.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/controller/idempotence/IdempotenceController.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/controller/idempotence/IdempotenceController.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/controller/minio/MinioController.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/minio/MinioController.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/controller/minio/MinioController.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/controller/minio/MinioController.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/controller/rabbitMqEvent/EventController.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/rabbitMqEvent/EventController.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/controller/rabbitMqEvent/EventController.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/controller/rabbitMqEvent/EventController.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/controller/rabbitMqEvent/RabbitMQController.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/rabbitMqEvent/RabbitMQController.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/controller/rabbitMqEvent/RabbitMQController.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/controller/rabbitMqEvent/RabbitMQController.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/controller/redEnvelope/RedEnvelopeController.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/redEnvelope/RedEnvelopeController.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/controller/redEnvelope/RedEnvelopeController.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/controller/redEnvelope/RedEnvelopeController.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/controller/wxQrLogin/WxQrLoginController.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/wxQrLogin/WxQrLoginController.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/controller/wxQrLogin/WxQrLoginController.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/controller/wxQrLogin/WxQrLoginController.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/entity/UserService.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/entity/UserService.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/entity/UserService.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/entity/UserService.java diff --git a/SpringDemo/src/main/java/cn/whaifree/springdemo/utils/BroomFilter/BroomFilter.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/utils/BroomFilter/BroomFilter.java new file mode 100644 index 0000000..1a07b40 --- /dev/null +++ b/SpringDemo/src/main/java/cn/whaifree/springdemo/utils/BroomFilter/BroomFilter.java @@ -0,0 +1,57 @@ +package cn.whaifree.springdemo.utils.BroomFilter; + +import cn.hutool.extra.spring.SpringUtil; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/30 21:01 + * @注释 + */ +@Component +public class BroomFilter{ + RedisTemplate redisTemplate = SpringUtil.getBean("redisTemplate", RedisTemplate.class); + + private int hashCount; // 哈希函数数量 + private int size; // 位图大小 + private String key; // Redis 中的 Bitmap 键 + + public BroomFilter() { + this.hashCount = 10; + this.size = 1000000; + this.key = "broomFilter"; + } + + public boolean contains(Object o) { + // 计算Hashcode + int[] hash = getHash(o); + for (int i : hash) { + // 检查位图中对应位置是否为true + if (!redisTemplate.opsForValue().getBit(key, i)) { + return false; + } + } + return true; + } + + public void addToBroom(Object o) { + + // 计算Hashcode + int[] hash = getHash(o); + for (int i : hash) { + // 设置位图中对应位置为true + redisTemplate.opsForValue().setBit(key, i, true); + } + } + + // 生成哈希值 + private int[] getHash(Object value) { + int[] result = new int[hashCount]; + for (int i = 0; i < hashCount; i++) { + result[i] = Math.abs(value.hashCode() + i * 123456) % size; + } + return result; + } +} diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/utils/Filter/SelfFilter.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/utils/Filter/SelfFilter.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/utils/Filter/SelfFilter.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/utils/Filter/SelfFilter.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/utils/ResVo.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/utils/ResVo.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/utils/ResVo.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/utils/ResVo.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/utils/constants/HttpStatus.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/utils/constants/HttpStatus.java similarity index 100% rename from springDemo/src/main/java/cn/whaifree/springdemo/utils/constants/HttpStatus.java rename to SpringDemo/src/main/java/cn/whaifree/springdemo/utils/constants/HttpStatus.java diff --git a/springDemo/src/main/resources/META-INF/spring.factories b/SpringDemo/src/main/resources/META-INF/spring.factories similarity index 100% rename from springDemo/src/main/resources/META-INF/spring.factories rename to SpringDemo/src/main/resources/META-INF/spring.factories diff --git a/springDemo/src/main/resources/application.yaml b/SpringDemo/src/main/resources/application.yaml similarity index 100% rename from springDemo/src/main/resources/application.yaml rename to SpringDemo/src/main/resources/application.yaml diff --git a/springDemo/src/main/resources/lua/RobEnvelope.lua b/SpringDemo/src/main/resources/lua/RobEnvelope.lua similarity index 100% rename from springDemo/src/main/resources/lua/RobEnvelope.lua rename to SpringDemo/src/main/resources/lua/RobEnvelope.lua diff --git a/springDemo/src/main/resources/redisson.yaml b/SpringDemo/src/main/resources/redisson.yaml similarity index 100% rename from springDemo/src/main/resources/redisson.yaml rename to SpringDemo/src/main/resources/redisson.yaml diff --git a/springDemo/src/test/java/cn/whaifree/springdemo/RedisData/RedisDataTest.java b/SpringDemo/src/test/java/cn/whaifree/springdemo/RedisData/RedisDataTest.java similarity index 83% rename from springDemo/src/test/java/cn/whaifree/springdemo/RedisData/RedisDataTest.java rename to SpringDemo/src/test/java/cn/whaifree/springdemo/RedisData/RedisDataTest.java index a801fd2..01dd1fc 100644 --- a/springDemo/src/test/java/cn/whaifree/springdemo/RedisData/RedisDataTest.java +++ b/SpringDemo/src/test/java/cn/whaifree/springdemo/RedisData/RedisDataTest.java @@ -1,8 +1,11 @@ package cn.whaifree.springdemo.RedisData; +import cn.hutool.extra.spring.SpringUtil; +import cn.whaifree.springdemo.utils.BroomFilter.BroomFilter; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.geo.*; import org.springframework.data.redis.connection.BitFieldSubCommands; @@ -23,6 +26,8 @@ public class RedisDataTest { @Resource private RedisTemplate redisTemplate; + @Autowired + private SpringUtil springUtil; @Test public void testHyperloglog() { @@ -68,8 +73,6 @@ public class RedisDataTest { - - // 创建BitFieldSubCommands对象,用于执行更复杂的位图操作 BitFieldSubCommands subCommands = BitFieldSubCommands.create(); // 添加一个指令到subCommands中,获取第11个位(从0开始计数)的64位整型值 @@ -114,4 +117,30 @@ public class RedisDataTest { } } + @Test + public void testbitMap2() { + + BroomFilter broomFilter = new BroomFilter(); + broomFilter.addToBroom("obj1"); + broomFilter.addToBroom("obj2"); + broomFilter.addToBroom("obj3"); + broomFilter.addToBroom("obj4"); + broomFilter.addToBroom("obj5"); + broomFilter.addToBroom("obj6"); + broomFilter.addToBroom("obj7"); + broomFilter.addToBroom("obj8"); + broomFilter.addToBroom("obj9"); + broomFilter.addToBroom("obj10"); + broomFilter.addToBroom("obj11"); + broomFilter.addToBroom("obj12"); + broomFilter.addToBroom("obj13"); + broomFilter.addToBroom("obj14"); + broomFilter.addToBroom("obj15"); + System.out.println(broomFilter.contains("obj1")); + System.out.println(broomFilter.contains("obj16")); + } + + } + + diff --git a/springDemo/src/test/java/cn/whaifree/springdemo/SpringDemoApplicationTests.java b/SpringDemo/src/test/java/cn/whaifree/springdemo/SpringDemoApplicationTests.java similarity index 100% rename from springDemo/src/test/java/cn/whaifree/springdemo/SpringDemoApplicationTests.java rename to SpringDemo/src/test/java/cn/whaifree/springdemo/SpringDemoApplicationTests.java diff --git a/springDemo/src/test/java/cn/whaifree/springdemo/tech/AbstractClass.java b/SpringDemo/src/test/java/cn/whaifree/springdemo/tech/AbstractClass.java similarity index 100% rename from springDemo/src/test/java/cn/whaifree/springdemo/tech/AbstractClass.java rename to SpringDemo/src/test/java/cn/whaifree/springdemo/tech/AbstractClass.java diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/config/CacheConfig.java b/springDemo/src/main/java/cn/whaifree/springdemo/config/CacheConfig.java deleted file mode 100644 index d58687c..0000000 --- a/springDemo/src/main/java/cn/whaifree/springdemo/config/CacheConfig.java +++ /dev/null @@ -1,43 +0,0 @@ -package cn.whaifree.springdemo.config; - -import org.springframework.cache.Cache; -import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.concurrent.ConcurrentMapCache; -import org.springframework.cache.transaction.TransactionAwareCacheDecorator; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -import java.util.Arrays; -import java.util.Collection; - -/** - * @version 1.0 - * @Author whai文海 - * @Date 2024/10/25 22:44 - * @注释 - */ -@Configuration -@EnableCaching() -@EnableTransactionManagement() -public class CacheConfig { - @Bean - public CacheManager cacheManager() { - return new CustomCacheManager(); - } - // 自定义的 CacheManager,装饰每个缓存为 TransactionAwareCacheDecorator - static class CustomCacheManager implements CacheManager { - @Override - public Cache getCache(String name) { - Cache cache = new ConcurrentMapCache(name); // 使用 ConcurrentMapCache 实现缓存 - return new TransactionAwareCacheDecorator(cache); // 包装为 TransactionAwareCacheDecorator - } - - @Override - public Collection getCacheNames() { - return Arrays.asList("myCache"); // 设定缓存的名称 - } - } - -} diff --git a/springDemo/src/main/java/cn/whaifree/springdemo/controller/CacheDecoratorDemo/CacheDecoratorController.java b/springDemo/src/main/java/cn/whaifree/springdemo/controller/CacheDecoratorDemo/CacheDecoratorController.java deleted file mode 100644 index ec2407d..0000000 --- a/springDemo/src/main/java/cn/whaifree/springdemo/controller/CacheDecoratorDemo/CacheDecoratorController.java +++ /dev/null @@ -1,66 +0,0 @@ -package cn.whaifree.springdemo.controller.CacheDecoratorDemo; - -import cn.hutool.extra.spring.SpringUtil; -import org.springframework.cache.Cache; -import org.springframework.cache.annotation.CachePut; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.data.redis.cache.RedisCacheManager; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * @version 1.0 - * @Author whai文海 - * @Date 2024/10/25 22:43 - * @注释 - */ -@RestController -public class CacheDecoratorController { - - private final SpringUtil springUtil; - private final RedisCacheManager cacheManager; - - public CacheDecoratorController(SpringUtil springUtil, RedisCacheManager cacheManager) { - this.springUtil = springUtil; - this.cacheManager = cacheManager; - } - - @RequestMapping("/getData") - public String getData(Long id) { - // 先从缓存中获取数据 - String data = SpringUtil.getBean(CacheDecoratorController.class).getDataById(id); - return data; - } - - @RequestMapping("/update") - public void update(Long id, String newValue) { - // 更新数据 - SpringUtil.getBean(CacheDecoratorController.class).updateData(id, newValue); - } - - @Cacheable(value = "myCache", key = "#id") - public String getDataById(Long id) { - // 模拟获取数据操作 - return "Data for ID " + id; - } - - @Transactional - @CachePut(value = "myCache", key = "#id", condition = "#result != null") - public String updateData(Long id, String newValue) { - // 更新数据库操作 - // ... - // 返回的新值会在事务提交后自动更新到缓存中 - // return newValue; - - - // 或者手动操作缓存 - Cache myCache = cacheManager.getCache("myCache"); - if (myCache != null) { - myCache.put(id, newValue); - return null; - } - return null; - } - -}