Guest User

Untitled

a guest
Oct 21st, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. List list1 = new ArrayList();
  2. list1.add("0");
  3. List list2 = new LinkedList();
  4. list2.add("1");
  5. list3.add("2");
  6. List list3 = new CompositeList(list1, list2...)
  7.  
  8. assertEquals("0", list3.get(0));
  9. assertEquals("1", list3.get(1));
  10. assertEquals("2", list3.get(2));
  11.  
  12. /**
  13. * A list composed of zero to many child lists. Additions occur in the first
  14. * acceptable list: if an insertion is attempted at an index that lies on a break
  15. * between lists, the insert occurs in the first list. Modifications are undefined
  16. * if list of lists has no elements.
  17. * @param <T> Type of element stored in list.
  18. */
  19. public class CompositeList<T> extends AbstractList<T> {
  20. // member variables -------------------------------------------------------------------
  21. private Collection<List<T>> mLists;
  22.  
  23. // constructors -----------------------------------------------------------------------
  24. public CompositeList(Collection<List<T>> pLists) {mLists = pLists;}
  25.  
  26. // methods ----------------------------------------------------------------------------
  27. /** Sum of sizes of component lists. */
  28. public int size() {return mLists.stream().mapToInt(Collection::size).sum();}
  29.  
  30. @Override public T get(int pIdx) {
  31. final Map.Entry<List<T>,Integer> m = findIndex(pIdx);
  32. return m.getKey().get(m.getValue());
  33. }
  34.  
  35. /**
  36. * If add could occur at end of one list or beginning of the next, the former
  37. * behavior is guaranteed.
  38. */
  39. @Override public void add(int pIdx, T pElement) {
  40. if (pIdx == 0) {
  41. mLists.iterator().next().add(0, pElement);
  42. } else {
  43. // find prior object
  44. final Map.Entry<List<T>,Integer> m = findIndex(pIdx - 1);
  45. m.getKey().add(m.getValue() + 1, pElement);
  46. }
  47. }
  48.  
  49. @Override public T remove(int pIdx) {
  50. final Map.Entry<List<T>,Integer> m = findIndex(pIdx);
  51.  
  52. // don't auto-box because remove(Object) and remove(int) can be confused
  53. return m.getKey().remove(m.getValue().intValue());
  54. }
  55.  
  56. @Override public T set(int pIdx, T pElement) {
  57. final Map.Entry<List<T>,Integer> m = findIndex(pIdx);
  58. return m.getKey().set(m.getValue(), pElement);
  59. }
  60.  
  61. /** More efficient than superclass implementation. */
  62. @Override public Iterator<T> iterator() {
  63. return Iterators.concat(
  64. Collections2.transform(mLists, Collection::iterator).iterator()
  65. );
  66. }
  67.  
  68. @Override public void clear() {mLists.forEach(Collection::clear);}
  69.  
  70. /**
  71. * Identify list and index that composite index refers to. For
  72. * [A], [], [], [B, C]; composite index 1 would return the fourth list
  73. * mapped to the number 0.
  74. */
  75. private Map.Entry<List<T>,Integer> findIndex(int pCompositeIdx) {
  76. // composite index of list's starting point
  77. int listStart = 0;
  78. for (final List<T> list : mLists) {
  79. if (listStart + list.size() > pCompositeIdx) {
  80. return new AbstractMap.SimpleImmutableEntry<>(
  81. list, pCompositeIdx - listStart
  82. );
  83. }
  84. listStart += list.size();
  85. }
  86. throw new IndexOutOfBoundsException(pCompositeIdx + " >= " + size());
  87. }
  88. }
Add Comment
Please, Sign In to add comment