com.fluxtion.ext.streaming.api.ArrayListWrappedCollection 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.NoEventReference;
import com.fluxtion.api.annotations.OnEvent;
import com.fluxtion.api.annotations.OnParentUpdate;
import com.fluxtion.api.annotations.SepNode;
import com.fluxtion.api.partition.LambdaReflection.SerializableFunction;
import com.fluxtion.ext.streaming.api.util.FunctionComparator;
import com.fluxtion.ext.streaming.api.window.WindowBuildOperations;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
/**
*
* @author Greg Higgins [email protected]
* @param
*/
@Slf4j
public class ArrayListWrappedCollection implements WrappedList {
private List unmodifiableCollection;
private final Wrapper wrappedSource;
@NoEventReference
@SepNode
private Comparator comparator;
private SerializableFunction comparingFunction;
private List collection;
private Object resetNotifier;
private boolean reset;
private boolean reversed;
public ArrayListWrappedCollection() {
this(null);
}
public ArrayListWrappedCollection(Wrapper wrappedSource) {
this.wrappedSource = wrappedSource;
reversed = false;
init();
}
@Override
public void reset() {
init();
}
@OnParentUpdate("resetNotifier")
public void resetNotification(Object resetNotifier) {
collection.clear();
reset = true;
}
@Override
public WrappedList resetNotifier(Object resetNotifier) {
this.resetNotifier = resetNotifier;
return this;
}
public Object getResetNotifier() {
return resetNotifier;
}
public void setResetNotifier(Object resetNotifier) {
this.resetNotifier = resetNotifier;
}
@Initialise
public final void init() {
this.collection = new ArrayList<>();
this.unmodifiableCollection = Collections.unmodifiableList(collection);
if(comparingFunction!=null){
comparator = new FunctionComparator();
Comparator.comparing(comparingFunction);
}
if (reversed) {
comparator = comparator.reversed();
reversed = false;
}
}
@Override
public WrappedList reverse() {
reversed = !reversed;
return this;
}
public void sort() {
if (comparator != null) {
log.debug("sorting");
this.collection.sort(comparator);
}else{
log.debug("no sorting - comparator is null");
}
}
@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));
}
@OnEvent
public boolean updated() {
if (!reset && wrappedSource != null) {
final T newItem = wrappedSource.event();
addItem(newItem);
}
reset = false;
return true;
}
@Override
public WrappedList sliding(int itemsPerBucket, int numberOfBuckets){
WrappedList sliding = WindowBuildOperations.service().sliding(self(), itemsPerBucket, numberOfBuckets);
return sliding;
}
@Override
public WrappedList sliding(Duration timePerBucket, int numberOfBuckets) {
WrappedList sliding = WindowBuildOperations.service().sliding(self(), timePerBucket, numberOfBuckets);
return sliding;
}
@Override
public WrappedList tumbling(Duration time) {
WrappedList sliding = WindowBuildOperations.service().tumbling(self(), time);
return sliding;
}
@Override
public WrappedList tumbling(int itemCount) {
WrappedList sliding = WindowBuildOperations.service().tumbling(self(), itemCount);
return sliding;
}
@Override
public void combine(WrappedList other) {
List otherCollection = other.collection();
if (otherCollection == null) {
return;
}
if (otherCollection instanceof List) {
List list = (List) otherCollection;
for (int i = 0; i < list.size(); i++) {
this.addItem(list.get(i));
}
} else {
otherCollection.forEach(this::addItem);
}
}
@Override
public void deduct(WrappedList other) {
List collection1 = other.collection();
for (int i = 0; i < collection1.size(); i++) {
T get = collection1.get(i);
this.collection.remove(get);
}
}
public ArrayListWrappedCollection addItem(final T newItem) {
if (comparator != null) {
int index = Collections.binarySearch(collection, newItem, comparator);
if (index < 0) {
index = ~index;
}
collection.add(index, newItem);
} else {
collection.add(newItem);
}
return this;
}
public ArrayListWrappedCollection removeItem(final T newItem) {
this.collection.remove(newItem);
return this;
}
@Override
public WrappedList comparator(Comparator comparator) {
setComparator(comparator);
return this;
}
public void setComparator(Comparator comparator) {
this.comparator = comparator;
}
public Comparator getComparator() {
return comparator;
}
public boolean isReversed() {
return reversed;
}
public void setReversed(boolean reversed) {
this.reversed = reversed;
}
@Override
public WrappedList comparing(SerializableFunction comparingFunction) {
setComparator(new FunctionComparator(comparingFunction));
return this;
}
@Override
public List collection() {
return unmodifiableCollection;
}
@Override
public List subList(int fromIndex, int toIndex) {
return collection().subList(fromIndex, toIndex);
}
@Override
public String toString() {
return "ArrayListWrappedCollection{" + "collection=" + collection + '}';
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy