com.fluxtion.ext.streaming.api.SubList Maven / Gradle / Ivy
/*
* Copyright (c) 2020, V12 Technology Ltd.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package com.fluxtion.ext.streaming.api;
import com.fluxtion.api.SepContext;
import com.fluxtion.api.annotations.Initialise;
import com.fluxtion.api.annotations.OnEvent;
import com.fluxtion.api.annotations.OnParentUpdate;
import java.util.Collections;
import java.util.List;
import lombok.Data;
/**
* A filtered view of a parent WrappedList
*
* @author Greg Higgins [email protected]
*/
@Data
public class SubList implements WrappedList {
private final WrappedList parent;
private boolean recalculated;
private final int start;
private final int stop;
private transient int sourceSize;
private List unmodifiableCollection;
@OnParentUpdate
public void parentUpdate(WrappedList parent) {
updatePointers();
recalculated = true;
}
@OnEvent
public boolean onEvent(){
return true;
}
@Override
public List collection() {
if(unmodifiableCollection == null | !recalculated){
return Collections.EMPTY_LIST;
}
return unmodifiableCollection;
}
@Override
public WrappedList top(int n) {
return SepContext.service().addOrReuse(new SubList<>(this, 0, n));
}
@Override
public WrappedList last(int n) {
return SepContext.service().addOrReuse(new SubList<>(this, -n, 0));
}
@Override
public WrappedList skip(int n) {
return SepContext.service().addOrReuse(new SubList<>(this, n, -n));
}
@Override
public void reset() {
parent.reset();
init();
}
@Override
public WrappedList reverse() {
parent.reverse();
return this;
}
@Initialise
public void init() {
sourceSize = -1;
recalculated = false;
updatePointers();
}
@Override
public WrappedList resetNotifier(Object resetNotifier){
return this;
}
private void updatePointers() {
int size = parent.size();
// if (sourceSize < 0 | size != sourceSize) {
if (start < 0) {
//last
unmodifiableCollection = parent.subList(Math.max((size+start), 0), size);
} else if (stop < 0) {
//skip
unmodifiableCollection = parent.subList(Math.min(size, start), size);
}else{
//normal list
unmodifiableCollection = parent.subList(0, Math.min(stop, size));
}
// }
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy