1

I have two classes Float2 && Double2

public final class Float2 {
  public float width;
  public float height;
    
  public Float2 multiplyBy(float number) {
    return new Float2(this.width * number, this.height * number);
  }
}
public class Double2 {
  public double width;
  public double height;
}

I want to cast the result of multiplyBy method to a Double2 so that in the end I do not have Float2 newRectangleSize = rectangleSize.multiplyBy(3.2f); but Double2 newRectangleSize as the result of multiplication. Of course I can cast width and height values separately but I was wondering if there was a more ellegant solution in java?

1
  • Double2 newRectangleSize = (Double2) rectangleSize.multiplyBy(3.2f); would work if Float2 could be cast to Double2. Commented Aug 27, 2020 at 17:32

2 Answers 2

1

If Double2 and Float2 would be related types then casting could be possible.

As long as they aren't, you cannot cast them regardless the names of parameters are same including their types. However, you can use this advantage for a framework based on reflection that makes mapping between types easy, such as MapStruct:

@Mapper
public interface Double2Mapper {
 
    Double2Mapper INSTANCE = Mappers.getMapper(Double2Mapper.class); 

    @Mapping
    Double2 floatToDouble(Float2 float2); 
}
Double2 double2 = Double2Mapper.INSTANCE.floatToDouble(rectangleSize.multiplyBy(3.2f));

If you are not keen to use a framework (in case of only few specific and simple cases), I offer you two basic ways based on the pure OOP:

  1. A constructor of Double2 using Float2 (assuming the fields remain public):

    public Double2(Float2 float2) {
        this.width = float2.width;
        this.height = float2.height;
    }
    
  2. A method returning such type in the class Float2:

    public Double2 asDouble2() {
        return new Double2(this.width, this.height);
    }
    
Sign up to request clarification or add additional context in comments.

Comments

1

So clearly trying to cast in this case will result in a ClassCastException. One option would be to support an interface that is implemented by both classes that allows width() and height() to be defined as Number objects or even a type that extends Number. Then you could handle conversion on a implementation by implementation basis to handle precision loss in cases like int or short.

interface Size<T extends Number> {

    T width();

    T height();

}

Another option might be to provide static factory methods to these classes to allow for the conversion of Double2 to Float2 and vice versa. It might look like this;

public class Double2 {
    
    private final double width;

    private final double height;

    public Double2(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public static Double2 valueOf(Float2 float) {
        return new Double2((double) float.width, (double) float.height);
    }

}
Float2 float2 = new Float2(2f, 2f).multiplyBy(2f);

Double2 double2 = Double2.valueOf(float2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.