Skip to content

Instantly share code, notes, and snippets.

@dzharkov
Created April 1, 2017 10:00
Show Gist options
  • Save dzharkov/3f5e8b47952477272d0de05a5624204d to your computer and use it in GitHub Desktop.
Save dzharkov/3f5e8b47952477272d0de05a5624204d to your computer and use it in GitHub Desktop.
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