CloudSlang/cs-actions

View on GitHub
cs-hashicorp-terraform/src/main/java/io/cloudslang/content/hashicorp/terraform/utils/CounterProcessor.java

Summary

Maintainability
C
1 day
Test Coverage



package io.cloudslang.content.hashicorp.terraform.utils;

import com.hp.oo.sdk.content.plugin.GlobalSessionObject;
import io.cloudslang.content.hashicorp.terraform.exceptions.CounterImplException;

import java.util.HashMap;
import java.util.Map;

public class CounterProcessor {

    private static final String END_INDEX="endIndex";
    private static final String INDEX="index";
    private static final Long endIndex = -1L;
    private long end;
    private long start;
    private int index;
    private int increment;

    public CounterProcessor() {
    }

    private boolean initialized(GlobalSessionObject<Map<String, Object>> session) {
        return (session.get().get(INDEX) != null);

    }

    public void init(String to, String from, String by, boolean reset, GlobalSessionObject<Map<String, Object>> session) throws CounterImplException {
        /*
         * If the session resource is not ini
         */
        if (session.get() == null) {
            session.setResource(new CounterSessionResource(new HashMap<String, Object>()));
        }
        Map<String, Object> sessionMap = session.get();
        try {
            start = Long.parseLong(from.trim());
            end = Long.parseLong(to.trim());
            if (by == null || by.length() == 0)
                increment = 1;
            else
                try {
                    increment = Integer.parseInt(by);
                } catch (Exception e) {
                    increment = 1;
                }
        } catch (Exception e) {
            throw new CounterImplException("Start or end is not a long integer, or by is not an integer.\nfrom: " + from + "\nto: " + to + "\nby:" + by);
        }
        try{
            index = (Integer) sessionMap.get(INDEX);
        } catch (Exception e) {
            index = 0;
        }

        if(index == 0 && initialized(session)) {
            sessionMap.put(INDEX, 0);

        }
        // ok, now push data into context
        if (!initialized(session)) {
            sessionMap.put(INDEX, 0);
        }

        // pull data from context
        this.index = (Integer) sessionMap.get(INDEX);
    }

    public String getNext(GlobalSessionObject<Map<String, Object>> session) {
        String ret = null;
        if (start < end) {
            if (start + index <= end) {
                ret = "" + (index + start);
                index += increment;
                session.get().put(INDEX, index);
            } else
                index += increment;
        } else if (start - index >= end) {
            ret = "" + (start - index);
            index += increment;
            session.get().put(INDEX, index);
        } else
            index += increment;
        return ret;
    }

    public boolean hasNext() {
        long diff = (start < end) ? end - start : start - end;
        if (index <= diff) {
            return true;
        } else {
            return false;
        }
    }

    public void setStepSessionEnd(GlobalSessionObject<Map<String, Object>> session) {
        session.get().put(END_INDEX,endIndex);
    }


    public long getEnd() {
        return end;
    }

    public void setEnd(long end) {
        this.end = end;
    }

    public long getStart() {
        return start;
    }

    public void setStart(long start) {
        this.start = start;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getIncrement() {
        return increment;
    }

    public void setIncrement(int increment) {
        this.increment = increment;
    }
}