Created
February 18, 2012 01:12
-
-
Save Irfy/1856690 to your computer and use it in GitHub Desktop.
An alternative to late binding of template arguments to template arguments, by pulling the early bound template arguments to bind later, at the same time with their template arguments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.HashSet; | |
import java.util.Vector; | |
interface Wrapped<T> { | |
void commonWrappedMethod(); | |
} | |
class Wrapped1<T> implements Wrapped<T> { | |
private final T t; | |
public Wrapped1(T t) { | |
this.t = t; | |
} | |
@Override | |
public String toString() { | |
return String.format("Wrapped1(%s)", String.valueOf(t)); | |
} | |
@Override | |
public void commonWrappedMethod() { | |
System.err.println("Wrapped1.commonWrappedMethod"); | |
} | |
} | |
class Wrapped2<T> implements Wrapped<T> { | |
private final T t; | |
public Wrapped2(T t) { | |
this.t = t; | |
} | |
@Override | |
public String toString() { | |
return String.format("Wrapped2(%s)", String.valueOf(t)); | |
} | |
@Override | |
public void commonWrappedMethod() { | |
System.err.println("Wrapped2.commonWrappedMethod"); | |
} | |
} | |
class Adder { | |
public <T, W extends Wrapped<T>> void add(ArrayList<W> list, W w) { | |
System.out.println("list.add " + w); | |
w.commonWrappedMethod(); | |
list.add(w); | |
} | |
public <T, W extends Wrapped<T>> void add(HashSet<W> set, W w) { | |
System.out.println("set.add " + w); | |
w.commonWrappedMethod(); | |
set.add(w); | |
} | |
// For all other collections | |
public <T, W extends Wrapped<T>> void add(Collection<W> col, W w) { | |
System.out.println("col.add " + w); | |
w.commonWrappedMethod(); | |
col.add(w); | |
} | |
} | |
public class so9336486 { | |
public static void main(String[] args) { | |
ArrayList<Wrapped1<Integer>> w1il = new ArrayList<Wrapped1<Integer>>(); | |
ArrayList<Wrapped1<String>> w1sl = new ArrayList<Wrapped1<String>>(); | |
ArrayList<Wrapped2<Integer>> w2il = new ArrayList<Wrapped2<Integer>>(); | |
ArrayList<Wrapped2<String>> w2sl = new ArrayList<Wrapped2<String>>(); | |
HashSet<Wrapped1<Integer>> w1is = new HashSet<Wrapped1<Integer>>(); | |
HashSet<Wrapped1<String>> w1ss = new HashSet<Wrapped1<String>>(); | |
HashSet<Wrapped2<Integer>> w2is = new HashSet<Wrapped2<Integer>>(); | |
HashSet<Wrapped2<String>> w2ss = new HashSet<Wrapped2<String>>(); | |
Vector<Wrapped1<Integer>> w1iv = new Vector<Wrapped1<Integer>>(); | |
Vector<Wrapped1<String>> w1sv = new Vector<Wrapped1<String>>(); | |
Vector<Wrapped2<Integer>> w2iv = new Vector<Wrapped2<Integer>>(); | |
Vector<Wrapped2<String>> w2sv = new Vector<Wrapped2<String>>(); | |
Adder adder = new Adder(); | |
adder.add(w1il, new Wrapped1<Integer>(1)); | |
adder.add(w1sl, new Wrapped1<String>("two")); | |
adder.add(w2il, new Wrapped2<Integer>(3)); | |
adder.add(w2sl, new Wrapped2<String>("four")); | |
adder.add(w1is, new Wrapped1<Integer>(5)); | |
adder.add(w1ss, new Wrapped1<String>("six")); | |
adder.add(w2is, new Wrapped2<Integer>(7)); | |
adder.add(w2ss, new Wrapped2<String>("eight")); | |
adder.add(w1iv, new Wrapped1<Integer>(9)); | |
adder.add(w1sv, new Wrapped1<String>("ten")); | |
adder.add(w2iv, new Wrapped2<Integer>(11)); | |
adder.add(w2sv, new Wrapped2<String>("twelve")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
Note that the second half is printed to
System.err
so the exact order of printing in relation to the first half is not necessarily defined. Eclipse outputs exactly like this, but console interleavesSystem.err
withSystem.out
.