Thank you for your comments.
Par is spanish for Pair. It seems close enough. Also, the name pops up
in third pary code from time to time.
car and cdr are names on the back of my head, from being lisped a few years ago.
I don't like returning non-serializable pairs, so I decided it would
be better to force it in compile-time. Most of the time is BigDecimal,
Date, Long, String or ArrayList. When something different appeared...
well... it smelled.
I also don't like to know when something is not serializable even if
declared serializable. Serializable interface shows its age in code
like this, but I don't think there is something terribly wrong the way
it is.
The getters are needed for the JavaBeans nonsense. but I agree on public final.
You are totally right on lacking toString. I guess I never needed it.

On Sun, Feb 8, 2009 at 9:58 PM, Reinier Zwitserloot <reini...@gmail.com> wrote:
>
> 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, "marcelomorales.n...@gmail.com"
> <marcelomorales.n...@gmail.com> 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;
>>     }
>>
>> }
> >
>



-- 
Marcelo Morales

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to javaposse@googlegroups.com
To unsubscribe from this group, send email to 
javaposse+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to