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.utils;
014
015import java.util.concurrent.atomic.*;
016
017import org.apache.juneau.annotation.*;
018
019/**
020 * Represents an entry in {@link StackTraceDatabase}.
021 */
022@Bean(bpi="exception,hash,count")
023public class StackTraceInfo implements Comparable<StackTraceInfo> {
024   final AtomicInteger count;
025   final long timeout;
026   final String hash;
027   final String exception;
028
029   /**
030    * Constructor.
031    *
032    * @param exception The simple name of the exception class.  Can be <jk>null</jk>.
033    * @param timeout The time in UTC milliseconds at which point this stack trace info should be discarded from caches.
034    * @param hash The hash id of this stack trace.
035    */
036   public StackTraceInfo(String exception, long timeout, int hash) {
037      this.count = new AtomicInteger(0);
038      this.timeout = timeout;
039      this.hash = Integer.toHexString(hash);
040      this.exception = exception;
041   }
042
043   private StackTraceInfo(String exception, int count, long timeout, String hash) {
044      this.count = new AtomicInteger(count);
045      this.timeout = timeout;
046      this.hash = hash;
047      this.exception = exception;
048   }
049
050   /**
051    * Returns the number of times this stack trace was encountered.
052    *
053    * @return The number of times this stack trace was encountered.
054    */
055   public int getCount() {
056      return count.intValue();
057   }
058
059   /**
060    * Returns an 8-byte hash of the stack trace.
061    *
062    * @return An 8-byte hash of the stack trace.
063    */
064   public String getHash() {
065      return hash;
066   }
067
068   /**
069    * Returns the simple class name of the exception.
070    *
071    * @return The simple class name of the exception, or <jk>null</jk> if not specified.
072    */
073   public String getException() {
074      return exception;
075   }
076
077   /**
078    * Increments the occurrence count of this exception.
079    *
080    * @return This object (for method chaining).
081    */
082   public StackTraceInfo increment() {
083      count.incrementAndGet();
084      return this;
085   }
086
087   @Override /* Comparable */
088   public int compareTo(StackTraceInfo o) {
089      return Integer.compare(o.getCount(), getCount());
090   }
091
092   @Override /* Object */
093   public StackTraceInfo clone() {
094      return new StackTraceInfo(exception, count.intValue(), timeout, hash);
095   }
096}