###前提・実現したいこと
ここに質問したいことを詳細に書いてください
下記のようなOrgクラスを、インターフェースと実装クラスに分割したいと考えております。
ここで、IBar::mapメソッドに問題を抱えています。
java
1 /** 2 * オリジナルのクラス。 3 * これを、下記のインターフェースと実装クラスに分割したい。 4 */ 5 public static class Org<E> 6 extends java.util.ArrayList<E> { 7 8 /** 9 * Foo<E>を、Foo<O>に変換する。 10 */ 11 public <O> Org<O> map(final Function<E,O> mapper) { 12 return 13 super 14 .stream() 15 .map(mapper) 16 .collect(Collectors.toCollection(Org::new)); 17 } 18 19 /** 20 * Bar<E>の要素をフィルターする。 21 * ※質問の趣旨とは直接関係なし 22 */ 23 public Org<E> filter(final Predicate<E> filter) { 24 /* ロジックを簡略 */ 25 return this; 26 } 27 28 /** 29 * Bar<E>の重複する要素を削除する。 30 * ※質問の趣旨とは直接関係なし 31 */ 32 public <K> Org<E> distinct(final Function<E,K> keySelector) { 33 /* ロジックを簡略 */ 34 return this; 35 } 36 37 /** 38 * Bar<E>の要素を昇順に並べる。 39 * ※質問の趣旨とは直接関係なし 40 */ 41 public Org<E> orderBy(final Function<E, Integer> keySelector) { 42 /* ロジックを簡略 */ 43 return this; 44 } 45 46 }
java
1 /** 2 * インターフェース 3 */ 4 public interface IBar<E, L extends IBar<E, L>> 5 extends java.util.List<E>{ 6 7 /** 8 * Bar<E>をBar<O>に変換する。 9 */ 10 default 11 <O, OL extends IBar<O, OL>> 12 OL map(final Function<E,O> mapper){ 13 try { 14 Constructor<OL> con = (Constructor<OL>) this.getClass().getConstructor(); 15 OL ol = con.newInstance(); 16 17 this.stream().map(mapper).forEach(value -> ol.add(value)); 18 19 return ol; 20 } 21 catch (Exception e) { 22 return null; 23 } 24 } 25 26 /** 27 * Bar<E>の要素をフィルターする。 28 * ※質問の趣旨とは直接関係なし 29 */ 30 default L filter(final Predicate<E> filter) { 31 /* ロジックを簡略 */ 32 return (L) this; 33 } 34 35 /** 36 * Bar<E>の重複する要素を削除する。 37 * ※質問の趣旨とは直接関係なし 38 */ 39 default <K> L distinct(final Function<E,K> keySelector) { 40 /* ロジックを簡略 */ 41 return (L) this; 42 } 43 44 /** 45 * Bar<E>の要素を昇順に並べる。 46 * ※質問の趣旨とは直接関係なし 47 */ 48 default L orderBy(final Function<E, Integer> keySelector) { 49 /* ロジックを簡略 */ 50 return (L) this; 51 } 52 53 } 54 55 /** 56 * IBar実装クラス。 57 */ 58 public static class Bar<E> 59 extends java.util.ArrayList<E> 60 implements IBar<E, Bar<E>> { 61 /** 62 * ここには何も書きたくない 63 */ 64 } 65 66 /** 67 * IBar実装クラス。 68 */ 69 public static class Foo<E> 70 extends java.util.LinkedList<E> 71 implements IBar<E, Foo<E>> { 72 /** 73 * ここには何も書きたくない 74 */ 75 }
###発生している問題・エラーメッセージ
型の不一致: IBar<Object,IBar<Object,OL>> から Foo<Foo<Integer>> には変換できません
###該当のソースコード
java
1 public static void main(final String[] args) { 2 try { 3 // (1)このケースは問題なく動作する。 4 Bar<String> strBar = new Bar<>(...); 5 Bar<Integer> intBar = strBar.map(item -> item.length()); 6 7 // (2)このケースはコンパイルエラー 8 Foo<Foo<String>> strFooFoo = new Foo<>(...); 9 Foo<Foo<Integer>> intFooFoo = strFooFoo.map(bar -> bar.map(item -> item.length())); /* 型の不一致! */ 10 11 // (3)このケースは問題なく動作するが、こんな書き方をさせたくない 12 Foo<Foo<String>> strFooFoo2 = new Foo<>(...); 13 Foo<Foo<Integer>> intFooFoo2 = strFooFoo2.<Foo<Integer>, Foo<Foo<Integer>>>map(foo -> foo.<Integer, Foo<Integer>>map(item -> item.length())); 14 15 // (4)オリジナルのクラスでも、問題なく動作する。 16 Org<Org<String>> strOrgOrg = new Org<>(...); 17 Org<Org<Integer>> intOrgOrg = strOrgOrg.map(org -> org.map(item -> item.length())); 18 19 } 20 catch (Exception e) { 21 e.printStackTrace(); 22 } 23 }
###ご質問
「型の不一致」エラーが出ている(2)について、
ソースコード側ではなく、IBarインターフェース側に何らかの手を加えて、コンパイルエラーを解消できないでしょか?
###補足情報(言語/FW/ツール等のバージョンなど)
言語:Java8(JDK1.8)
IDE:Eclipse 4.5
実行環境:Windows 10