Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Common Pair Class

0 views
Skip to first unread message

sks

unread,
Jan 6, 2005, 3:15:41 AM1/6/05
to
I often find myself wanting to wrap two values together and make a list of
such wrapped objects, and I hate having to create a whole class for the
pairing everytime (I use a simple class called Pair I wrote), is there
another way people do it ? I sometimes put them in a LinkedHashMap, but
that's not really 'right' because the values aren't mappings from a to b.

Joona I Palaste

unread,
Jan 6, 2005, 7:05:29 AM1/6/05
to
sks <sks.re...@magnum55.remove11.co.uk> scribbled the following:

I've had the need to wrap two values together as well, and I generally
also use a custom-built wrapper class. If it's only ever needed within
one class, I make it an inner class of that class.

--
/-- Joona Palaste (pal...@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"To doo bee doo bee doo."
- Frank Sinatra

Andrew McDonagh

unread,
Jan 6, 2005, 7:20:13 AM1/6/05
to
Joona I Palaste wrote:
> sks <sks.re...@magnum55.remove11.co.uk> scribbled the following:
>
>>I often find myself wanting to wrap two values together and make a list of
>>such wrapped objects, and I hate having to create a whole class for the
>>pairing everytime (I use a simple class called Pair I wrote), is there
>>another way people do it ? I sometimes put them in a LinkedHashMap, but
>>that's not really 'right' because the values aren't mappings from a to b.
>
>
> I've had the need to wrap two values together as well, and I generally
> also use a custom-built wrapper class. If it's only ever needed within
> one class, I make it an inner class of that class.
>

What happens to the Pair object when it is received?

It may help to know that these pair classes can usually become 'proper,
fully working' normal classes.

Usually, I find Pair objects useful when sending an event of some kind,
but the event system only allows a single user object to be transmitted.
However, these pair objects do not have to stay as mere data holders for
long. They can in fact become intelligent objects which know how to
initiate their processing. Quite often patterns like Visitor can be
used in this case.

So, Pair classes (aka data holders) are ok, but usually can be turned
into 'normal' classes.

Worth a look ... http://www.martinfowler.com/articles/injection.html

Andrew

Chris Uppal

unread,
Jan 6, 2005, 7:56:45 AM1/6/05
to
Andrew McDonagh wrote:

> Usually, I find Pair objects useful when sending an event of some kind,
> but the event system only allows a single user object to be transmitted.
> However, these pair objects do not have to stay as mere data holders for
> long. They can in fact become intelligent objects which know how to
> initiate their processing.

I agree. And that's why I decided not to reply pointing out that one could
create a single (reusable), generic Pair<U, V> class. The generic solution
looks OK, but would probably inhibit further development.

-- chris


sks

unread,
Jan 6, 2005, 10:47:00 AM1/6/05
to

"Chris Uppal" <chris...@metagnostic.REMOVE-THIS.org> wrote in message
news:Y6-dnbQ3v5O...@nildram.net...

That's what I use atm, a List<Pair<A,B>> reference.

But I hate all the extra typing just to get values out of it. Would like
some syntatic sugar. I use an array when they're of the same type.


Chris Uppal

unread,
Jan 6, 2005, 11:47:03 AM1/6/05
to
sks wrote:

> That's what I use atm, a List<Pair<A,B>> reference.
>
> But I hate all the extra typing just to get values out of it. Would like
> some syntatic sugar. I use an array when they're of the same type.

Would something like:

=======
public class PairArrayList<K, V>
extends java.util.ArrayList<Pair<K, V>>
{
public K keyAt(int n) { return this.get(n).key(); }
public V valueAt(int n) { return this.get(n).value(); }
public boolean add(K key, V value) { return this.add(new Pair<K, V>(key,
value)); }
// ... and so on ...
}
=======

(plus suitable generic interfaces, etc) alleviate your distress ?

-- chris


sks

unread,
Jan 6, 2005, 12:17:21 PM1/6/05
to

"Chris Uppal" <chris...@metagnostic.REMOVE-THIS.org> wrote in message
news:icSdnRGjnJx...@nildram.net...

Similar to a LinkedHashMap really like I mentioned originally.

I was just bitching about lack of tuples or something really :)

