Skip to content

Commit 4927c93

Browse files
committed
more book ideas
1 parent b47bd35 commit 4927c93

File tree

10 files changed

+502
-1
lines changed

10 files changed

+502
-1
lines changed

.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="src" path="resources"/>
5-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
66
<classpathentry kind="lib" path="libs/commons-codec-1.7.jar"/>
77
<classpathentry kind="output" path="bin"/>
88
</classpath>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package benblack86.collection;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collection;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
9+
public class MergeCollections {
10+
public static void main(String[] args) {
11+
Collection<Double> list1 = Arrays.asList(1.0, 5.0, 10.0);
12+
Collection<Double> list2 = Arrays.asList(7.0, 8.0, 100.0, 101.0);
13+
14+
System.out.println(MergeCollections.merge(list1, list2));
15+
16+
}
17+
18+
19+
public static <T extends Comparable<? super T>> List<T> merge(Collection<? extends T> c1, Collection<? extends T> c2) {
20+
List<T> mergedList = new ArrayList<T>();
21+
Iterator<? extends T> itr1 = c1.iterator();
22+
Iterator<? extends T> itr2 = c2.iterator();
23+
T element1 = getNextElement(itr1);
24+
T element2 = getNextElement(itr2);
25+
26+
while (element1 != null || element2 != null) {
27+
boolean useElement1 = (element2 == null || element1 != null && element1.compareTo(element2) < 0);
28+
29+
if(useElement1) {
30+
mergedList.add(element1);
31+
element1 = getNextElement(itr1);
32+
} else {
33+
mergedList.add(element2);
34+
element2 = getNextElement(itr2);
35+
}
36+
}
37+
38+
return mergedList;
39+
}
40+
41+
private static <E> E getNextElement(Iterator<E> itr) {
42+
if (itr.hasNext()) {
43+
E element = itr.next();
44+
45+
if (element == null) {
46+
throw new NullPointerException();
47+
}
48+
49+
return element;
50+
}
51+
52+
return null;
53+
}
54+
}

src/benblack86/java/Reflection.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package benblack86.java;
2+
3+
import java.lang.reflect.Array;
4+
import java.lang.reflect.InvocationTargetException;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
9+
public class Reflection {
10+
11+
public static void main(String[] args) {
12+
try {
13+
System.out.println(GenericReflection.newInstance(new String()));
14+
15+
System.out.println(GenericReflection.getComponentType(new String[0]));
16+
17+
System.out.println(Arrays.toString(GenericReflection.newArray(String.class, 10)));
18+
19+
System.out.println(Arrays.toString(GenericReflection.newArray(new Integer[0], 100)));
20+
21+
Collection<String> items = new ArrayList<String>();
22+
items.add("hi");
23+
Collection<String> items2 = Reflection.copy(items);
24+
items.add("bye");
25+
System.out.println(items2);
26+
27+
} catch (Throwable t) {
28+
t.printStackTrace();
29+
}
30+
}
31+
32+
public static <T, C extends Collection<T>> C copy(C collection) throws Exception {
33+
// GenericReflection class hides the unchecked cast
34+
C copy = GenericReflection.newInstance(collection);
35+
copy.addAll(collection);
36+
return copy;
37+
}
38+
39+
}
40+
41+
/*
42+
* The idea is to encapsulate unchecked casts into a number of library methods
43+
*/
44+
class GenericReflection {
45+
@SuppressWarnings("unchecked")
46+
public static <T> T newInstance(T obj) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException {
47+
// can only handle zero-argument constructors
48+
Object newObj = obj.getClass().getConstructor(new Class<?>[]{}).newInstance();
49+
return (T)newObj; // unchecked cast
50+
}
51+
52+
@SuppressWarnings("unchecked")
53+
public static <T> Class<? extends T> getComponentType(T[] a) {
54+
Class<?> k = a.getClass().getComponentType();
55+
return (Class<? extends T>)k; // unchecked cast
56+
}
57+
58+
@SuppressWarnings("unchecked")
59+
public static <T> T[] newArray(Class<? extends T> k, int size) {
60+
if (k.isPrimitive()) {
61+
throw new IllegalArgumentException("Argument cannot be primitive");
62+
}
63+
64+
Object a = Array.newInstance(k, size);
65+
return (T[])a; // unchecked cast
66+
}
67+
68+
public static <T> T[] newArray(T[] a, int size) {
69+
return GenericReflection.newArray(GenericReflection.getComponentType(a), size);
70+
}
71+
}

src/benblack86/java/Reification.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package benblack86.java;
2+
3+
import java.lang.reflect.Array;
4+
import java.util.Arrays;
5+
import java.util.Collection;
6+
import java.util.List;
7+
8+
public class Reification {
9+
10+
/**
11+
* @param args
12+
*/
13+
public static void main(String[] args) {
14+
Collection<String> collection = Arrays.asList("hi", "there");
15+
List<String> list = Reification.asList(collection);
16+
System.out.println(list);
17+
18+
List<Object> objects = Arrays.<Object>asList("three", "four");
19+
List<String> strings = Reification.promote(objects);
20+
System.out.println(strings);
21+
22+
String[] stringArray = Reification.toArray(strings, new String[0]);
23+
System.out.println(Arrays.toString(stringArray));
24+
25+
String[] stringArray2 = Reification.toArray(strings, String.class);
26+
System.out.println(Arrays.toString(stringArray2));
27+
}
28+
29+
public static <T> List<T> asList(Collection<T> collection) {
30+
// cannot do List<T> as information is erased
31+
if (collection instanceof List<?>) {
32+
// can cast using T as the argument expects T
33+
return (List<T>) collection;
34+
} else {
35+
throw new IllegalArgumentException("Argument not a list");
36+
}
37+
}
38+
39+
@SuppressWarnings("unchecked")
40+
public static List<String> promote(List<Object> objs) {
41+
for (Object obj : objs) {
42+
if (!(obj instanceof String)) {
43+
throw new ClassCastException();
44+
}
45+
}
46+
47+
return (List<String>)(List<?>)objs; // unchecked cast
48+
}
49+
50+
@SuppressWarnings("unchecked")
51+
public static <T> T[] toArray(Collection<T> collection, T[] a) {
52+
if (a.length < collection.size()) {
53+
a = (T[])Array.newInstance(a.getClass().getComponentType(), collection.size()); // unchecked cast
54+
}
55+
56+
int i = 0;
57+
for(T element : collection) {
58+
a[i++] = element;
59+
}
60+
61+
return a;
62+
}
63+
64+
@SuppressWarnings("unchecked")
65+
public static <T> T[] toArray(Collection<T> collection, Class<T> c) {
66+
T[] a = (T[])Array.newInstance(c, collection.size()); // unchecked cast
67+
68+
int i = 0;
69+
for(T element : collection) {
70+
a[i++] = element;
71+
}
72+
73+
return a;
74+
}
75+
}

