de.kaiserpfalzEdv.commons.exceptions.BaseBusinessException.java Source code

Java tutorial

Introduction

Here is the source code for de.kaiserpfalzEdv.commons.exceptions.BaseBusinessException.java

Source

/*
 * Copyright 2015 Kaiserpfalz EDV-Service Roland Lichti
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.kaiserpfalzEdv.commons.exceptions;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.UUID;

/**
 * The default exception class for the KP Commons library.
 *
 * @author klenkes
 * @version 1.0.0
 * @since 11.10.2008 16:26:58
 */
public class BaseBusinessException extends RuntimeException implements BaseException {
    /**
     * Serial UID of this exception class.
     */
    private static final long serialVersionUID = -478653650420499466L;

    /**
     * The unique ID of this exception. It is read-only.
     */
    private final UUID id = UUID.randomUUID();

    /**
     * @param message The failure message.
     */
    public BaseBusinessException(final String message) {
        super(message);
    }

    /**
     * @param cause The failure cause of this exception.
     */
    public BaseBusinessException(final BaseBusinessException cause) {
        super(cause.getPureMessage(), cause);
    }

    /**
     * @param cause The failure cause of this exception.
     */
    public BaseBusinessException(final BaseSystemException cause) {
        super(cause.getPureMessage(), cause);
    }

    /**
     * @param cause The failure cause of this exception.
     */
    public BaseBusinessException(final Throwable cause) {
        super(cause instanceof BaseException ? ((BaseException) cause).getPureMessage() : cause.getMessage(),
                cause);
    }

    /**
     * @param message The failure message.
     * @param cause   The failure cause of this exception.
     */
    public BaseBusinessException(final String message, final Throwable cause) {
        super(message, cause);
    }

    @Override
    public UUID getId() {
        return id;
    }

    @Override
    public String getPureMessage() {
        return super.getMessage();
    }

    @Override
    public String getMessage() {
        StringBuilder result = new StringBuilder();

        result.append("{\"message\":\"").append(super.getMessage()).append("\"; \"id\":\"")
                .append(getId().toString()).append("\";}");

        return result.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        BaseBusinessException rhs = (BaseBusinessException) obj;
        return new EqualsBuilder().appendSuper(super.equals(obj)).append(this.id, rhs.id).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().appendSuper(super.hashCode()).append(id).toHashCode();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).appendSuper(super.toString()).append("id", id)
                .append("message", getPureMessage()).toString();
    }
}