001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client;
017
018import com.fasterxml.jackson.databind.ObjectMapper;
019import org.apache.commons.lang3.builder.EqualsBuilder;
020import org.apache.commons.lang3.builder.HashCodeBuilder;
021
022/**
023 * Base class for requests and responses which are serialised and parsed to and from JSON.
024 * In addition to the {@link Jsonable} interface, this class provides reflective implementations
025 * of {@code equals}, {@code hashCode} and {@code toString} methods based on the class's fields.
026 *
027 * @since 8.2.0
028 */
029public abstract class JsonableBaseObject implements Jsonable {
030
031    /**
032     * Provides a mechanism for overriding the Jackson configuration options for this class.
033     *
034     * @return A new ObjectMapper with the desired serialisation options to use.
035     * @since 8.5.0
036     */
037    protected ObjectMapper createJsonObjectMapper() {
038        return Jsonable.createDefaultObjectMapper();
039    }
040
041    @Override
042    public int hashCode() {
043        return HashCodeBuilder.reflectionHashCode(this);
044    }
045
046    @Override
047    public boolean equals(Object obj) {
048        return EqualsBuilder.reflectionEquals(this, obj);
049    }
050
051    @Override
052    public String toString() {
053        return getClass().getSimpleName()+' '+toJson();
054    }
055}