
com.jongsoft.lang.collection.impl.TreeSet Maven / Gradle / Ivy
The newest version!
/*
* The MIT License
*
* Copyright 2016-2019 Jong Soft.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.jongsoft.lang.collection.impl;
import com.jongsoft.lang.collection.Iterator;
import com.jongsoft.lang.collection.Sequence;
import com.jongsoft.lang.collection.Traversable;
import com.jongsoft.lang.collection.Tree;
import com.jongsoft.lang.collection.support.Collections;
import com.jongsoft.lang.control.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class TreeSet implements Tree {
private T value;
private String label;
private Tree parent;
private NodeCollection children;
public TreeSet(String label, T value) {
this.value = value;
this.label = label;
this.children = new NodeCollectionImpl<>(new Object[0]);
}
public TreeSet(String label, T value, Tree parent) {
this.value = value;
this.label = label;
this.parent = parent;
this.children = new NodeCollectionImpl<>(new Object[0]);
}
public TreeSet(String label, T value, Iterable> children) {
this.value = value;
this.label = label;
this.children = new NodeCollectionImpl<>(com.jongsoft.lang.Collections.Set(children)
.iterator()
.toNativeArray());
}
@Override
public Tree parent() {
return parent;
}
@Override
public NodeCollection children() {
return children;
}
@Override
public String label() {
return label;
}
@Override
public Tree appendChild(final String label, final T child) {
final Tree childNode = new TreeSet<>(label, child, this);
Object[] rawChildren = Iterator.concat(this.children.iterator(), com.jongsoft.lang.Collections.>Iterator(childNode))
.toNativeArray();
this.children = new NodeCollectionImpl<>(rawChildren);
return this;
}
@Override
public T get() {
return value;
}
@Override
public boolean isSingleValued() {
return children.isEmpty();
}
@Override
public Iterator iterator() {
return foldLeft(com.jongsoft.lang.Collections.List(), Sequence::append)
.iterator();
}
@Override
public Tree map(Function mapper) {
return com.jongsoft.lang.Collections.Tree(label, mapper.apply(value), children.map(t -> t.map(mapper)));
}
@Override
public Tree orElse(final Iterable extends T> other) {
return this;
}
@Override
public Tree orElse(final Supplier extends Iterable extends T>> supplier) {
return this;
}
@Override
public Traversable filter(Predicate predicate) {
throw new UnsupportedOperationException();
}
@Override
public Traversable reject(Predicate predicate) {
throw new UnsupportedOperationException();
}
@Override
public Optional first(Predicate predicate) {
return foldLeft(com.jongsoft.lang.Collections.List(), Sequence::append)
.first(predicate);
}
@Override
public Optional last(Predicate predicate) {
return foldLeft(com.jongsoft.lang.Collections.List(), Sequence::append)
.last(predicate);
}
@Override
public U foldLeft(U start, BiFunction super U, ? super T, ? extends U> combiner) {
U x = start;
x = combiner.apply(x, get());
for (Tree child : children) {
x = child.foldLeft(x, combiner);
}
return x;
}
@Override
public U foldRight(U start, BiFunction super T, ? super U, ? extends U> combiner) {
return foldLeft(start, (y, x) -> combiner.apply(x, y));
}
@Override
public T reduceLeft(BiFunction super T, ? super T, ? extends T> reducer) {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("Tree: {value:")
.append(get())
.append(", label: ")
.append(label);
builder.append(Collections.textValueOf(", children:", children));
builder.append("}");
return builder.toString();
}
private static class NodeCollectionImpl extends HashSet> implements NodeCollection {
public NodeCollectionImpl(final Object[] delegate) {
super(delegate);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy