Guest User

Untitled

a guest
Dec 14th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. // Illegal code - because otherwise life would be Bad
  2. List<Dog> dogs = new ArrayList<Dog>(); // ArrayList implements List
  3. List<Animal> animals = dogs; // Awooga awooga
  4. animals.add(new Cat());
  5. Dog dog = dogs.get(0); // This should be safe, right?
  6.  
  7. Object[] objects = new String[10];
  8. objects[0] = Boolean.FALSE;
  9.  
  10. add(List<Animal>){//You can add List<Dog or List<Cat> and this will compile as per rules of polymorphism}
  11.  
  12. public void passOn(Consumer<Animal> consumer, Supplier<Animal> supplier) {
  13. consumer.accept(supplier.get());
  14. }
  15.  
  16. public <A extends Animal> void passOn(Consumer<A> consumer, Supplier<? extends A> supplier) {
  17. consumer.accept(supplier.get());
  18. }
  19.  
  20. public <A extends Animal> void passOn(Consumer<? super A> consumer, Supplier<A> supplier) {
  21. consumer.accept(supplier.get());
  22. }
  23.  
  24. public <A extends Animal> void passOn(Consumer<? super A> consumer, Supplier<? extends A> supplier) {
  25. consumer.accept(supplier.get());
  26. }
  27.  
  28. public interface Animal {
  29. String getName();
  30. String getVoice();
  31. }
  32. public class Dog implements Animal{
  33. @Override
  34. String getName(){return "Dog";}
  35. @Override
  36. String getVoice(){return "woof!";}
  37.  
  38. List <Animal> animalGroup = new ArrayList<Animal>();
  39. animalGroup.add(new Dog());
  40.  
  41. // All compiles but throws ArrayStoreException at runtime at last line
  42. Dog[] dogs = new Dog[10];
  43. Animal[] animals = dogs; // compiles
  44. animals[0] = new Cat(); // throws ArrayStoreException at runtime
  45.  
  46. List<Dog> dogs = new ArrayList<>();
  47. List<Animal> animals = dogs; // compile-time error, otherwise heap pollution
  48. animals.add(new Cat());
  49.  
  50. (List<Animal>) (List<?>) dogs
  51.  
  52. public abstract class Shape {
  53. public abstract void draw(Canvas c);
  54. }
  55.  
  56. public class Circle extends Shape {
  57. private int x, y, radius;
  58. public void draw(Canvas c) {
  59. ...
  60. }
  61. }
  62.  
  63. public class Rectangle extends Shape {
  64. private int x, y, width, height;
  65. public void draw(Canvas c) {
  66. ...
  67. }
  68. }
  69.  
  70. // drawAll method call
  71. drawAll(circleList);
  72.  
  73.  
  74. public void drawAll(List<Shape> shapes) {
  75. shapes.add(new Rectangle());
  76. }
  77.  
  78. /**Could use Collection<? extends Object> and that is the better choice.
  79. * But I am doing this to illustrate how to use DownCastCollection. **/
  80.  
  81. public static void print(Collection<Object> col){
  82. for(Object obj : col){
  83. System.out.println(obj);
  84. }
  85. }
  86. public static void main(String[] args){
  87. ArrayList<String> list = new ArrayList<>();
  88. list.addAll(Arrays.asList("a","b","c"));
  89. print(new DownCastCollection<Object>(list));
  90. }
  91.  
  92. import java.util.AbstractCollection;
  93. import java.util.Collection;
  94. import java.util.Iterator;
  95. import java.util.NoSuchElementException;
  96.  
  97. public class DownCastCollection<E> extends AbstractCollection<E> implements Collection<E> {
  98. private Collection<? extends E> delegate;
  99.  
  100. public DownCastCollection(Collection<? extends E> delegate) {
  101. super();
  102. this.delegate = delegate;
  103. }
  104.  
  105. @Override
  106. public int size() {
  107. return delegate ==null ? 0 : delegate.size();
  108. }
  109.  
  110. @Override
  111. public boolean isEmpty() {
  112. return delegate==null || delegate.isEmpty();
  113. }
  114.  
  115. @Override
  116. public boolean contains(Object o) {
  117. if(isEmpty()) return false;
  118. return delegate.contains(o);
  119. }
  120. private class MyIterator implements Iterator<E>{
  121. Iterator<? extends E> delegateIterator;
  122.  
  123. protected MyIterator() {
  124. super();
  125. this.delegateIterator = delegate == null ? null :delegate.iterator();
  126. }
  127.  
  128. @Override
  129. public boolean hasNext() {
  130. return delegateIterator != null && delegateIterator.hasNext();
  131. }
  132.  
  133. @Override
  134. public E next() {
  135. if(!hasNext()) throw new NoSuchElementException("The iterator is empty");
  136. return delegateIterator.next();
  137. }
  138.  
  139. @Override
  140. public void remove() {
  141. delegateIterator.remove();
  142.  
  143. }
  144.  
  145. }
  146. @Override
  147. public Iterator<E> iterator() {
  148. return new MyIterator();
  149. }
  150.  
  151.  
  152.  
  153. @Override
  154. public boolean add(E e) {
  155. throw new UnsupportedOperationException();
  156. }
  157.  
  158. @Override
  159. public boolean remove(Object o) {
  160. if(delegate == null) return false;
  161. return delegate.remove(o);
  162. }
  163.  
  164. @Override
  165. public boolean containsAll(Collection<?> c) {
  166. if(delegate==null) return false;
  167. return delegate.containsAll(c);
  168. }
  169.  
  170. @Override
  171. public boolean addAll(Collection<? extends E> c) {
  172. throw new UnsupportedOperationException();
  173. }
  174.  
  175. @Override
  176. public boolean removeAll(Collection<?> c) {
  177. if(delegate == null) return false;
  178. return delegate.removeAll(c);
  179. }
  180.  
  181. @Override
  182. public boolean retainAll(Collection<?> c) {
  183. if(delegate == null) return false;
  184. return delegate.retainAll(c);
  185. }
  186.  
  187. @Override
  188. public void clear() {
  189. if(delegate == null) return;
  190. delegate.clear();
  191.  
  192. }
Add Comment
Please, Sign In to add comment