Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > pair.class revised

Reply
Thread Tools

pair.class revised

 
 
opalpa@gmail.com opalinski from opalpaweb
Guest
Posts: n/a
 
      03-15-2006
A while back I posted what I used as pair

http://groups.google.com/group/comp....de06bb1f053619


I had some concerns about the code, which a revision has eliminated.
By making pair immutable synchronization concerns disappeared. Here is
a simple class which is used frequently in my code:

/**
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 String toString() {
return "(" + one + "," + two + ")";
}
public First first() { return one; }
public Second second() { return two; }
/**
A pair equals another pair when their
respective parts equal one another.
*/
public boolean equals(Object o) {
boolean same = false;
if (o instanceof pair) {
pair p = (pair) o;
if (p == this)
same = true;
else {
same = p.one.equals(one) && p.two.equals(two);
}
}
return same;
}
public int hashCode() {
return one.hashCode()*47 + two.hashCode();
}
};

The benefits of being able to update each part of the relation are
small (as a new pair can be created with wanted parts). The benefits
of immutability are greater.

Sharingly,
Opalinski
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.geocities.com/opalpaweb/

 
Reply With Quote
 
 
 
 
lewmania942@yahoo.fr
Guest
Posts: n/a
 
      03-16-2006
Bonjour mon ami (saw you were learning french ou your website


> By making pair immutable synchronization concerns disappeared.


Indeed. Though you'll only get referential transparency if your
First and Second object are also really immutable.

http://en.wikipedia.org/wiki/Referential_transparency


> The benefits of being able to update each part of the relation are
> small.


To me that would be a procedural approach... and I'd indeed
say that the benefits would be tiniest than tiny.


> The benefits of immutability are greater.


I think like you. So does Joshua Bloch...

"Effective Java", item 13: "favor immutability".

On creating a new instance when needed instead
of having a mutable instance:

"It is known as the functional approach because methods
"return the result of applying a function to their operand
"without modifying it. Contrast this to the more common
"procedural approach in which methods apply a procedure
"to their operand causing its state to change.
"
"The functional approach may appear unnatural if you're
"not familiar with it, but it enables immutability, which has
"many advantages.

Buy that book if you haven't done it yet, you'll like it
(it's getting old, but it's a goldmine).


Note also that OOP using immutable objects is possible, but
that is one of the best kept secret of some posters in comp.object
(disagreement on this should go either to comp.object or
to /dev/null <-- notice the gentle smiley

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      03-16-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>A while back I posted what I used as pair
>
>
>
> I had some concerns about the code, which a revision has eliminated.
> By making pair immutable synchronization concerns disappeared. Here is
> a simple class which is used frequently in my code:
>
> /**
> Class to maintain a relation between two objects.
> */
> public class pair<First, Second> implements java.io.Serializable {
> private First one; private Second two;


[snip]
> };
>
> The benefits of being able to update each part of the relation are
> small (as a new pair can be created with wanted parts). The benefits
> of immutability are greater.


Declare the fields as being final, to make your intent more explicit,
and to avoid subtle bugs.

- Oliver

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I announce this? (Revised) vbmark Computer Support 22 08-12-2005 12:21 AM
Re: Critique my company's revised web site. Spartanicus HTML 0 09-16-2004 12:04 PM
Gradual image fade in (revised) newtohtml@att.net HTML 1 07-19-2004 01:36 PM
Check Out This Great Site (Revised) TechGeekPro MCSE 26 06-29-2004 06:01 AM
DVD Verdict reviews: KREATOR: LIVE KREATION -- REVISED GLORY and more! DVD Verdict DVD Video 0 09-05-2003 09:04 AM



Advertisments