From cd17da4056b81f292f3d0d200508fa6942d4d75b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SALAH=20ABDULLAH=E9=98=BF=E5=B1=B1?= <2040abdullah@gmail.com> Date: Sun, 31 May 2026 03:28:30 +0800 Subject: [PATCH] w88 --- src/W88/Cache.java | 0 src/W88/GenericReflectionExample.java | 0 src/W88/Main.java | 0 src/W88/Pair.java | 0 src/w8/W88/Cache.java | 59 ++++++++++++++++++++++++ src/w8/W88/GenericReflectionExample.java | 53 +++++++++++++++++++++ src/w8/W88/Main.java | 33 +++++++++++++ src/w8/W88/Pair.java | 36 +++++++++++++++ 8 files changed, 181 insertions(+) delete mode 100644 src/W88/Cache.java delete mode 100644 src/W88/GenericReflectionExample.java delete mode 100644 src/W88/Main.java delete mode 100644 src/W88/Pair.java diff --git a/src/W88/Cache.java b/src/W88/Cache.java deleted file mode 100644 index e69de29..0000000 diff --git a/src/W88/GenericReflectionExample.java b/src/W88/GenericReflectionExample.java deleted file mode 100644 index e69de29..0000000 diff --git a/src/W88/Main.java b/src/W88/Main.java deleted file mode 100644 index e69de29..0000000 diff --git a/src/W88/Pair.java b/src/W88/Pair.java deleted file mode 100644 index e69de29..0000000 diff --git a/src/w8/W88/Cache.java b/src/w8/W88/Cache.java index e69de29..9aef456 100644 --- a/src/w8/W88/Cache.java +++ b/src/w8/W88/Cache.java @@ -0,0 +1,59 @@ +package w8.W88; + +import java.util.concurrent.ConcurrentHashMap; + +public class Cache { + private ConcurrentHashMap cache; + private long defaultTTL; + + public Cache() { + this.cache = new ConcurrentHashMap<>(); + this.defaultTTL = 60000; + } + + public Cache(long ttlMillis) { + this.cache = new ConcurrentHashMap<>(); + this.defaultTTL = ttlMillis; + } + + public void put(K key, V value) { + if (key == null) { + System.out.println("Error: Key cannot be null"); + return; + } + cache.put(key, value); + System.out.println("Cache put: " + key + " = " + value); + } + + public V get(K key) { + if (key == null) { + System.out.println("Error: Key cannot be null"); + return null; + } + V value = cache.get(key); + if (value == null) { + System.out.println("Cache miss: " + key + " not found"); + return null; + } + System.out.println("Cache hit: " + key + " = " + value); + return value; + } + + public void remove(K key) { + cache.remove(key); + System.out.println("Cache removed: " + key); + } + + public void clear() { + cache.clear(); + System.out.println("Cache cleared"); + } + + public int size() { + return cache.size(); + } + + public boolean containsKey(K key) { + return cache.containsKey(key); + } +} \ No newline at end of file diff --git a/src/w8/W88/GenericReflectionExample.java b/src/w8/W88/GenericReflectionExample.java index e69de29..eec22ba 100644 --- a/src/w8/W88/GenericReflectionExample.java +++ b/src/w8/W88/GenericReflectionExample.java @@ -0,0 +1,53 @@ +package w8.W88; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +public class GenericReflectionExample { + + public static void getGenericInfo() { + List list = new ArrayList() {}; + + Type type = list.getClass().getGenericSuperclass(); + if (type instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) type; + Type[] actualTypes = pt.getActualTypeArguments(); + for (Type t : actualTypes) { + System.out.println("Generic type: " + t.getTypeName()); + } + } + } + + public static void getFieldGenericInfo() throws NoSuchFieldException { + class MyClass { + public List names; + } + + Field field = MyClass.class.getField("names"); + ParameterizedType type = (ParameterizedType) field.getGenericType(); + Type[] types = type.getActualTypeArguments(); + System.out.println("Field generic type: " + types[0].getTypeName()); + } + + public static void getMethodGenericInfo() throws NoSuchMethodException { + class MyClass { + public void process(List numbers) {} + } + + Method method = MyClass.class.getMethod("process", List.class); + Type[] paramTypes = method.getGenericParameterTypes(); + ParameterizedType pt = (ParameterizedType) paramTypes[0]; + System.out.println("Method parameter generic: " + pt.getActualTypeArguments()[0]); + } + + public static void main(String[] args) throws Exception { + System.out.println("=== Reflection Generic Info ==="); + getGenericInfo(); + getFieldGenericInfo(); + getMethodGenericInfo(); + } +} \ No newline at end of file diff --git a/src/w8/W88/Main.java b/src/w8/W88/Main.java index e69de29..6c30888 100644 --- a/src/w8/W88/Main.java +++ b/src/w8/W88/Main.java @@ -0,0 +1,33 @@ +package w8.W88; + +import w8.Cache; +import w8.Pair; + +public class Main { + public static void main(String[] args) { + System.out.println("=== Pair Test ==="); + Pair pair = new Pair<>("age", 25); + System.out.println("Original: " + pair); + System.out.println("Swap: " + pair.swap()); + + System.out.println("\n=== Cache Test ==="); + Cache cache = new Cache<>(3000); + cache.put("name", "Ahmed"); + cache.put("city", "Cairo"); + + System.out.println("\n=== Get Values ==="); + System.out.println("Get name: " + cache.get("name")); + System.out.println("Get city: " + cache.get("city")); + System.out.println("Get country: " + cache.get("country")); + + System.out.println("\n=== Remove ==="); + cache.remove("city"); + System.out.println("Get city after remove: " + cache.get("city")); + + System.out.println("\n=== Size ==="); + System.out.println("Cache size: " + cache.size()); + + cache.clear(); + System.out.println("After clear, size: " + cache.size()); + } +} \ No newline at end of file diff --git a/src/w8/W88/Pair.java b/src/w8/W88/Pair.java index e69de29..2f4dc6a 100644 --- a/src/w8/W88/Pair.java +++ b/src/w8/W88/Pair.java @@ -0,0 +1,36 @@ +package w8.W88; + +public class Pair { + private K key; + private V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + public void setKey(K key) { + this.key = key; + } + + public void setValue(V value) { + this.value = value; + } + + public Pair swap() { + return new Pair<>(value, key); + } + + @Override + public String toString() { + return "Pair{" + key + ", " + value + "}"; + } +} \ No newline at end of file