All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dampcake.gson.immutable.DelegateAdapter Maven / Gradle / Ivy

/*
 * Copyright 2014 Adam Peck.
 *
 * 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 com.dampcake.gson.immutable;

import com.google.common.base.Preconditions;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

/**
 * A {@link TypeAdapter} that transforms the object returned by a delegate {@link TypeAdapter}.
 *
 * @author Adam Peck
 */
public abstract class DelegateAdapter extends TypeAdapter {

    private final TypeAdapter delegate;

    /**
     * Create and set the delegate {@link TypeAdapter} to be used.
     *
     * @param delegate the delegate {@link TypeAdapter}
     * @throws NullPointerException if the delegate {@link TypeAdapter} is null
     */
    public DelegateAdapter(TypeAdapter delegate) {
        Preconditions.checkNotNull(delegate, "delegate cannot be null");

        this.delegate = delegate;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void write(JsonWriter writer, T t) throws IOException {
        delegate.write(writer, t);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public T read(JsonReader reader) throws IOException {
        T t = delegate.read(reader);

        if (t == null)
            return null;

        return transform(t);
    }

    /**
     * Override to transform default Gson de-serialized object.
     *
     * @param t the converted Java object.
     * @return the transformed Java object.
     */
    protected abstract T transform(T t);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy