001 /**
002 * $Id: AbstractProfileMonitor.java,v 1.8 2014/04/20 19:58:49 oboehm Exp $
003 *
004 * Copyright (c) 2008 by Oliver Boehm
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 * (c)reated 27.12.2008 by oliver (ob@oasd.de)
019 */
020 package patterntesting.runtime.monitor;
021
022 import java.util.Locale;
023
024 import patterntesting.runtime.util.Converter;
025
026 /**
027 * @author <a href="boehm@javatux.de">oliver</a>
028 * @since 27.12.2008
029 * @version $Revision: 1.8 $
030 */
031 public abstract class AbstractProfileMonitor implements ProfileMonitor {
032
033 /**
034 * The ProfileMonitor with the higher number of totals is considered as
035 * "greater".
036 *
037 * @param other the other ProfileMonitor
038 * @return 0 if both ProfileMonitors has the same results
039 */
040 public final int compareTo(final ProfileMonitor other) {
041 if (this.getTotal() > other.getTotal()) {
042 return -1;
043 } else if (this.getTotal() == other.getTotal()) {
044 return other.getHits() - this.getHits();
045 } else {
046 return 1;
047 }
048 }
049
050 /**
051 * Gets the last value as time string. It returns the same result as
052 * {@link #getLastValue()} but in a human readable format. The english
053 * locale is used for formatting because this method is normally used for
054 * logging (which should be normally done in English).
055 *
056 * @return the last time (e.g. "42 seconds")
057 * @since 1.4.2
058 */
059 public final String getLastTime() {
060 double value = this.getLastValue();
061 return Converter.getTimeAsString(value, Locale.ENGLISH);
062 }
063
064 /**
065 * If we impmlement the {@link Comparable#compareTo(Object)} method we
066 * should also implement/overwrite the {@link Object#equals(Object)}
067 * method.
068 *
069 * @param other the other
070 * @return true, if successful
071 * @see java.lang.Object#equals(java.lang.Object)
072 */
073 @Override
074 public boolean equals(final Object other) {
075 if (other == null) {
076 return false;
077 }
078 try {
079 return equals((AbstractProfileMonitor) other);
080 } catch(ClassCastException mayhappen) {
081 return false;
082 }
083 }
084
085 /**
086 * If the {@link Comparable#compareTo(Object)} returns 0 the two objects
087 * are equals.
088 *
089 * @param other the other
090 * @return true, if successful
091 */
092 public boolean equals(final AbstractProfileMonitor other) {
093 return this.compareTo(other) == 0;
094 }
095
096 /**
097 * Hash code.
098 *
099 * @return the int
100 * @see java.lang.Object#hashCode()
101 */
102 @Override
103 public int hashCode() {
104 double total = this.getTotal();
105 return Double.valueOf(total).hashCode();
106 }
107
108 }