Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Illegal code - because otherwise life would be Bad
- List<Dog> dogs = new ArrayList<Dog>(); // ArrayList implements List
- List<Animal> animals = dogs; // Awooga awooga
- animals.add(new Cat());
- Dog dog = dogs.get(0); // This should be safe, right?
- Object[] objects = new String[10];
- objects[0] = Boolean.FALSE;
- add(List<Animal>){//You can add List<Dog or List<Cat> and this will compile as per rules of polymorphism}
- public void passOn(Consumer<Animal> consumer, Supplier<Animal> supplier) {
- consumer.accept(supplier.get());
- }
- public <A extends Animal> void passOn(Consumer<A> consumer, Supplier<? extends A> supplier) {
- consumer.accept(supplier.get());
- }
- public <A extends Animal> void passOn(Consumer<? super A> consumer, Supplier<A> supplier) {
- consumer.accept(supplier.get());
- }
- public <A extends Animal> void passOn(Consumer<? super A> consumer, Supplier<? extends A> supplier) {
- consumer.accept(supplier.get());
- }
- public interface Animal {
- String getName();
- String getVoice();
- }
- public class Dog implements Animal{
- @Override
- String getName(){return "Dog";}
- @Override
- String getVoice(){return "woof!";}
- List <Animal> animalGroup = new ArrayList<Animal>();
- animalGroup.add(new Dog());
- // All compiles but throws ArrayStoreException at runtime at last line
- Dog[] dogs = new Dog[10];
- Animal[] animals = dogs; // compiles
- animals[0] = new Cat(); // throws ArrayStoreException at runtime
- List<Dog> dogs = new ArrayList<>();
- List<Animal> animals = dogs; // compile-time error, otherwise heap pollution
- animals.add(new Cat());
- (List<Animal>) (List<?>) dogs
- public abstract class Shape {
- public abstract void draw(Canvas c);
- }
- public class Circle extends Shape {
- private int x, y, radius;
- public void draw(Canvas c) {
- ...
- }
- }
- public class Rectangle extends Shape {
- private int x, y, width, height;
- public void draw(Canvas c) {
- ...
- }
- }
- // drawAll method call
- drawAll(circleList);
- public void drawAll(List<Shape> shapes) {
- shapes.add(new Rectangle());
- }
- /**Could use Collection<? extends Object> and that is the better choice.
- * But I am doing this to illustrate how to use DownCastCollection. **/
- public static void print(Collection<Object> col){
- for(Object obj : col){
- System.out.println(obj);
- }
- }
- public static void main(String[] args){
- ArrayList<String> list = new ArrayList<>();
- list.addAll(Arrays.asList("a","b","c"));
- print(new DownCastCollection<Object>(list));
- }
- import java.util.AbstractCollection;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class DownCastCollection<E> extends AbstractCollection<E> implements Collection<E> {
- private Collection<? extends E> delegate;
- public DownCastCollection(Collection<? extends E> delegate) {
- super();
- this.delegate = delegate;
- }
- @Override
- public int size() {
- return delegate ==null ? 0 : delegate.size();
- }
- @Override
- public boolean isEmpty() {
- return delegate==null || delegate.isEmpty();
- }
- @Override
- public boolean contains(Object o) {
- if(isEmpty()) return false;
- return delegate.contains(o);
- }
- private class MyIterator implements Iterator<E>{
- Iterator<? extends E> delegateIterator;
- protected MyIterator() {
- super();
- this.delegateIterator = delegate == null ? null :delegate.iterator();
- }
- @Override
- public boolean hasNext() {
- return delegateIterator != null && delegateIterator.hasNext();
- }
- @Override
- public E next() {
- if(!hasNext()) throw new NoSuchElementException("The iterator is empty");
- return delegateIterator.next();
- }
- @Override
- public void remove() {
- delegateIterator.remove();
- }
- }
- @Override
- public Iterator<E> iterator() {
- return new MyIterator();
- }
- @Override
- public boolean add(E e) {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean remove(Object o) {
- if(delegate == null) return false;
- return delegate.remove(o);
- }
- @Override
- public boolean containsAll(Collection<?> c) {
- if(delegate==null) return false;
- return delegate.containsAll(c);
- }
- @Override
- public boolean addAll(Collection<? extends E> c) {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean removeAll(Collection<?> c) {
- if(delegate == null) return false;
- return delegate.removeAll(c);
- }
- @Override
- public boolean retainAll(Collection<?> c) {
- if(delegate == null) return false;
- return delegate.retainAll(c);
- }
- @Override
- public void clear() {
- if(delegate == null) return;
- delegate.clear();
- }
Add Comment
Please, Sign In to add comment