/*
* Copyright 2014-2015 GameUp
*
* 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 io.gameup.android.entity;
import com.google.gson.JsonElement;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import java.util.Map;
import io.gameup.android.json.GsonFactory;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
/**
* A shared storage entity.
*/
@Data
@AllArgsConstructor(suppressConstructorProperties = true)
public class SharedStorage {
/** The public portion of this shared storage key, if any. */
@SerializedName("public")
private final JsonElement publicEntity;
/** The protected portion of this shared storage key, if any. */
@SerializedName("protected")
private final JsonElement protectedEntity;
/**
* Get the public portion as a raw map.
*
* @return A Map instance containing the public portion.
*/
public Map<String, Object> getPublicEntity() {
return GsonFactory.get().fromJson(publicEntity,
new TypeToken<Map<String, Object>>(){}.getType());
}
/**
* Get the protected portion as a raw map.
*
* @return A Map instance containing the protected portion.
*/
public Map<String, Object> getProtectedEntity() {
return GsonFactory.get().fromJson(protectedEntity,
new TypeToken<Map<String, Object>>(){}.getType());
}
/**
* Get the public portion as a specific class type.
*
* @param type The class type to deserialize to.
* @return An instance of the requested type containing the deserialized
* public data.
*/
public <T> T getPublicEntityAs(final @NonNull Class<T> type) {
return GsonFactory.get().fromJson(publicEntity, type);
}
/**
* Get the protected portion as a specific class type.
*
* @param type The class type to deserialize to.
* @return An instance of the requested type containing the deserialized
* protected data.
*/
public <T> T getProtectedEntityAs(final @NonNull Class<T> type) {
return GsonFactory.get().fromJson(protectedEntity, type);
}
}