Guest User

Untitled

a guest
Mar 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. public class UnaClaseCualquiera<String> {
  2. }
  3.  
  4. public class UnaClaseCualquiera <String> {
  5. }
  6.  
  7. public class Caja<T> {
  8.  
  9. private T t;
  10.  
  11. public T get() { return t; }
  12. public void set(T t) { this.t = t; }
  13. }
  14.  
  15.  
  16. public interface Par<K, V> {
  17. public K getKey();
  18. public V getValue();
  19. }
  20.  
  21.  
  22. public class ParOrdenado<K, V> implements Par<K, V> {
  23.  
  24. private K key;
  25. private V value;
  26.  
  27. public ParOrdenado(K key, V value) {
  28. this.key = key;
  29. this.value = value;
  30. }
  31.  
  32. public K getKey() { return key; }
  33. public V getValue() { return value; }
  34. }
  35.  
  36. Caja<Integer> integerBox1 = new Caja<Integer>();
  37. Caja<Integer> integerBox2 = new Caja<>();
  38. ParOrdenado<String, Integer> p1 = new ParOrdenado <>("UnPar", 22);
  39.  
  40. Caja rawCaja = new Caja();
  41.  
  42. public static <K, V> boolean comparar(Par<K, V> p1, Par<K, V> p2) {
  43. return p1.getKey().equals(p2.getKey()) && p1.getValue().equals(p2.getValue());
  44. }
  45.  
  46. Par<Integer, String> p1 = new ParOrdenado <>(1, "apple");
  47. Par<Integer, String> p2 = new ParOrdenado <>(2, "pear");
  48. boolean igual = Util.<Integer, String>comparar(p1, p2);
  49.  
  50. boolean igual = Util.comparar(p1, p2);
  51.  
  52. public class CajaBounds<T> {
  53.  
  54. private T t;
  55.  
  56. public void set(T t) {
  57. this.t = t;
  58. }
  59.  
  60. public T get() {
  61. return t;
  62. }
  63.  
  64. public <U extends Number> void inspect(U u){
  65. System.out.println("T: " + t.getClass().getName());
  66. System.out.println("U: " + u.getClass().getName());
  67. }
  68.  
  69. public static void main(String[] args) {
  70. Caja<Integer> integerCaja = new Caja<Integer>();
  71. integerCaja.set(new Integer(10));
  72. integerCaja.inspect("some text"); // error: this is still String!
  73. }
  74. }
  75.  
  76. <T extends B1 & B2 & B3>
  77.  
  78. Class A { /* ... */ }
  79. interface B { /* ... */ }
  80. interface C { /* ... */ }
  81.  
  82. class D <T extends A & B & C> { /* ... */ }
  83.  
  84. Object object = new Object();
  85. Integer integer = new Integer(10);
  86. object = integer;
  87.  
  88. interface PayloadList<E,P> extends List<E> {
  89. void setPayload(int index, P val);
  90. ...
  91. }
  92.  
  93. PayloadList<String,String>
  94. PayloadList<String,Integer>
  95. PayloadList<String,Exception>
  96.  
  97. public static void process(List<? extends Number> list) { /* ... */ }
  98.  
  99. List<? extends Integer> intList = new ArrayList<>();
  100. List<? extends Number> numList = intList;
  101.  
  102. // TypeErasure to Object
  103. public class Node<T> {
  104.  
  105. private T data;
  106. private Node<T> next;
  107.  
  108. public Node(T data, Node<T> next) }
  109. this.data = data;
  110. this.next = next;
  111. }
  112.  
  113. public T getData() { return data; }
  114. // ...
  115. }
  116.  
  117. // Node type erased
  118. public class Node {
  119.  
  120. private Object data;
  121. private Node next;
  122.  
  123. public Node(Object data, Node next) {
  124. this.data = data;
  125. this.next = next;
  126. }
  127.  
  128. public Object getData() { return data; }
  129. // ...
  130. }
  131.  
  132. // TypeErasure to Comparable
  133. public class Node<T extends Comparable<T>> {
  134.  
  135. private T data;
  136. private Node<T> next;
  137.  
  138. public Node(T data, Node<T> next) {
  139. this.data = data;
  140. this.next = next;
  141. }
  142.  
  143. public T getData() { return data; }
  144. // ...
  145. }
  146.  
  147. // Node type erased
  148. public class Node {
  149.  
  150. private Comparable data;
  151. private Node next;
  152.  
  153. public Node(Comparable data, Node next) {
  154. this.data = data;
  155. this.next = next;
  156. }
  157.  
  158. public Comparable getData() { return data; }
  159. // ...
  160. }
  161.  
  162. List<String> lista = new ArrayList<String>();
  163.  
  164. List<String> lista = new ArrayList<>();
  165.  
  166. List<Integer> listaEnteros = new ArrayList<>();
  167. List<Persona> listaPersonas = new ArrayList<>();
  168.  
  169. E: elemento de una colección.
  170. K: clave.
  171. N: número.
  172. T: tipo.
  173. V: valor.
  174. S, U, V etc: para segundos, terceros y cuartos tipos.
  175.  
  176. Comprobación de tipos más fuerte en tiempo de compilación.
  177. Eliminación de casts aumentando la legibilidad del código.
  178. Posibilidad de implementar algoritmos genéricos, con tipado seguro.
  179.  
  180. public void create(Persona persona) {
  181. getEntityManager().persist(persona );
  182. }
  183.  
  184. public void edit(Persona persona) {
  185. getEntityManager().merge(persona);
  186. }
  187.  
  188. public void create(Animales animales) {
  189. getEntityManager().persist(animales);
  190. }
  191.  
  192. public void edit(Animales animales) {
  193. getEntityManager().merge(animales);
  194. }
  195.  
  196. public abstract class AbstractFacade<T> {
  197.  
  198. protected abstract EntityManager getEntityManager();
  199.  
  200. public void create(T entity) {
  201. getEntityManager().persist(entity);
  202. }
  203.  
  204. public void edit(T entity) {
  205. getEntityManager().merge(entity);
  206. }
  207.  
  208. }
Add Comment
Please, Sign In to add comment