com.bestvike.linq.enumerable._SingleLinkedNode Maven / Gradle / Ivy
package com.bestvike.linq.enumerable;
import com.bestvike.collections.generic.Array;
/**
* Created by 许崇雷 on 2018-05-09.
*/
final class SingleLinkedNode {
private final TSource item;
private final SingleLinkedNode linked;
SingleLinkedNode(TSource item) {
this.linked = null;
this.item = item;
}
private SingleLinkedNode(SingleLinkedNode linked, TSource item) {
assert linked != null;
this.linked = linked;
this.item = item;
}
public TSource getItem() {
return this.item;
}
public SingleLinkedNode getLinked() {
return this.linked;
}
public SingleLinkedNode add(TSource item) {
return new SingleLinkedNode<>(this, item);
}
public int getCount() {
int count = 0;
for (SingleLinkedNode node = this; node != null; node = node.linked)
count++;
return count;
}
public SingleLinkedNode getNode(int index) {
assert index >= 0 && index < this.getCount();
SingleLinkedNode node = this;
for (; index > 0; index--) {
node = node.linked;
assert node != null;
}
return node;
}
public Array toArray(int count) {
assert count == this.getCount();
Object[] array = new Object[count];
int index = count;
for (SingleLinkedNode node = this; node != null; node = node.linked) {
--index;
array[index] = node.item;
}
assert index == 0;
return new Array<>(array);
}
}