View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.impl;
18  
19  import java.io.Serializable;
20  
21  import org.apache.logging.log4j.core.pattern.PlainTextRenderer;
22  import org.apache.logging.log4j.core.pattern.TextRenderer;
23  
24  /**
25   * Class and package data used with a {@link StackTraceElement} in a {@link ExtendedStackTraceElement}.
26   */
27  public final class ExtendedClassInfo implements Serializable {
28  
29      private static final long serialVersionUID = 1L;
30  
31      private final boolean exact;
32  
33      private final String location;
34  
35      private final String version;
36  
37      /**
38       * Constructs a new instance.
39       *
40       * @param exact
41       * @param location
42       * @param version
43       */
44      public ExtendedClassInfo(final boolean exact, final String location, final String version) {
45          super();
46          this.exact = exact;
47          this.location = location;
48          this.version = version;
49      }
50  
51      @Override
52      public boolean equals(final Object obj) {
53          if (this == obj) {
54              return true;
55          }
56          if (obj == null) {
57              return false;
58          }
59          if (!(obj instanceof ExtendedClassInfo)) {
60              return false;
61          }
62          final ExtendedClassInfo other = (ExtendedClassInfo) obj;
63          if (this.exact != other.exact) {
64              return false;
65          }
66          if (this.location == null) {
67              if (other.location != null) {
68                  return false;
69              }
70          } else if (!this.location.equals(other.location)) {
71              return false;
72          }
73          if (this.version == null) {
74              if (other.version != null) {
75                  return false;
76              }
77          } else if (!this.version.equals(other.version)) {
78              return false;
79          }
80          return true;
81      }
82  
83      public boolean getExact() {
84          return this.exact;
85      }
86  
87      public String getLocation() {
88          return this.location;
89      }
90  
91      public String getVersion() {
92          return this.version;
93      }
94  
95      @Override
96      public int hashCode() {
97          final int prime = 31;
98          int result = 1;
99          result = prime * result + (this.exact ? 1231 : 1237);
100         result = prime * result + ((this.location == null) ? 0 : this.location.hashCode());
101         result = prime * result + ((this.version == null) ? 0 : this.version.hashCode());
102         return result;
103     }
104 
105     public void renderOn(final StringBuilder output, final TextRenderer textRenderer) {
106         if (!this.exact) {
107             textRenderer.render("~", output, "ExtraClassInfo.Inexact");
108         }
109         textRenderer.render("[", output, "ExtraClassInfo.Container");
110         textRenderer.render(this.location, output, "ExtraClassInfo.Location");
111         textRenderer.render(":", output, "ExtraClassInfo.ContainerSeparator");
112         textRenderer.render(this.version, output, "ExtraClassInfo.Version");
113         textRenderer.render("]", output, "ExtraClassInfo.Container");
114     }
115 
116     @Override
117     public String toString() {
118         final StringBuilder sb = new StringBuilder();
119         renderOn(sb, PlainTextRenderer.getInstance());
120         return sb.toString();
121     }
122 
123 }