Andrew McDonagh

unread,
Jan 6, 2005, 12:25:25 PM1/6/05
to

If you hate the typing to get the values so much, can you not convert
these data holders into normal classes as I suggested? This removes the
smell and creates a powerful design.

Can you post the code for us to see? (i.e. the code that creates the
Pair objects, The Pair class, and the Pair Receiving class)

opa...@gmail.com

unread,
Jan 6, 2005, 6:39:48 PM1/6/05
to
I go this way:


/**
Class to maintain a relation between two objects.
*/
public class pair<First, Second> implements java.io.Serializable {
private First one; private Second two;
public pair(First one, Second two) {
this.one = one;
this.two = two;
}
/**
evaluates to "(" + first() + "," + second() + ")"
*/
public synchronized String toString() {
return "(" + one + "," + two + ")";
}
public First first() { return one; }
public Second second() { return two; }
public void first(First o) { one = o; }
public void second(Second o) { two = o; }
/**
A pair equals another pair when their
respective parts equal one another.
*/
public /*synchronized*/ boolean equals(Object o) {
boolean same = false;
if (o instanceof pair) {
pair p = (pair) o;
if (p == this)
same = true;
else {
//synchronized(p) {
same = p.one.equals(one) && p.two.equals(two);
//}
}
}
return same;
}
/**
*/
public int hashCode() {
return one.hashCode();
}
};

Yo, you got a better hashCode() method for me? I saw a C function
that makes me consider:

public int hashCode() {
return one.hashCode()*37 + two.hashCode();
}

But I dunno.

Um, I thought about pair and I had some "design" considerations:

easy to read code.
i want an equals (... and so i want a hashCode)

when writing I thought:
if toString is not synchronized then there is a weird thing so I made
it syncrhonized. the weird thing to me is that say some pair changes
in between evaluating the two parts of the String, well then I can see
a toString that was never a value.

bye

opa...@gmail.com

unread,
Jan 6, 2005, 6:59:02 PM1/6/05
to
Geezies-fabreezies. All my indents is gone. Is there a way to indent
code (...especially through google groups?) This is pre Kernighan
(antekernighanerian).

don't flame now. I have legit addons:

some more of my design considerations:

) i don't like "get" "set". it's more ink and no additional meaning.
) implements Serializable because why not
) to spent time on class because it is applicable in many contexts

I'd like to know anyone has experience with equals on pair and whether
it should be synchronized?

bye

Andrew McDonagh

unread,
Jan 8, 2005, 8:41:22 AM1/8/05
to
opa...@gmail.com wrote:
> I go this way:

snipped and re-added your code once reformatted


>
> when writing I thought:
> if toString is not synchronized then there is a weird thing so I made
> it syncrhonized. the weird thing to me is that say some pair changes
> in between evaluating the two parts of the String, well then I can see
> a toString that was never a value.
>
> bye
>

/**
Class to maintain a relation between two objects.
*/

public class Pair<First, Second> implements java.io.Serializable {
private First one;

private Second two;

public Pair(First one, Second two) {


this.one = one;
this.two = two;
}

/**
evaluates to "(" + first() + "," + second() + ")"
*/
public synchronized String toString() {
return "(" + one + "," + two + ")";
}

public First first() {
return one;
}

public Second second() {
return two;
}

public void first(First o) {
one = o;
}

public void second(Second o) {
two = o;
}

/**
A pair equals another pair when their
respective parts equal one another.
*/

public/*synchronized*/boolean equals(Object o) {
boolean same = false;
if (o instanceof Pair) {
Pair p = (Pair) o;

0 new messages