All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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.partition.LambdaReflection.SerializableBiFunction;
import com.fluxtion.api.partition.LambdaReflection.SerializableFunction;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @author Greg Higgins [email protected]
 * @param 
 */
public class ArrayListWrappedCollection implements WrappedList {


    private List unmodifiableCollection;
    private final Wrapper wrappedSource;
    @NoEventReference
    private Comparator comparator;
    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(reversed){
            comparator = comparator.reversed();
            reversed = false;
        }
    }

    @Override
    public WrappedList reverse() {
        reversed = !reversed;
        return this;
    }

    @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 void combine(Stateful other) {
        final WrappedList otherList = (WrappedList)other;
        List collection1 = otherList.collection();
        for (int i = 0; i < collection1.size(); i++) {
            T get = collection1.get(i);
            this.addItem(get);
        }
    }

    @Override
    public void deduct(Stateful other) {
        final WrappedList otherList = (WrappedList)other;
        List collection1 = otherList.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;
    }

    @Override
    public WrappedList comparator(Comparator comparator) {
        setComparator(SepContext.service().addOrReuse(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 < I extends Integer> void comparing(SerializableBiFunction func) {
        System.out.println("SETTING COMPARATOR STATIC FUNCTION " + func.method().getParameters()[0].getType());
    }

    @Override
    public  void comparing(SerializableFunction in) {
        System.out.println("SETTING COMPARATOR USING PROPERTY");
    }

    @Override
    public List collection() {
        return unmodifiableCollection;
    }

    public List subList(int fromIndex, int toIndex) {
        return collection().subList(fromIndex, toIndex);
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy