Created
April 1, 2017 10:00
This file contains hidden or 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
package ru.spbau.mit; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Main { | |
static void baz(ArrayList z) { | |
z.add(1); | |
z.add(""); | |
z.get(2); | |
foo(z); | |
} | |
static void foo(List<Integer> x) { | |
List<Number> y = (List)x; | |
y.add(3.14); | |
Main3103.bar(x, new ArrayList<>()); | |
@SuppressWarnings("unchecked") | |
A<Integer> a = new A().foo(1).foo(""); | |
System.out.println(a.x.intValue()); | |
List<Integer> z1 = new ArrayList<>(); | |
} | |
static class A<T> { | |
T x; | |
A() { | |
} | |
A<T> foo(T x) { | |
this.x = x; | |
return this; | |
} | |
} | |
static <F> void bar(List<? extends Number> x, List<F> y) { | |
for (Number number : x) { | |
System.out.println(number); | |
} | |
//x.add(1); | |
} | |
public static void main(String[] args) { | |
List<Integer> integers = new ArrayList<>(); | |
integers.add(1); | |
foo(integers); | |
System.out.println(integers.get(1).intValue()); | |
} | |
class Box<T> { | |
T reference; | |
} | |
abstract static class Function1<X, Y> { | |
abstract Y apply(X x); | |
<Z> Function1<Z, Y> compose(Function1<Z, ? extends X> g) { | |
return new Function1<Z, Y>() { | |
@Override | |
Y apply(Z z) { | |
return Function1.this.apply(g.apply(z)); | |
} | |
}; | |
} | |
} | |
static void foobar() { | |
Function1<Integer, Double> g = new Function1<Integer, Double>() { | |
@Override | |
Double apply(Integer integer) { | |
return integer.doubleValue(); | |
} | |
}; | |
Function1<Number, String> f = new Function1<Number, String>() { | |
@Override | |
String apply(Number number) { | |
return number.toString(); | |
} | |
}; | |
f.compose(g); | |
Function1<Object, Boolean> u = new Function1<Object, Boolean>() { | |
@Override | |
Boolean apply(Object o) { | |
return o.hashCode() % 2 == 0; | |
} | |
}; | |
Function1<CharSequence, Boolean> u2 = new Function1<CharSequence, Boolean>() { | |
@Override | |
Boolean apply(CharSequence charSequence) { | |
return charSequence.length() > 2; | |
} | |
}; | |
// X: String extends X | |
Function1<? super String, Boolean> y = u; | |
y = u2; | |
y.apply(""); | |
y.apply(1); | |
List<? super CharSequence> q = new ArrayList<>(); | |
q.add(""); | |
q.get(9); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment