What's with the serializable? What's with the typo from pair to par? You can make Pair serializable and then make P and Q not explicitly serializable. If you pass in serializables, it'll work. If you don't, it won't. That seems like a better solution than forcing. A powerful type system should be able to fix this (make the pair serializable if and only if both of its generics parameter types are serializable), but java's isn't quite that powerful. It can be argued, because of the generic nature of the Pair class, that it creates nicer code if the fields are made public (and final), instead of having getters. The lack of a toString() is rather inexcusable, too.
If you've been adding it to every project, why not make a jar out of it? On Feb 8, 2:12 pm, "[email protected]" <[email protected]> wrote: > This is the first time I've felt the need to change the language. I > guess everybody else here wants other changes. I've got this class in > every project I've worked on for the last 2 years. Anybody has a > better one? > > public class Par<P extends Serializable, Q extends Serializable> > implements Serializable { > > static final long serialVersionUID = 9L; > > private P car; > private Q cdr; > > public Par(P car, Q cdr) { > this.car = car; > this.cdr = cdr; > } > > public P getCar() { > return car; > } > > public Q getCdr() { > return cdr; > } > > @Override > public boolean equals(Object obj) { > if (obj == null) { > return false; > } > if (getClass() != obj.getClass()) { > return false; > } > @SuppressWarnings("unchecked") > final Par<P, Q> other = (Par<P, Q>) obj; > if (this.car != other.car && (this.car == null || ! > this.car.equals(other.car))) { > return false; > } > if (this.cdr != other.cdr && (this.cdr == null || ! > this.cdr.equals(other.cdr))) { > return false; > } > return true; > } > > @Override > public int hashCode() { > int hash = 9; > hash += 97 * hash + (this.car != null ? this.car.hashCode() : > 0); > hash += 97 * hash + (this.cdr != null ? this.cdr.hashCode() : > 0); > return hash; > } > > } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaposse?hl=en -~----------~----~----~----~------~----~------~--~---
