Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.NoSuchElementException;
- import java.util.function.Function;
- class Teddy {
- private final String name;
- Teddy(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return String.format("{ Teddy: %s }", this.name);
- }
- }
- class Box<T> {
- private T value;
- Box() {
- this.value = null;
- }
- Box(T value) {
- this.value = value;
- }
- public void add(T thing) {
- if (this.value != null) {
- System.out.println("No empty space for you!");
- } else {
- this.value = thing;
- }
- }
- public static <T> Box<T> of(T thing){
- return new Box<>(thing);
- }
- public static <T extends Number> Box<T> ofNumber(T thing){
- return new Box<>(thing);
- }
- @Override
- public String toString() {
- if (this.value == (null)) {
- return "[]";
- }
- return String.format("[%s]", this.value);
- }
- public <R> Box<R> manipulate(Function<T,R> transform){
- R result = transform.apply(this.value);
- return new Box<>(result);
- }
- }
- public class GenericsBruhh {
- public static void main(String[] args) {
- Box<Integer> integerBox = Box.ofNumber(1);
- Function<Integer, Double> incOnePointOne = (v) -> v + 1.1;
- Box<Double> doubleBox = integerBox.manipulate(incOnePointOne);
- System.out.println(new Box<>(doubleBox));
- }
- }
Add Comment
Please, Sign In to add comment