<commit_before>/*
 * Copyright 2017 mk
 *
 * 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 mk.gdx.firebase.deserialization;

import java.util.Map;

import mk.gdx.firebase.annotations.MapConversion;
import mk.gdx.firebase.annotations.NestedGenericType;
import mk.gdx.firebase.callbacks.DataCallback;
import mk.gdx.firebase.reflection.AnnotationFinder;

/**
 * Wraps data callback results and do conversion from map to objects, if necessary.
 * <p>
 * DataCallbackMitmConverter means: Data callback man-in-the-middle converter.
 * We call {@code DataCallback<T>} because of nested generic type. For ex. it can be List<User> and we can't do such conversion directly to into the database.
 * {@code coveredCallback} data will be cast to {@code <E>} after conversion.
 * TODO - get mapConverter from GdxFIRDatabase instance? When someone change convert in-fly it can make wrong results.
 *
 * @param <T> Result type of callback
 */
public class DataCallbackMitmConverter<T, E extends T> extends MapMitmConverter
{

    private DataCallback<E> coveredCallback;
    private Class<T> dataType;

    public DataCallbackMitmConverter(Class<T> dataType, DataCallback<E> coveredCallback, FirebaseMapConverter mapConverter)
    {
        super(mapConverter);
        this.coveredCallback = coveredCallback;
    }

    /**
     * Detect if client want POJO class or not and return appropriate callback.
     * <p>
     * If wanted type is POJO we need to retrieve Map and then convert it.
     * Otherwise we need to retrieve type given by user (as T generic type).
     *
     * @return DataCallback which retrieves data from database, not null.
     */
    public DataCallback<?> get()
    {
        return isPojo(dataType) ? getPojoDataCallback() : getGenericDataCallback();
    }

    /**
     * Get callback appropriate for pojo retrieving.
     *
     * @return New Pojo data callback instance, not null.
     */
    public DataCallback<Map> getPojoDataCallback()
    {
        return new PojoDataCallback();
    }

    /**
     * Get callback appropriate for pojo retrieving.
     *
     * @return New Pojo data callback instance, not null.
     */
    public DataCallback<T> getGenericDataCallback()
    {
        return new GenericDataCallback();
    }

    /**
     * Do MITM Map conversion and pass through to {@code coveredCallback}.
     *
     * @param data Received data from Firebase - data was cast specified generic type {@code <T>}, may be null.
     * @see #doMitmConversion(Class, Object)
     */
    @SuppressWarnings("unchecked")
    public void onData(Object data)
    {

        MapConversion mapConversionAnnotation = AnnotationFinder.getMethodAnnotation(MapConversion.class, coveredCallback);
        // If MapConversions was not indicated - do nothing.
        if (mapConversionAnnotation != null) {
            data = doMitmConversion(mapConversionAnnotation.value(), data);
        } else {
            // Depracated - will be remove in next releases.
            NestedGenericType nestedGenericTypeAnnotation = AnnotationFinder.getMethodAnnotation(NestedGenericType.class, coveredCallback);
            if (nestedGenericTypeAnnotation != null) {
                data = doMitmConversion(nestedGenericTypeAnnotation.value(), data);
            }
        }
        coveredCallback.onData((E) data);
    }

    /**
     * Pass through to {@code coveredCallback}.
     *
     * @param e Exception with description what was wrong.
     */
    public void onError(Exception e)
    {
        coveredCallback.onError(e);
    }

    /**
     * Retrieve map from database and pass to {@link #onData(Object)}<commit_msg>Remove depracated @NestedGenericType from DataCallbackMitmConverter<commit_after>/*
 * Copyright 2017 mk
 *
 * 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 mk.gdx.firebase.deserialization;

import com.badlogic.gdx.utils.reflect.ClassReflection;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import mk.gdx.firebase.annotations.MapConversion;
import mk.gdx.firebase.callbacks.DataCallback;
import mk.gdx.firebase.reflection.AnnotationFinder;

/**
 * Wraps data callback results and do conversion from map to objects, if necessary.
 * <p>
 * DataCallbackMitmConverter means: Data callback man-in-the-middle converter.
 * We call {@code DataCallback<T>} because of nested generic type. For ex. it can be List<User> and we can't do such conversion directly to into the database.
 * {@code coveredCallback} data will be cast to {@code <E>} after conversion.
 * TODO - get mapConverter from GdxFIRDatabase instance? When someone change convert in-fly it can make wrong results.
 *
 * @param <T> Result type of callback
 */
public class DataCallbackMitmConverter<T, E extends T> extends MapMitmConverter
{

    private DataCallback<E> coveredCallback;
    private Class<T> dataType;

    public DataCallbackMitmConverter(Class<T> dataType, DataCallback<E> coveredCallback, FirebaseMapConverter mapConverter)
    {
        super(mapConverter);
        this.coveredCallback = coveredCallback;
    }

    /**
     * Detect if client want POJO class or not and return appropriate callback.
     * <p>
     * If wanted type is POJO we need to retrieve Map and then convert it.
     * Otherwise we need to retrieve type given by user (as T generic type).
     *
     * @return DataCallback which retrieves data from database, not null.
     */
    public DataCallback<?> get()
    {
        return isPojo(dataType) ? getPojoDataCallback() : getGenericDataCallback();
    }

    /**
     * Get callback appropriate for pojo retrieving.
     *
     * @return New Pojo data callback instance, not null.
     */
    public DataCallback<Map> getPojoDataCallback()
    {
        return new PojoDataCallback();
    }

    /**
     * Get callback appropriate for pojo retrieving.
     *
     * @return New Pojo data callback instance, not null.
     */
    public DataCallback<T> getGenericDataCallback()
    {
        return new GenericDataCallback();
    }

    /**
     * Do MITM Map conversion and pass through to {@code coveredCallback}.
     *
     * @param data Received data from Firebase - data was cast specified generic type {@code <T>}, may be null.
     * @see #doMitmConversion(Class, Object)
     */
    @SuppressWarnings("unchecked")
    public void onData(Object data)
    {

        MapConversion mapConversionAnnotation = AnnotationFinder.getMethodAnnotation(MapConversion.class, coveredCallback);
        // If MapConversions was not indicated - do nothing.
        if (mapConversionAnnotation != null) {
            data = doMitmConversion(mapConversionAnnotation.value(), data);
            // TODO - wrap one-value result in List if user want a List?
        }
        coveredCallback.onData((E) data);
    }

    /**
     * Pass through to {@code coveredCallback}.
     *
     * @param e Exception with description what was wrong.
     */
    public void onError(Exception e)
    {
        coveredCallback.onError(e);
    }

    /**
     * Retrieve map from database and pass to {@link #onData(Object)}
{'fraction_non_alphanumeric': 0.08500123548307388, 'fraction_numerical': 0.0019767729182110206, 'mean_word_length': 3.1133130081300813, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6276011', 'n_tokens_mistral': 2183, 'n_tokens_neox': 2142, 'n_words': 784}