admin管理员组文章数量:1335414
Jackson's objectMapper.readValues(...)
returns a MappingIterator
class, that implements both Closeable
and Iterator
:
public class MappingIterator<T> implements Iterator<T>, Closeable {...}
I'm using it to deserialize a JSONL, but I don't want my method to be tied to Jackson specific objects, so I've created an CloseableIterator
instead that implements both:
public interface CloseableIterator<T> extends Iterator<T>, Closeable {...}
I want my method to return CloseableIterator
, instead of directly exposing MappingIterator
:
CloseableIterator<FooBar> readFooBars(...) {
return objectMapper.readValues(...);
}
However, the compiler won't let me compile it without adding an unchecked cast, which seems unnecessary. Is there a way to cast an object into an interface that in turn extends multiple interfaces?
Jackson's objectMapper.readValues(...)
returns a MappingIterator
class, that implements both Closeable
and Iterator
:
public class MappingIterator<T> implements Iterator<T>, Closeable {...}
I'm using it to deserialize a JSONL, but I don't want my method to be tied to Jackson specific objects, so I've created an CloseableIterator
instead that implements both:
public interface CloseableIterator<T> extends Iterator<T>, Closeable {...}
I want my method to return CloseableIterator
, instead of directly exposing MappingIterator
:
CloseableIterator<FooBar> readFooBars(...) {
return objectMapper.readValues(...);
}
However, the compiler won't let me compile it without adding an unchecked cast, which seems unnecessary. Is there a way to cast an object into an interface that in turn extends multiple interfaces?
Share Improve this question edited Nov 20, 2024 at 0:42 Joseph asked Nov 20, 2024 at 0:35 JosephJoseph 1729 bronze badges1 Answer
Reset to default 4Unfortunately, it's not possible even with casting. The cast would fail at runtime because the returned type does not implement your interface, even if its methods are identical to MappingIterator
's.
A simple workaround is to return a private implementation of your interface that delegates to the Jackson iterator:
private static class JacksonCloseableIterator<T> implements CloseableIterator<T> {
private final MappingIterator<T> iterator;
JacksonCloseableIterator(MappingIterator<T> iterator) {
this.iterator = iterator;
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public T next() {
return iterator.next();
}
@Override
public void close() throws IOException {
iterator.close();
}
}
(If you have Guava, you can extend ForwardingIterator
to reduce the boilerplate somewhat.)
See here for an alternative approach using generics with multiple bounds.
本文标签: javaCasting objects into interface that extends multiple interfacesStack Overflow
版权声明:本文标题:java - Casting objects into interface that extends multiple interfaces - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742388627a2465541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论