Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. public class ListCollectorJvm extends ListCollector {
  2. static final Set<Collector.Characteristics> CHARACTERLESS = Collections
  3. .unmodifiableSet(EnumSet.noneOf(Collector.Characteristics.class));
  4.  
  5. @Override
  6. public <T> Collector<T, ?, List<T>> toList() {
  7. return new CollectorImpl<>((Supplier<SpinyBuffer<T>>) SpinyBuffer::new,
  8. SpinyBuffer::add, (left, right) -> {
  9. left.addAll(right);
  10. return left;
  11. }, SpinyBuffer::toArrayList, CHARACTERLESS);
  12. }
  13.  
  14. static class SpinyBuffer<T> {
  15. ConcurrentLinkedQueue<T> queue = new ConcurrentLinkedQueue<>();
  16.  
  17. ConcurrentLinkedQueue<SpinyBuffer<T>> buffers = new ConcurrentLinkedQueue<>();
  18.  
  19. boolean addedOther = false;
  20.  
  21. private AtomicInteger elementSize = new AtomicInteger(0);
  22.  
  23. public SpinyBuffer() {
  24. }
  25.  
  26. public void add(T t) {
  27. if (addedOther) {
  28. int debug = 3;
  29. }
  30. elementSize.incrementAndGet();
  31. queue.add(t);
  32. }
  33.  
  34. public void addAll(SpinyBuffer<T> other) {
  35. addedOther = true;
  36. buffers.add(other);
  37. }
  38.  
  39. int size() {
  40. return elementSize.get()
  41. + buffers.stream().mapToInt(SpinyBuffer::size).sum();
  42. }
  43.  
  44. int size = elementSize.get();
  45.  
  46. List<T> toArrayList() {
  47. List<T> result = new ArrayList<>(size());
  48. addToList(result);
  49. return result;
  50. }
  51.  
  52. private void addToList(List<T> result) {
  53. result.addAll(queue);
  54. for (SpinyBuffer<T> buffer : buffers) {
  55. buffer.addToList(result);
  56. }
  57. }
  58. }
  59.  
  60. /**
  61. * Simple implementation class for {@code Collector}.
  62. *
  63. * @param <T>
  64. * the type of elements to be collected
  65. * @param <R>
  66. * the type of the result
  67. */
  68. static class CollectorImpl<T, A, R> implements Collector<T, A, R> {
  69. private final Supplier<A> supplier;
  70.  
  71. private final BiConsumer<A, T> accumulator;
  72.  
  73. private final BinaryOperator<A> combiner;
  74.  
  75. private final Function<A, R> finisher;
  76.  
  77. private final Set<Characteristics> characteristics;
  78.  
  79. CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator,
  80. BinaryOperator<A> combiner, Function<A, R> finisher,
  81. Set<Characteristics> characteristics) {
  82. this.supplier = supplier;
  83. this.accumulator = accumulator;
  84. this.combiner = combiner;
  85. this.finisher = finisher;
  86. this.characteristics = characteristics;
  87. }
  88.  
  89. @Override
  90. public BiConsumer<A, T> accumulator() {
  91. return accumulator;
  92. }
  93.  
  94. @Override
  95. public Supplier<A> supplier() {
  96. return supplier;
  97. }
  98.  
  99. @Override
  100. public BinaryOperator<A> combiner() {
  101. return combiner;
  102. }
  103.  
  104. @Override
  105. public Function<A, R> finisher() {
  106. return finisher;
  107. }
  108.  
  109. @Override
  110. public Set<Characteristics> characteristics() {
  111. return characteristics;
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement