All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.getstream.core.models.Data Maven / Gradle / Ivy

There is a newer version: 3.15.0
Show newest version
package io.getstream.core.models;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.getstream.core.utils.Serialization.convert;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Maps;
import io.getstream.core.models.serialization.DataDeserializer;
import java.util.Map;
import java.util.Objects;

@JsonDeserialize(using = DataDeserializer.class)
public final class Data {
  private final String id;
  private final Map data = Maps.newHashMap();

  public Data(String id) {
    this.id = checkNotNull(id, "ID required");
  }

  public Data() {
    this("");
  }

  public static  Data buildFrom(T data) {
    return convert(data, Data.class);
  }

  public String getID() {
    return id;
  }

  @JsonAnyGetter
  public Map getData() {
    return data;
  }

  public  Data set(String key, T value) {
    checkNotNull(key, "Key can't be null");

    data.put(key, value);
    return this;
  }

  public  Data from(T data) {
    return from(convert(data, new TypeReference>() {}));
  }

  public Data from(Map map) {
    checkNotNull(data, "Can't extract data from null");
    if (map == null || map.isEmpty()) {
      return this;
    }

    for (Map.Entry entry : map.entrySet()) {
      set(entry.getKey(), entry.getValue());
    }
    return this;
  }

  public  T get(String key) {
    return (T) data.get(checkNotNull(key, "Key can't be null"));
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Data data = (Data) o;
    return Objects.equals(id, data.id) && Objects.equals(data, data.data);
  }

  @Override
  public int hashCode() {
    return Objects.hash(id, data);
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this).add("id", this.id).add("data", this.data).toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy