Created
June 1, 2013 08:52
-
-
Save prietopa/5689739 to your computer and use it in GitHub Desktop.
CRUD con List, implementarlo con Collection<?>
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 net.josemanuel.prietopalacios.util; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class CrudList<T> { | |
public List<T> create(){ | |
return new ArrayList<T>(); | |
} | |
public T read(List<T> list, int index){ | |
if(list == null) return null; | |
if(index < 0) return null; | |
if(list.size() > 1){ | |
return list.get(index); | |
}else return null; | |
} | |
public List<T> update(List<T> list, T data, int index){ | |
if(list == null) return null; | |
if(data == null) return null; | |
if(index < 0) return null; | |
if(list.size() > 1){ | |
list.remove(index); | |
list.add(data); | |
return list; | |
}else{ | |
list = create(); | |
list.add(data); | |
return list; | |
} | |
} | |
public List<T> delete(List<T> list, int index){ | |
if(list == null) return null; | |
if(index < 0) return null; | |
if(list.size() > 1){ | |
list.remove(index); | |
return list; | |
}else{ | |
return create(); | |
} | |
} | |
public List<T> delete(List<T> list, T data){ | |
if(list == null) return null; | |
if(data == null) return null; | |
if(list.size() > 1){ | |
for (int i = 0; i < list.size(); i++) { | |
T t = list.get(i); | |
if(t.equals(data)){ | |
return delete(list, i); | |
} | |
} | |
return list; | |
}else{ | |
return create(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment