First, some code. I have a class, Circle:
package aag;
public class Circle{
private final int RADIUS;
private float xVector;
private float yVector;
private float xCurrent;
private float yCurrent;
public Circle(int radius, int xCurrent, int yCurrent){
RADIUS = radius;
this.xCurrent = xCurrent;
this.yCurrent = yCurrent;
xVector = 0;
yVector = 0;
}
public float getXVector(){
return xVector;
}
public void setXVector(float xVector){
this.xVector = xVector;
}
public float getYVector(){
return yVector;
}
public void setYVector(float yVector){
this.yVector = yVector;
}
public float getXPosition(){
return xCurrent;
}
public void setXPosition(float xCurrent){
this.xCurrent = xCurrent;
}
public float getYPosition(){
return yCurrent;
}
public void setYPosition(float yCurrent){
this.yCurrent = yCurrent;
}
public int getRadius(){
return RADIUS;
}
public void tick(){
xCurrent += xVector;
yCurrent += yVector;
}
}
The Circle's tick() method is called once per cycle, which updates the position members (xCurrent & yCurrent). I have another class, aptly named CollisionDetector, whose responsibility is to assess Circle positions and make changes under certain criteria. In that particular class, I am currently working on the following method:
public void checkBounds(Circle circle, int fieldWidth, int fieldHeight){
int radius = circle.getRadius();
int boundWidth = fieldWidth - radius;
int boundHeight = fieldHeight - radius;
float xPosition = circle.getXPosition();
float yPosition = circle.getYPosition();
if(xPosition >= boundWidth){
if(xPosition != boundWidth)
circle.setXPosition(boundWidth + boundWidth - xPosition);
circle.setXVector(-circle.getXVector());
}
if(xPosition <= radius){
if(xPosition != radius){
circle.setXPosition(radius + radius - xPosition);
circle.setXVector(-circle.getXVector());
}
}
if(yPosition >= boundHeight){
if(yPosition != boundHeight)
circle.setYPosition(boundHeight + boundHeight - yPosition);
circle.setYVector(-circle.getYVector());
}
if(yPosition <= radius){
if(yPosition != radius){
circle.setYPosition(radius + radius - yPosition);
circle.setYVector(-circle.getYVector());
}
}
}
I think it is obvious that there are several patterns repeating themselves in this method. I am trying to reduce repetitive code, and have so far come up with the following:
private void upperCheck(float position, int bound, float vector, Consumer<Float> setPosition, Consumer<Float> setVector){
if(position >= bound){
checkHelper(position, bound, setPosition);
setVector.accept(-vector);
}
}
private void lowerCheck(float position, int bound, float vector, Consumer<Float> setPosition, Consumer<Float> setVector){
if(position <= bound){
checkHelper(position, bound, setPosition);
setVector.accept(-vector);
}
}
private void checkHelper(float position, int bound, Consumer<Float> setPosition){
if(position != bound)
setPosition.accept(bound + bound - position);
}
At the moment, my gut says that there must be a way to extract the comparison between position
and bound
so that I have a generic method which will (potentially) take >=
or <=
as a parameter.
Finally, my questions:
- First, Can I extract my comparison in such way as to not require two methods differing only in how they compare values?
- Second, how can I structure this method?
I hope I am not asking for something too specific my first time out. I felt that reducing repetition was a general enough idea, as well as having tried for a few days to divine a more efficient solution, that I should pose the question.
P.S.--Much of the code from Circle has been left out since it is not relevant to the scope of my question. Additionally, I have only included the methods from my CollisionDetector relevant to my concern. I could provide full code if necessary.