Given that he customized this to being called 'Par', omitting the
toString, and having the awkward Serializable requirement, it seems
like it would be considerably LESS work in the long run to do it right
once. It also seems obvious, given those mistakes, that he may have to
make updates, so copy/pasting the class into every new project is also
a bad idea, as then he couldn't easily make updates to this class.
Here are some more problems with your pair class:
1. By using a constructor, you don't get type inference. Use a static
method, like so:
public static <P, Q> Pair<P, Q> create(P a, P b) {
return new Pair<P, Q>(a, b);
}
and then make the actual constructor private.
2. While you're at it, you can also fix your serializable issue, by
making two constructors:
public static <P extends Serializable, Q extends Serializable> Pair<P,
Q> create(P a, P b) {
return new Pair<P, Q>(a, b);
}
public static <P, Q> Pair<P, Q> createNonSerializable(P a, P b) {
return new Pair<P, Q>(a, b);
}
so, now by default things have to be serializable, but you can get
around the issue if you want without having to bother with raw types
and @SuppressWarnings("unchecked").
(If you're really anal about it, you can create a Pair class and a
SerializablePair subclass, and have the create() method return a
SerializablePair, but I'd have to question your sanity).
Also, your insistence on Serializable is itself a code smell; because,
as Keith Haber suggested: If serializable 'seems like a good idea',
then so does Comparable, and then why not Runnable (for if both pairs
are Runnables and you want to run both of them, or some such). Where
do you draw the line? Sure, serializable needs special treatment
because its such a bad design, but that's actually a good reason to
work around it as best you can.
NB: Here's something I might submit to Project Coin: Make all entities
serializable regardless of whether or not they implement Serializable,
but run every attempt to serialize a non-serializable through the
securitymanager.
On Feb 9, 3:08 am, Weiqi Gao <
weiqi...@gmail.com> wrote:
> Reinier Zwitserloot wrote:
>
> > If you've been adding it to every project, why not make a jar out of
> > it?
>
> Because that would be more work than writing the class.
>
> --
> Weiqi Gao
> weiqi...@gmail.comhttp://
www.weiqigao.com/blog/