content: /*
   * Copyright (C) 2020 - Amir Hossein Aghajari
   *
   * 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.aghajari.emojiview.sticker;
  
  import android.content.Context;
  import android.util.Base64;
  import android.util.Log;
  
  import androidx.annotation.NonNull;
  
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  
  public class Sticker<T> implements Serializable {
      private static final long serialVersionUID = 3L;
  
      T data;
  
      public Sticker(T data) {
          this.data = data;
      }
  
      public T getData() {
          return data;
      }
  
      public void setData(T data) {
          this.data = data;
      }
  
      @NonNull
      @Override
      public String toString() {
          return data.toString();
      }
  
      @Override
      public boolean equals(Object o) {
          if (!(o instanceof Sticker)) return false;
          return getData().equals(((Sticker<?>) o).getData());
      }
  
      @SuppressWarnings("unchecked")
      public static <T extends Serializable> T load(String string) {
          byte[] bytes = Base64.decode(string, 0);
          T object = null;
          try {
              ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
              object = (T) objectInputStream.readObject();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return object;
      }
  
      public static String toString(@SuppressWarnings("rawtypes") Sticker object) {
          Log.d("sticker", object.getClass().getSimpleName());
          String encoded = null;
          try {
              ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
              ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
              objectOutputStream.writeObject(object);
              objectOutputStream.close();
              encoded = Base64.encodeToString(byteArrayOutputStream.toByteArray(), 0);
          } catch (IOException e) {
              e.printStackTrace();
          }
          return encoded;
      }
  
      public RecentStickerManager getDefaultRecentManager(Context context, String type) {
          return new RecentStickerManager(context, type);
      }
  
      public boolean isInstance(Class<?> o) {
          return o.isInstance(getData());
      }
  
  }
  