ar.com.zauber.garfio.modules.mantis.model.actions.NumberUpdateCustomFieldAction.java Source code

Java tutorial

Introduction

Here is the source code for ar.com.zauber.garfio.modules.mantis.model.actions.NumberUpdateCustomFieldAction.java

Source

/**
 * Copyright (c) 2007-2009 Zauber S.A. <http://www.zauber.com.ar/>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.zauber.garfio.modules.mantis.model.actions;

import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import ar.com.zauber.garfio.modules.mantis.model.MantisIssue;
import ar.com.zauber.garfio.modules.mantis.model.MantisTrackerSession;

/**
 * Change the value of a custom field that accepts a number.
 * 
 * The value can be absolute or a delta
 * @author Juan F. Codagnone
 * @since Mar 10, 2007
 */
public class NumberUpdateCustomFieldAction extends UpdateCustomFieldAction {
    private final Pattern reDelta = Pattern.compile("^(\\s)*[+-](\\d|[.,])+$");

    /**
     * Creates the NumberUpdateCustomFieldAction.
     *
     * @param issue
     * @param session
     * @param field
     * @param value
     */
    public NumberUpdateCustomFieldAction(final MantisIssue issue, final MantisTrackerSession session,
            final String field, final String value) {
        super(issue, session, field, StringUtils.trim(value));
    }

    // CHECKSTYLE:DESIGN:OFF
    /**  @see UpdateCustomFieldAction#getValue(String) */
    @Override
    public String getValue(String currentValue) {
        String ret;
        boolean isANumber = true;
        try {
            Double.parseDouble(currentValue);
        } catch (final Throwable e) {
            isANumber = false;
        }

        if (isANumber || StringUtils.isEmpty(currentValue)) {
            if (StringUtils.isEmpty(currentValue)) {
                currentValue = "0";
            }
            final String value = super.getValue(currentValue);
            isANumber = true;
            try {
                Double.parseDouble(value);
            } catch (final Throwable e) {
                isANumber = false;
            }

            if (isANumber) {
                if (reDelta.matcher(value).matches()) {
                    ret = Double.toString(Double.parseDouble(currentValue) + Double.parseDouble(value));
                } else {
                    ret = Double.toString(Double.parseDouble(value));
                }
            } else {
                throw new IllegalArgumentException("`" + value + "' " + "should be a number.");
            }
        } else {
            ret = currentValue;
        }

        return ret;
    }

    //  CHECKSTYLE:DESIGN:ON    
    /**  @see AbstractIssueAction#equals(java.lang.Object) */
    @Override
    public final boolean equals(final Object o) {
        boolean ret = false;

        if (o == this) {
            ret = true;
        } else if (o instanceof NumberUpdateCustomFieldAction) {
            final NumberUpdateCustomFieldAction action = (NumberUpdateCustomFieldAction) o;
            ret = super.equals(action) && getValue(super.getValue("")).equals(action.getValue(super.getValue("")));
        }
        return ret;
    }

    /**  @see AbstractIssueAction#hashCode() */
    @Override
    public final int hashCode() {
        return super.hashCode() + getValue("").hashCode();
    }
}