Automatic type conversion in Java?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 19:55:57

The answer is short: It's possible with overloading in C++ but there is no way to do that in Java.

You could overload your methods, something like this:

public void doSomething(FooSet data) {
    /* .. */
}

public void doSomething(BarSet data) {
    doSomething(barTOfoo(data));
}

You could do both together:

(1) Write wrapper methods which do conversion on the back-scenes.

(2) If your objects have proper hashCode overriden method, the wrapper methods may manage a very simple and fast cache, which you can build yourself with simple Map implementation and probably synchronization (if you use the objects concurrently at all).

That will get you rid of both converting between the two types all the time and probably of performance concerns. Even if you go against caching, I would still recommend (as other posters said) to use wrapping methods. That at least saves you lots of unnecessary typing.

Good luck.

Overloading works in the opposite way, you declare two methods on the receiver object:

public void doSomething(FooSet data)
{
    /* .. */
}

public void doSomething(BarSet data)
{
    doSomething(barToFoo(data));
}

then thanks to Dynamic Binding (wikipedia) the right method is choosen at run-time.

Of course overloading in Java is scoped at object-level (since calls are restricted to instances or class declarations), but it works in the same way.

In your case you can try by extending the class also if it's in an external library, since it's java you should be able to do it, and add the method or by using reflection (java tutorial) to add the method dynamically.

Dominic Fox

Automatic type conversion isn't supported (you want Scala's implicit type conversions for that).

But you could try using a Variant type as a junction-box for switching between types:

/** Given a BarSet, returns a FooSet */
public Function<BarSet, FooSet> barTofoo = new Function<BarSet, FooSet>() {
    @Override public FooSet apply(BarSet input) { /* ... */ }
}

/** Given a FooSet, returns a BarSet */
public Function<FooSet, BarSet> fooToBar = new Function<FooSet, BarSet>() {
    @Override public BarSet apply(FooSet input) { /* ... */ }
}

/** Create a type conversion context in which both of these conversions are registered */
TypeConversionContext fooBarConversionContext = MatchingTypeConversionContext.builder()
    .register(FooSet.class, BarSet.class, fooToBar)
    .register(BarSet.class, FooSet.class, barToFoo)
    .build();

/** Put a FooSet into a Variant, bound to our type conversion context */
FooSet fooSet = new FooSet();
Variant data = Variant.of(fooSet).in(fooBarConversionContext);

/** Pull a BarSet out of the Variant */
public void doSomething(Variant data) {
    Preconditions.checkArgument(data.isConvertibleTo(BarSet.class);
    BarSet barSet = data.as(BarSet.class);
    // ...
}

Java was created with visibility in mind. Every programmer should be able read just one line and understand what is happening there. This is why it doesn't have operator overloading, this is why it doesn't have any kind of automatic custom type conversion. So, unless you write your own wrappers around one of the library that will accept types from other library and explicitly convert them, you are out of luck there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!