mk.gdx.firebase.deserialization.DataCallbackMitmConverter Maven / Gradle / Ivy
Show all versions of gdx-fireapp-core Show documentation
/*
* 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.Collections;
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.
*
* DataCallbackMitmConverter means: Data callback man-in-the-middle converter.
* We call {@code DataCallback} because of nested generic type. For ex. it can be List and we can't do such conversion directly to into the database.
* {@code coveredCallback} data will be cast to {@code } after conversion.
* TODO - get mapConverter from GdxFIRDatabase instance? After converter change it may occur invalid result.
*
* @param Result type of callback
*/
public class DataCallbackMitmConverter extends MapMitmConverter {
private DataCallback coveredCallback;
private Class dataType;
public DataCallbackMitmConverter(Class dataType, DataCallback coveredCallback, FirebaseMapConverter mapConverter) {
super(mapConverter);
this.coveredCallback = coveredCallback;
this.dataType = dataType;
}
/**
* Detect if client want POJO class or not and return appropriate callback.
*
* 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