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;
020import java.util.Objects;
021
022import org.apache.logging.log4j.core.pattern.PlainTextRenderer;
023import org.apache.logging.log4j.core.pattern.TextRenderer;
024
025/**
026 * Class and package data used with a {@link StackTraceElement} in a {@link ExtendedStackTraceElement}.
027 */
028public final class ExtendedClassInfo implements Serializable {
029
030    private static final long serialVersionUID = 1L;
031
032    private final boolean exact;
033
034    private final String location;
035
036    private final String version;
037
038    /**
039     * Constructs a new instance.
040     *
041     * @param exact
042     * @param location
043     * @param version
044     */
045    public ExtendedClassInfo(final boolean exact, final String location, final String version) {
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 (!Objects.equals(this.location, other.location)) {
067            return false;
068        }
069        if (!Objects.equals(this.version, other.version)) {
070            return false;
071        }
072        return true;
073    }
074
075    public boolean getExact() {
076        return this.exact;
077    }
078
079    public String getLocation() {
080        return this.location;
081    }
082
083    public String getVersion() {
084        return this.version;
085    }
086
087    @Override
088    public int hashCode() {
089        return Objects.hash(exact, location, version);
090    }
091
092    public void renderOn(final StringBuilder output, final TextRenderer textRenderer) {
093        if (!this.exact) {
094            textRenderer.render("~", output, "ExtraClassInfo.Inexact");
095        }
096        textRenderer.render("[", output, "ExtraClassInfo.Container");
097        textRenderer.render(this.location, output, "ExtraClassInfo.Location");
098        textRenderer.render(":", output, "ExtraClassInfo.ContainerSeparator");
099        textRenderer.render(this.version, output, "ExtraClassInfo.Version");
100        textRenderer.render("]", output, "ExtraClassInfo.Container");
101    }
102
103    @Override
104    public String toString() {
105        final StringBuilder sb = new StringBuilder();
106        renderOn(sb, PlainTextRenderer.getInstance());
107        return sb.toString();
108    }
109
110}