H
Hendrik Maryns
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi
I have some code like this which I am trying to generify:
private static <T> Comparator<T> reverse(final Comparator<T> wrapped) {
return new Reverse<T>(wrapped);
}
public static <T> Comparator<T> switchFirst() {
return new SwitchFirst<T>();
}
public static <T> Comparator<T> switchLast() {
return reverse(switchFirst());
}
Where Reverse<T> and SwitchFirst<T> extends Comparator<T>. Reverse<T>
does the obvious, whereas SwitchFirst<T> is some idiosyncratic stuff.
However:
Type mismatch: cannot convert from Comparator<Object> to Comparator<T>
(This is in Eclipse).
Why?
SSCCE:
import java.util.Comparator;
public class Test {
private static <T> Comparator<T> reverse(final Comparator<T> wrapped) {
return new Reverse<T>(wrapped);
}
private static class Reverse<T> implements Comparator<T> {
private final Comparator<T> wrapped;
/**
* Creates a Comparator with reverse logic
*/
public Reverse(final Comparator<T> wrapped) {
this.wrapped = wrapped;
}
public int compare(final T left, final T right) {
return -this.wrapped.compare(left, right);
}
}
public static <T> Comparator<T> switchFirst() {
return new SwitchFirst<T>();
}
public static <T> Comparator<T> switchLast() {
return Test.reverse(Test.switchFirst());
}
private static class SwitchFirst<T> implements Comparator<T> {
public int compare(final T left, final T right) {
final boolean l = left instanceof Integer;
final boolean r = right instanceof Integer;
// I replaced Switch with Integer here, just for example’s sake
if (l ^ r) {
if (l) {
return -1;
}
return 1;
}
return 0;
}
}
}
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org
iEYEARECAAYFAkjzd98ACgkQBGFP0CTku6PcagCgv83OqBV19LUgeayZOS1rBlLs
CAsAn22gWhX+4tW1aCuK/1wU+S0r/KYc
=UePx
-----END PGP SIGNATURE-----
Hash: SHA1
Hi
I have some code like this which I am trying to generify:
private static <T> Comparator<T> reverse(final Comparator<T> wrapped) {
return new Reverse<T>(wrapped);
}
public static <T> Comparator<T> switchFirst() {
return new SwitchFirst<T>();
}
public static <T> Comparator<T> switchLast() {
return reverse(switchFirst());
}
Where Reverse<T> and SwitchFirst<T> extends Comparator<T>. Reverse<T>
does the obvious, whereas SwitchFirst<T> is some idiosyncratic stuff.
However:
Type mismatch: cannot convert from Comparator<Object> to Comparator<T>
(This is in Eclipse).
Why?
SSCCE:
import java.util.Comparator;
public class Test {
private static <T> Comparator<T> reverse(final Comparator<T> wrapped) {
return new Reverse<T>(wrapped);
}
private static class Reverse<T> implements Comparator<T> {
private final Comparator<T> wrapped;
/**
* Creates a Comparator with reverse logic
*/
public Reverse(final Comparator<T> wrapped) {
this.wrapped = wrapped;
}
public int compare(final T left, final T right) {
return -this.wrapped.compare(left, right);
}
}
public static <T> Comparator<T> switchFirst() {
return new SwitchFirst<T>();
}
public static <T> Comparator<T> switchLast() {
return Test.reverse(Test.switchFirst());
}
private static class SwitchFirst<T> implements Comparator<T> {
public int compare(final T left, final T right) {
final boolean l = left instanceof Integer;
final boolean r = right instanceof Integer;
// I replaced Switch with Integer here, just for example’s sake
if (l ^ r) {
if (l) {
return -1;
}
return 1;
}
return 0;
}
}
}
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org
iEYEARECAAYFAkjzd98ACgkQBGFP0CTku6PcagCgv83OqBV19LUgeayZOS1rBlLs
CAsAn22gWhX+4tW1aCuK/1wU+S0r/KYc
=UePx
-----END PGP SIGNATURE-----