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

patterntesting.runtime.monitor.AbstractProfileMonitor Maven / Gradle / Ivy

/**
 * $Id: AbstractProfileMonitor.java,v 1.8 2014/04/20 19:58:49 oboehm Exp $
 *
 * Copyright (c) 2008 by Oliver Boehm
 *
 * 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 orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 27.12.2008 by oliver ([email protected])
 */
package patterntesting.runtime.monitor;

import java.util.Locale;

import patterntesting.runtime.util.Converter;

/**
 * @author oliver
 * @since 27.12.2008
 * @version $Revision: 1.8 $
 */
public abstract class AbstractProfileMonitor implements ProfileMonitor {

    /**
     * The ProfileMonitor with the higher number of totals is considered as
     * "greater".
     *
     * @param other the other ProfileMonitor
     * @return 0 if both ProfileMonitors has the same results
     */
    public final int compareTo(final ProfileMonitor other) {
        if (this.getTotal() > other.getTotal()) {
            return -1;
        } else if (this.getTotal() == other.getTotal()) {
            return other.getHits() - this.getHits();
        } else {
            return 1;
        }
    }

    /**
     * Gets the last value as time string. It returns the same result as
     * {@link #getLastValue()} but in a human readable format. The english
     * locale is used for formatting because this method is normally used for
     * logging (which should be normally done in English).
     *
     * @return the last time (e.g. "42 seconds")
     * @since 1.4.2
     */
    public final String getLastTime() {
        double value = this.getLastValue();
        return Converter.getTimeAsString(value, Locale.ENGLISH);
    }

    /**
     * If we impmlement the {@link Comparable#compareTo(Object)} method we
     * should also implement/overwrite the {@link Object#equals(Object)}
     * method.
     *
     * @param other the other
     * @return true, if successful
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object other) {
        if (other == null) {
            return false;
        }
        try {
            return equals((AbstractProfileMonitor) other);
        } catch(ClassCastException mayhappen) {
            return false;
        }
    }

    /**
     * If the {@link Comparable#compareTo(Object)} returns 0 the two objects
     * are equals.
     *
     * @param other the other
     * @return true, if successful
     */
    public boolean equals(final AbstractProfileMonitor other) {
        return this.compareTo(other) == 0;
    }

    /**
     * Hash code.
     *
     * @return the int
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        double total = this.getTotal();
        return Double.valueOf(total).hashCode();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy