/* * 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.reflect.TypeToken; import java.util.List; import java.util.Map; import io.gameup.android.json.GsonFactory; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; /** * Represents a message in a gamer's mailbox. */ @Data @AllArgsConstructor(suppressConstructorProperties = true) public class Message { /** Unique message identifier. */ private final String messageId; /** A list of tags for this message. */ private final List<String> tags; /** Message subject line. */ private final String subject; /** UTC timestamp when the message was created. */ private final long createdAt; /** UTC timestamp when the message expires. */ private final long expiresAt; /** UTC timestamp when the message was read, if applicable. */ private final Long readAt; /** Message contents, if applicable. */ private final JsonElement body; /** * Get the message contents as a raw map. * * @return A Map containing the message body data. */ public Map<String, Object> getBody() { return GsonFactory.get().fromJson(body, new TypeToken<Map<String, Object>>(){}.getType()); } /** * Get the message contents as a specified type. * * @param type The class type to deserialize to. * @return An instance of the requested type containing the deserialized * message body data. */ public <T> T getBodyAs(final @NonNull Class<T> type) { return GsonFactory.get().fromJson(body, type); } }