001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.impl;
018
019import java.io.Serializable;
020
021import org.apache.logging.log4j.core.pattern.PlainTextRenderer;
022import org.apache.logging.log4j.core.pattern.TextRenderer;
023
024/**
025 * Class and package data used with a {@link StackTraceElement} in a {@link ExtendedStackTraceElement}.
026 */
027public final class ExtendedClassInfo implements Serializable {
028
029    private static final long serialVersionUID = 1L;
030
031    private final boolean exact;
032
033    private final String location;
034
035    private final String version;
036
037    /**
038     * Constructs a new instance.
039     *
040     * @param exact
041     * @param location
042     * @param version
043     */
044    public ExtendedClassInfo(final boolean exact, final String location, final String version) {
045        super();
046        this.exact = exact;
047        this.location = location;
048        this.version = version;
049    }
050
051    @Override
052    public boolean equals(final Object obj) {
053        if (this == obj) {
054            return true;
055        }
056        if (obj == null) {
057            return false;
058        }
059        if (!(obj instanceof ExtendedClassInfo)) {
060            return false;
061        }
062        final ExtendedClassInfo other = (ExtendedClassInfo) obj;
063        if (this.exact != other.exact) {
064            return false;
065        }
066        if (this.location == null) {
067            if (other.location != null) {
068                return false;
069            }
070        } else if (!this.location.equals(other.location)) {
071            return false;
072        }
073        if (this.version == null) {
074            if (other.version != null) {
075                return false;
076            }
077        } else if (!this.version.equals(other.version)) {
078            return false;
079        }
080        return true;
081    }
082
083    public boolean getExact() {
084        return this.exact;
085    }
086
087    public String getLocation() {
088        return this.location;
089    }
090
091    public String getVersion() {
092        return this.version;
093    }
094
095    @Override
096    public int hashCode() {
097        final int prime = 31;
098        int result = 1;
099        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}