From pretty much any book I've read it always said to code on interfaces/superclasses and rarely on implementations, today though that advice made a bit confused about how I should format my code.
Questions:
- Should I be using
Point2DAdvanced
orPoint2D.Double
in the the methods ofMedianPoint
(like JavaRDD<>, the 2 private methods, the variables)? Same goes forFlagPointPairProducer
. - Should I be using
MedianPoint
orPoint2D.Double
in theFlagPointPairProducer
class? - Should I store the
MedianPoint
from the static method in aMedianPoint
orPoint2D.Double
?
The Code:
First off I have a MedianPoint
class which extends Point2D.Double
. This class is responsible for outputting a median point out of a collection of points (for now only an RDD).
public class MedianPoint extends Point2D.Double {
public MedianPoint(double x, double y) {
super(x, y);
}
public static MedianPoint fromPointRDD(JavaRDD<Point2DAdvanced> points) {
Point2DAdvanced biggestPointByXDimension = points.reduce((a, b) -> getBiggestPointByXDimension(a, b));
Point2DAdvanced biggestPointByYDimension = points.reduce((a, b) -> getBiggestPointByYDimension(a, b));
double xDimensionMedian = biggestPointByXDimension.getX() / 2.0;
double yDimensionMedian = biggestPointByYDimension.getY() / 2.0;
return new MedianPoint(xDimensionMedian, yDimensionMedian);
}
private static Point2DAdvanced getBiggestPointByXDimension(Point2DAdvanced first, Point2DAdvanced second) {
return first.getX() > second.getX() ? first : second;
}
private static Point2DAdvanced getBiggestPointByYDimension(Point2DAdvanced first, Point2DAdvanced second) {
return first.getY() > second.getY() ? first : second;
}
}
I also have a FlagPointPairProducer
class which according to a median point it calculates a subspace flag for a given Point2DAdvanced
(another Point2D.Double
extension) and returns a tuple of the flag and the point.
public class FlagPointPairProducer implements Serializable {
private final Point2D.Double medianPoint;
public FlagPointPairProducer(Point2D.Double medianPoint) {
this.medianPoint = medianPoint;
}
public Tuple2<PointFlag, Point2DAdvanced> getFlagPointPair(Point2DAdvanced point) {
PointFlag flag = calculateFlag(point);
return new Tuple2<>(flag, point);
}
private PointFlag calculateFlag(Point2DAdvanced point) {
double x = point.getX();
double y = point.getY();
double medianX = medianPoint.getX();
double medianY = medianPoint.getY();
int xBit = x < medianX ? 0 : 1;
int yBit = y < medianY ? 0 : 1;
return new PointFlag(xBit, yBit);
}
}
So I can initialize a FlagPointPairProducer
like so:
Point2D.Double medianPoint = MedianPoint.fromPointRDD(points);
FlagPointPairProducer producer = new FlagPointPairProducer(medianPoint);
Code requested for Point2DAdvanced
:
public class Point2DAdvanced extends Point2D.Double implements Serializable {
public Point2DAdvanced(double x, double y) {
super(x, y);
}
public boolean dominates(Point2D.Double point) {
return (x <= point.getX() && y < point.getY())
|| (y <= point.getY() && x < point.getX());
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append('(').append(x).append(", ").append(y).append(')');
return builder.toString();
}
public static Point2DAdvanced fromTextLine(String textLine, String delimiter) {
textLine = textLine.trim();
String[] lineArray = textLine.split(delimiter);
double x = java.lang.Double.parseDouble(lineArray[0]);
double y = java.lang.Double.parseDouble(lineArray[1]);
return new Point2DAdvanced(x, y);
}
}
fromPointRDD()
? That's not what has been suggested to you in codereview.stackexchange.com/a/70878/58906 and codereview.stackexchange.com/a/70905/58906. You commented "This is mostly for project use so I guess you are right and I should extend it rather than wrap it both in this case and my other question." But you still havePoint2Advanced
(which is your former 'Point' I guess) the methods of which can be moved intoMedianPoint
. – Gerold Broser Nov 27 '14 at 21:08fromPointRDD()
derivingMedianPoint
fromPoint2D.Double
doesn't make sense. You could use a barePoint2D.Double
instead. – Gerold Broser Nov 27 '14 at 21:13super()
issue). The methods ofPoint2DAdvanced
can't be moved toMedianPoint
, these two classes have an entirely different purpose. One cares about point domination issues and the other cares about being the median point of a collection. But if you have a suggestion feel free to write an answer. – Dimitris K Nov 27 '14 at 21:14Point2DAdvanced
as well? – Gerold Broser Nov 27 '14 at 21:16