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

com.fluxtion.ext.streaming.api.numeric.DefaultNumberWrapper Maven / Gradle / Ivy

There is a newer version: 2.10.50
Show newest version
/*
 * Copyright (C) 2020 V12 Technology Ltd.
 *
 * 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 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.numeric;

import com.fluxtion.api.annotations.AfterEvent;
import com.fluxtion.api.annotations.OnEvent;
import com.fluxtion.ext.streaming.api.FilterWrapper;
import com.fluxtion.ext.streaming.api.Wrapper;
import com.fluxtion.ext.streaming.api.stream.AbstractFilterWrapper;

/**
 *
 * @author V12 Technology Ltd.
 */
public abstract class DefaultNumberWrapper extends AbstractFilterWrapper {

    private final Wrapper parent;
    protected transient final MutableNumber result = new MutableNumber();

    public DefaultNumberWrapper(Wrapper parent) {
        this.parent = parent;
    }

    public static class DefaultIntWrapper extends DefaultNumberWrapper {

        private final int defaultVal;

        public DefaultIntWrapper(Wrapper parent, int defaultVal) {
            super(parent);
            this.defaultVal = defaultVal;
        }

        @Override
        public void reset() {
            recalculate = true;
            result.set(defaultVal);
        }

    }

    public static class DefaultDoubleWrapper extends DefaultNumberWrapper {

        private final double defaultVal;

        public DefaultDoubleWrapper(Wrapper parent, double defaultVal) {
            super(parent);
            this.defaultVal = defaultVal;
        }

        @Override
        public void reset() {
            recalculate = true;
            result.set(defaultVal);
        }

    }

    public static class DefaultLongWrapper extends DefaultNumberWrapper {

        private final long defaultVal;

        public DefaultLongWrapper(Wrapper parent, long defaultVal) {
            super(parent);
            this.defaultVal = defaultVal;
        }

        @Override
        public void reset() {
            recalculate = true;
            result.set(defaultVal);
        }

    }

    @OnEvent
    public boolean onEvent() {
        boolean updated = true;
        if (recalculate) {
            Number parentNumber = parent.event();
            updated = !parentNumber.equals(result);
            result.set(parentNumber);
        }
        recalculate = true;
        return updated;
    }
    
    @AfterEvent
    public void resetAfterEvent() {
      if (reset) {
        reset();
      }
      reset = false;
    } 

    @Override
    public Number event() {
        return result;
    }

    @Override
    public Class eventClass() {
        return Number.class;
    }

    @Override
    public boolean isValidOnStart() {
        return true;
    }

    @Override
    public FilterWrapper publishAndReset(Object notifier) {
        parent.publishAndReset(notifier);
        return super.publishAndReset(notifier);
    }

    @Override
    public FilterWrapper resetNoPublish(Object notifier) {
        parent.resetNoPublish(notifier);
        return super.resetNoPublish(notifier);
    }

    @Override
    public FilterWrapper resetAndPublish(Object notifier) {
        parent.resetAndPublish(notifier);
        return super.resetAndPublish(notifier);
    }

    @Override
    public FilterWrapper triggerOverride(Object triggerOverride) {
        parent.triggerOverride(triggerOverride);
        return super.triggerOverride(triggerOverride); 
    }   

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy