com.xlrit.gears.base.collection.CollectionRef Maven / Gradle / Ivy
package com.xlrit.gears.base.collection;
import java.util.Comparator;
import java.util.List;
import lombok.RequiredArgsConstructor;
public interface CollectionRef extends HasElementType {
default CollectionRef sorted(Comparator comparator) {
return new SortedCollectionRef<>(this, comparator);
}
List getElements();
void updateElements(List newElements);
}
@RequiredArgsConstructor
class SortedCollectionRef implements CollectionRef {
private final CollectionRef delegate;
private final Comparator comparator;
@Override
public Class getElementType() {
return delegate.getElementType();
}
@Override
public List getElements() {
return delegate.getElements().stream().sorted(comparator).toList();
}
@Override
public void updateElements(List newElements) {
delegate.updateElements(newElements);
}
}