8 changed files with 181 additions and 0 deletions
@ -0,0 +1,59 @@ |
|||||
|
package w8.W88; |
||||
|
|
||||
|
import java.util.concurrent.ConcurrentHashMap; |
||||
|
|
||||
|
public class Cache<K, V> { |
||||
|
private ConcurrentHashMap<K, V> 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); |
||||
|
} |
||||
|
} |
||||
@ -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<String> list = new ArrayList<String>() {}; |
||||
|
|
||||
|
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<String> 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<Integer> 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(); |
||||
|
} |
||||
|
} |
||||
@ -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<String, Integer> pair = new Pair<>("age", 25); |
||||
|
System.out.println("Original: " + pair); |
||||
|
System.out.println("Swap: " + pair.swap()); |
||||
|
|
||||
|
System.out.println("\n=== Cache Test ==="); |
||||
|
Cache<String, String> 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()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
package w8.W88; |
||||
|
|
||||
|
public class Pair<K, V> { |
||||
|
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<V, K> swap() { |
||||
|
return new Pair<>(value, key); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return "Pair{" + key + ", " + value + "}"; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue