O
opalpa
A while back I posted what I used as pair
http://groups.google.com/group/comp...de06bb1f053619?q=pair&rnum=1#bdde06bb1f053619
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
(e-mail address removed)
http://www.geocities.com/opalpaweb/
http://groups.google.com/group/comp...de06bb1f053619?q=pair&rnum=1#bdde06bb1f053619
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
(e-mail address removed)
http://www.geocities.com/opalpaweb/