Guest User

Untitled

a guest
Jul 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import java.io.Serializable;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.NoSuchElementException;
  6. import java.util.function.Function;
  7.  
  8. class Teddy {
  9. private final String name;
  10.  
  11. Teddy(String name) {
  12. this.name = name;
  13. }
  14.  
  15. @Override
  16. public String toString() {
  17. return String.format("{ Teddy: %s }", this.name);
  18. }
  19. }
  20.  
  21. class Box<T> {
  22. private T value;
  23.  
  24. Box() {
  25. this.value = null;
  26. }
  27.  
  28. Box(T value) {
  29. this.value = value;
  30. }
  31.  
  32. public void add(T thing) {
  33. if (this.value != null) {
  34. System.out.println("No empty space for you!");
  35. } else {
  36. this.value = thing;
  37. }
  38. }
  39.  
  40. public static <T> Box<T> of(T thing){
  41. return new Box<>(thing);
  42. }
  43.  
  44. public static <T extends Number> Box<T> ofNumber(T thing){
  45. return new Box<>(thing);
  46. }
  47.  
  48. @Override
  49. public String toString() {
  50. if (this.value == (null)) {
  51. return "[]";
  52. }
  53. return String.format("[%s]", this.value);
  54. }
  55.  
  56. public <R> Box<R> manipulate(Function<T,R> transform){
  57. R result = transform.apply(this.value);
  58. return new Box<>(result);
  59. }
  60. }
  61.  
  62. public class GenericsBruhh {
  63. public static void main(String[] args) {
  64. Box<Integer> integerBox = Box.ofNumber(1);
  65. Function<Integer, Double> incOnePointOne = (v) -> v + 1.1;
  66. Box<Double> doubleBox = integerBox.manipulate(incOnePointOne);
  67. System.out.println(new Box<>(doubleBox));
  68. }
  69. }
Add Comment
Please, Sign In to add comment