001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.mstat;
014
015import java.util.*;
016import java.util.concurrent.atomic.*;
017
018import org.apache.juneau.annotation.*;
019
020/**
021 * Represents an entry in {@link ExceptionStore}.
022 */
023@Bean(bpi="hash,count,exceptionClass,message,stackTrace,causedBy", fluentSetters=true)
024public class ExceptionStats extends ExceptionInfo implements Comparable<ExceptionStats> {
025   private final AtomicInteger count = new AtomicInteger(0);
026   private transient long timeout = -1;
027
028   public static ExceptionStats create() {
029      return new ExceptionStats();
030   }
031
032   /**
033    * Returns the number of times this stack trace was encountered.
034    *
035    * @return The number of times this stack trace was encountered.
036    */
037   public int getCount() {
038      return count.intValue();
039   }
040
041   /**
042    * TODO
043    *
044    * @param value TODO
045    * @return This object (for method chaining).
046    */
047   public ExceptionStats count(int value) {
048      count.set(value);
049      return this;
050   }
051
052   /**
053    * Increments the occurrence count of this exception.
054    *
055    * @return This object (for method chaining).
056    */
057   public ExceptionStats increment() {
058      count.incrementAndGet();
059      return this;
060   }
061
062   /**
063    * TODO
064    *
065    * @param value TODO
066    * @return This object (for method chaining).
067    */
068   public ExceptionStats timeout(long value) {
069      this.timeout = value;
070      return this;
071   }
072
073   /**
074    * TODO
075    *
076    * @return TODO
077    */
078   public boolean isExpired() {
079      return timeout >= 0 && System.currentTimeMillis() > timeout;
080   }
081
082
083   @Override
084   public ExceptionStats hash(String value) {
085      super.hash(value);
086      return this;
087   }
088
089   @Override
090   public ExceptionStats exceptionClass(String value) {
091      super.exceptionClass(value);
092      return this;
093   }
094
095   @Override
096   public ExceptionStats message(String value) {
097      super.message(value);
098      return this;
099   }
100
101   @Override
102   public ExceptionStats stackTrace(List<StackTraceElement> value) {
103      super.stackTrace(value);
104      return this;
105   }
106
107   @Override
108   public ExceptionStats causedBy(ExceptionInfo value) {
109      super.causedBy(value);
110      return this;
111   }
112
113
114
115   @Override /* Comparable */
116   public int compareTo(ExceptionStats o) {
117      return Integer.compare(o.getCount(), getCount());
118   }
119
120   @Override /* Object */
121   public ExceptionStats clone() {
122      try {
123         return (ExceptionStats) super.clone();
124      } catch (CloneNotSupportedException e) {
125         throw new RuntimeException(e);
126      }
127   }
128}