src/benblack86/java/Varargs.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static void main(String[] args) {
1111
System.out.println(Varargs.toList(new Object[]{}));
1212
}
1313

14+
@SafeVarargs
1415
public static <T> List<T> toList(T... array) {
1516
List<T> list = new ArrayList<T>();
1617

src/benblack86/java/Wildcards.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Wildcards {
1313
*
1414
*/
1515
public static void main(String[] args) {
16+
// cannot use Arrays.asList as it returns a structure that can not be added to
1617
List<Number> items = new ArrayList<Number>();
1718
items.add(3);
1819
items.add(8.0);

src/benblack86/pattern/Functions.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package benblack86.pattern;
2+
3+
import java.lang.reflect.Method;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
public class Functions {
9+
10+
/*
11+
* Function pattern: converts arbitrary method into an object
12+
*/
13+
14+
public static void main(String[] args) {
15+
Function<String, Integer, Error> length = new Function<String, Integer, Error>() {
16+
public Integer apply(String s) {
17+
return s.length();
18+
}
19+
};
20+
21+
Function<String, Class<?>, ClassNotFoundException> forName = new Function<String, Class<?>, ClassNotFoundException>() {
22+
public Class<?> apply(String s) throws ClassNotFoundException {
23+
return Class.forName(s);
24+
}
25+
};
26+
27+
Function<String, Method, Exception> getRunMethod = new Function<String, Method, Exception>() {
28+
public Method apply(String s) throws ClassNotFoundException, NoSuchMethodException {
29+
return Class.forName(s).getMethod("run");
30+
}
31+
};
32+
33+
try {
34+
List<String> strings = Arrays.asList("java.lang.Thread", "java.lang.Runnable");
35+
36+
System.out.println(applyAll(length, strings));
37+
38+
System.out.println(applyAll(forName, strings));
39+
40+
System.out.println(applyAll(getRunMethod, strings));
41+
42+
} catch(Throwable t) {
43+
t.printStackTrace();
44+
}
45+
}
46+
47+
public static <A, B, X extends Throwable> List<B> applyAll(Function<A, B, X> f, List<A> list) throws X {
48+
List<B> results = new ArrayList<B>();
49+
50+
for(A a : list) {
51+
results.add(f.apply(a));
52+
}
53+
54+
return results;
55+
}
56+
}
57+
58+
interface Function<A, B, X extends Throwable> {
59+
public B apply(A a) throws X;
60+
}
61+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package benblack86.pattern;
2+
3+
public class Interpreter {
4+
/*
5+
* Interpreter design pattern: specifies how to evaluate sentences in a language
6+
*/
7+
8+
public static void main(String[] args) {
9+
Exp<Integer> e = Exp.left(Exp.pair(Exp.plus(Exp.num(3), Exp.num(4)), Exp.num(5)));
10+
11+
System.out.println(e.eval());
12+
}
13+
14+
}
15+
16+
class Pair<A, B> {
17+
private final A left;
18+
private final B right;
19+
20+
public Pair(A left, B right) {
21+
this.left = left;
22+
this.right = right;
23+
}
24+
25+
public A left() {
26+
return left;
27+
}
28+
29+
public B right() {
30+
return right;
31+
}
32+
}
33+
34+
abstract class Exp<T> {
35+
abstract public T eval();
36+
37+
public static Exp<Integer> num(final int i) {
38+
return new Exp<Integer>() {
39+
public Integer eval() {
40+
return i;
41+
}
42+
};
43+
}
44+
45+
public static Exp<Integer> plus(final Exp<Integer> e1, final Exp<Integer> e2) {
46+
return new Exp<Integer>() {
47+
public Integer eval() {
48+
return e1.eval()+e2.eval();
49+
}
50+
};
51+
}
52+
53+
public static <A, B> Exp<Pair<A, B>> pair(final Exp<A> e1, final Exp<B> e2) {
54+
return new Exp<Pair<A, B>>() {
55+
public Pair<A, B> eval() {
56+
return new Pair<A, B>(e1.eval(), e2.eval());
57+
}
58+
};
59+
}
60+
61+
public static <A, B> Exp<A> left(final Exp<Pair<A, B>> e) {
62+
return new Exp<A>() {
63+
public A eval() {
64+
return e.eval().left();
65+
}
66+
};
67+
}
68+
69+
public static <A, B> Exp<B> right(final Exp<Pair<A, B>> e) {
70+
return new Exp<B>() {
71+
public B eval() {
72+
return e.eval().right();
73+
}
74+
};
75+
}
76+
}

0 commit comments

Comments
 (0)