Last active
August 27, 2016 03:34
-
-
Save dfparker2002/e1e85d3b0e580052ac7a6eac3c94d92c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* adapted from example @ http://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super/2723538#2723538 | |
* Get And Put Principle (From Java Generics and Collections) | |
* | |
* It states, | |
* use an extends wildcard when you only get values out of a structure | |
* use a super wildcard when you only put values into a structure | |
* **** and don’t use a wildcard when you both get and put. *** | |
* | |
*/ | |
import java.util.ArrayList; | |
import java.util.List; | |
public class TestContraVariance { | |
List list = new ArrayList<>(); | |
/* | |
* Example for an upper bound wildcard We can not safely add, because we may | |
* have `List<Circle>`, `List<Square>`and List<Rectangle>. | |
* | |
*/ | |
public void testCoVariance(List<? extends Shape> list) { | |
// list.add(new Shape()); // does not compile | |
// list.add(new Circle()); // does not compile | |
// list.add(new Square()); // does not compile | |
// list.add(new Rectangle()); // does not compile | |
Shape shape = list.get(0);// compiles | |
list.stream().forEach(o -> o.draw()); // does not compile | |
} | |
/* | |
* Example for a lower bound wildcard We can not safely get, because we may | |
* have `List<Circle>`, `List<Square>`and List<Rectangle>. | |
*/ | |
public void testContraVariance() { | |
list.add(new Shape());// compiles | |
list.add(new Circle());// compiles | |
list.add(new Square());// compiles | |
list.add(new Rectangle());// compiles | |
// Shape shape = list.get(0); // does not compile | |
// list.stream().forEach(o -> o.draw() ); // does not compile | |
} | |
public static void main(String[] args) { | |
TestContraVariance o = new TestContraVariance(); | |
o.testContraVariance(); | |
o.testCoVariance(o.list); | |
o = null; | |
} | |
} | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
public class Square extends Shape { | |
private static String description = "square\n" | |
+ "skwer/\n" | |
+ "noun\n" | |
+ "1a. plane figure with four equal straight sides and four right angles.\n" | |
+ "2. the product of a number multiplied by itself.\n" | |
+ "\"a circle's area is proportional to the square of its radius\"\n" | |
+"adjective\n" | |
+ "1. having the shape or approximate shape of a square.\n" | |
+ "\"a square table\n" | |
+"synonyms: quadrilateral, rectangular, oblong, right-angled, at right angles, perpendicular; More\n" | |
+ "2. denoting a unit of measurement equal to the area of a square whose side is of the unit specified.\n" | |
+ "\"30,000 square feet of new gallery space\"\n" | |
+"adverb\n" | |
+ "1. directly; straight.\n" | |
+ "\"it hit me square in the forehead\"\n" | |
+ "verb\n" | |
+ "1. make square or rectangular; give a square or rectangular cross section to.\n" | |
+ "\"you can square off the other edge\"\n" | |
+ "2. multiply (a number) by itself.\n" | |
+ "\"5 squared equals 25\n"; | |
public void draw() { | |
System.out.printf("%s\n%s", this.getClass().getName(), description); | |
} | |
} | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
public class Shape { | |
private static String description = "shape\n" | |
+ "SHāp/Submit\n" | |
+ "noun\n" | |
+ "1. the external form or appearance characteristic of someone or something; the outline of an area or figure.\n" | |
+ "\"she liked the shape of his nose\"\n" | |
+ "synonyms: form, appearance, configuration, formation, structure; More\n" | |
+ "2. the particular condition or state of someone or something.\n" | |
+ "\"he was in no shape to drive\"\n" | |
+ "synonyms: condition, health, fettle, order\n" | |
+ "\"you're in pretty good shape\"\n" | |
+ "verb\n" | |
+ "1. give a particular shape or form to.\n" | |
+ "\"most caves are shaped by the flow of water through limestone\"\n" | |
+ "synonyms: form, fashion, make, mold, model, cast; More\n"; | |
public void draw() { | |
System.out.printf("%s\n%s", this.getClass().getName(), description ); | |
} | |
@Override | |
public String toString() { | |
return ToStringBuilder.reflectionToString(this); | |
} | |
} | |
public class Rectangle extends Shape { | |
private static String description = "rec·tan·gle\n" | |
+ "ˈrekˌtaNGɡəl/\n" | |
+ "noun\n" | |
+ "a plane figure with four straight sides and four right angles, especially one with unequal adjacent sides, in contrast to a square."; | |
public void draw() { | |
System.out.printf("%s\n%s", this.getClass().getName(), description); | |
} | |
}public class Circle extends Shape { | |
private static String description = "cir·cle" | |
+ "ˈsərk(ə)l/\n" | |
+ "noun\n" | |
+ "1a. round plane figure whose boundary (the circumference) consists of points equidistant from a fixed point (the center).\n" | |
+ "2. a group of people with shared professions, interests, or acquaintances.\n" | |
+ "\"she did not normally move in such exalted circles\n" | |
+ "synonyms: group, set, company, coterie, clique; More \n" | |
+ "verb\n" | |
+ "1. move all the way around (someone or something), especially more than once. \n" | |
+ "\"the two dogs circle each other with hackles raised\n" | |
+"synonyms: wheel, move around, revolve, rotate, whirl, spiral\n"; | |
public void draw() { | |
System.out.printf("%s\n%s", this.getClass().getName(), description ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment