汎用ワイルドカードを使用してJava APIの利便性を強化する

良い一日!

この投稿は、Java言語で別のAPIに取り組んでいる人、または既存のAPIを改善しようとしている人向けです。 ここで簡単なアドバイスが与えられます。どのように構成体を使用し? extends T ? extends T? super T ? extends T? super T ? super Tは、インターフェースの使いやすさを大幅に向上させることができます。

ポイントに直行します

ソースAPI


たとえば、キータイプ( K )と値タイプ( V )の2つのタイプでパラメーター化されたオブジェクトの特定のストレージのインターフェースがあるとします。 インターフェイスは、リポジトリ内のデータを操作するための一連のメソッドを定義します。

 public interface MyObjectStore<K, V> { /** *       . * * @param key . * @param value . */ void put(K key, V value); /** *       . * * @param key . * @return   null. */ @Nullable V get(K key); /** *    -  . * * @param entries   -. */ void putAll(Map<K, V> entries); /** *        * . * * @param keys  . * @return  -. */ Map<K, V> getAll(Collection<K> keys); /** *     ,  *   (). * * @param p    . * @return ,  . */ Collection<V> getAll(Predicate<V> p); ... //    } 

述語の定義
 interface Predicate<E> { /** *  true,    * , false   . * * @param exp   . * @return true,  ; false,  . */ boolean apply(E exp); } 


インターフェイスは非常に適切で論理的に見え、ユーザーはリポジトリを操作するための簡単なコードを簡単に書くことができます。

 MyObjectStore<Long, Car> carsStore = ...; carsStore.put(20334L, new Car("BMW", "X5", 2013)); Car c = carsStore.get(222L); ... 

ただし、ささいなケースでは、APIクライアントに不快な制限が発生します。

使用し? super T ? super T


述語を満たす値を読み取る最後のメソッドを使用します。 彼に何が問題なのでしょうか? 私たちは次のように書きます:

 Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { ... //      . } }); 

しかし、実際には、私たちのクライアントはすでに車を選ぶための述語を持っています。 Carクラスではなく、 Car継承されるVehicleクラスによってのみパラメーター化されます。 彼は詰め込もうとするかもしれない
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);


Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
  1. Predicate Predicate, :

    no suitable method found for getAll(Predicate<Vehicle>)
    , , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

    final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
    ! :

    Collection<V> getAll(Predicate<? super V> p);
    Predicate<? super V> " V V ( Object)". , . Vehicle :

    MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
    , .

    ? extends T
    , . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

    MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
    , , , , wildcard ? extends T :

    void putAll(Map<? extends K, ? extends V> entries);
    Map<? extends K, ? extends V> " K K V V".


    PECS - Producer Extends Consumer Super
    , , , .

    Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

    (, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
    , ? : , - , , . , , T.

    Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

    , , - , , ( , ) - .

    - wildcard- , wildcard- .

    PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
    getAll(Collection) - , .

    Map<K, V> getAll(Collection<K> keys);



    Map<K, V> getAll(Collection<? extends K> keys);
    , API! (, !)


    . - :

    interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
    , , , :

    interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
    , ouput-, ( Java ).


    PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


    Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
  2. Predicate Predicate, :

    no suitable method found for getAll(Predicate<Vehicle>)
    , , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

    final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
    ! :

    Collection<V> getAll(Predicate<? super V> p);
    Predicate<? super V> " V V ( Object)". , . Vehicle :

    MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
    , .

    ? extends T
    , . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

    MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
    , , , , wildcard ? extends T :

    void putAll(Map<? extends K, ? extends V> entries);
    Map<? extends K, ? extends V> " K K V V".


    PECS - Producer Extends Consumer Super
    , , , .

    Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

    (, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
    , ? : , - , , . , , T.

    Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

    , , - , , ( , ) - .

    - wildcard- , wildcard- .

    PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
    getAll(Collection) - , .

    Map<K, V> getAll(Collection<K> keys);



    Map<K, V> getAll(Collection<? extends K> keys);
    , API! (, !)


    . - :

    interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
    , , , :

    interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
    , ouput-, ( Java ).


    PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


    Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Source: https://habr.com/ru/post/J207360/


All Articles