Questions tagged [pecs]

A java generics acronym for "Producer (uses) Extends, Consumer (uses) Super"

A good design pattern to follow when specifying java generic parameters is that the producers of objects specify their parameters as <T extends Something>, while the consumers of those same objects specify their parameters as <T super Something>.

See What is PECS (Producer Extends Consumer Super)? for more.

30 questions
1
vote
2 answers

PECS: How to convert a Consumer to a Producer?

I have a Restaurant that produces Meals. The kitchens is given plates that are Consumers. class Food{} class Bamboo extends Food {} interface Kitchen { void build(List dessert); } abstract class Restaurant { Kitchen…
Nicolas Zozol
  • 6,910
  • 3
  • 50
  • 74
1
vote
5 answers

Java Generics (bounded wildcards)

According to the book "Effective Java" of Joshua Bloch there is a rule about how/when use the bounded wildcards in generics. This rule is PECS (Producer-Extends, Comsumer-Super). When I study the following example: Stack numberStack = new…
LiTTle
  • 1,811
  • 1
  • 20
  • 37
0
votes
2 answers

Java's TreeSet.add() does not follow the PECS principle issue (Generics)

I have the following piece of code: public interface Segment extends Period { ... }; public class SegmentImpl_v1 implements Segment { ... }; public interface TimeLine> { ... }; public class TimeLineImpl
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
0
votes
1 answer

Parametrize generic list whilst filtering and mapping

I'm trying to grasp PECS idea in java and don't know, how to apply this idea in following example: package org.example; import java.util.List; import java.util.Optional; interface Command { String name();} class DeleteCommand implements Command { …
mtw
  • 144
  • 10
0
votes
2 answers

Kotlin and Java PECS

So my goal is use in Kotlin similar construction to Java PECS: List => MutableList When Jackson set after compiling data to this variable(list) it's ok. When I try to add item from Kotlin code ,…
faceoff
  • 151
  • 3
  • 12
0
votes
1 answer

Generics: LowerBounded wildcard vs UpperBounded wildcard

I understand the concept of PECS (Producer extends, Consumer super) but still have confusion regarding these notations: public class PECSTest { public static void main(String[] args) { //List ProducerList = new…
Manish
  • 1,274
  • 3
  • 22
  • 59
0
votes
1 answer

How to give default value for a optional with generic type in JAVA

Thanks a lot for all the answers/comments! Apologize for the bad code sample I put in the original question. I was trying to simpify my question but apprently it ended up rather confusing. I changed the code a little bit: Map
Sweden
  • 1
  • 1
0
votes
1 answer

When to use PECS when designing a library?

I am working on a library which makes heavy use of functional interfaces and currently struggle whether to apply PECS or not: Predicate Function BiFunction vs. Predicate Function BiFunction
roookeee
  • 1,710
  • 13
  • 24
0
votes
1 answer

How can I type for Collection?

I have a method look like this. public void some(..., Collection collection) { // WOOT, PECS!!! final Stream stream = getStream(); stream.collect(toCollection(() -> collection)); } And how can I make this method…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
0
votes
0 answers

PECS parametrized class

I have a little piece of code where i created my own java.util.stream realization. I need to parametrize it using PECS rule. But either I didn't understand PECS rule well or my class designed bad - I don't know how to correctly implement it. When…
ruslangm
  • 674
  • 7
  • 19
0
votes
1 answer

Java: Working with Generics and Maps without Casting / @SuppressWarnings

I now came several times across this problem and always kinda solved this with some casts and @SuppressWarnings annotations. The relevant interfaces / abstract classes: public abstract class Data { } public interface DataOperations { boolean…
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
0
votes
3 answers

Overcoming generics put-get rule

I have read about the generics get and put rule that should prevent you from adding a Banana to a List: public abstract class Fruit { } public class Banana extends Fruit { } public class Apple extends Fruit { } List
Matthieu
  • 2,736
  • 4
  • 57
  • 87
0
votes
3 answers

Java, generics and PECS: still having trouble understanding the C part; concrete example?

I'll post a single link here: Collections.sort(). There have been many posts on SO with regards to the PECS paradigm, including this one. In my own personal code, I use generics quite a lot, but have only ever had the use of the P part (that is,
fge
  • 119,121
  • 33
  • 254
  • 329
-1
votes
1 answer

Wildcard vs TypeParameter

class Employee { // valid } class Employee { // invalid } private static void test(List list1) { // valid } private static void test(List list1) { // invalid } what exactly…
user4811324
-1
votes
1 answer

How to remove 'unchecked' warning from my method?

I have following code private static class ParcelableParser { private ArrayList parse(List parcelables) { ArrayList parsedData = new ArrayList(); for(Parcelable parcelable : parcelables) { …
Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113
1
2