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

me.moros.bending.api.util.data.DataKey Maven / Gradle / Ivy

/*
 * Copyright 2020-2024 Moros
 *
 * This file is part of Bending.
 *
 * Bending is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bending is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bending. If not, see .
 */

package me.moros.bending.api.util.data;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.KeyPattern;

/**
 * Key implementation that also holds the type of data that can be associated with it.
 * @param  the type of data
 */
public final class DataKey implements Key {
  private final Key key;
  private final Class type;

  private DataKey(Key key, Class type) {
    this.key = key;
    this.type = type;
  }

  @Override
  public @KeyPattern.Namespace String namespace() {
    return key.namespace();
  }

  @Override
  public @KeyPattern.Value String value() {
    return key.value();
  }

  @Override
  public String asString() {
    return key.asString();
  }

  /**
   * Get the type of data this key can be associated with.
   * @return the type of data
   */
  public Class type() {
    return type;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj instanceof DataKey other) {
      return key.equals(other.key) && type == other.type;
    }
    return false;
  }

  private int hashcode;

  @Override
  public int hashCode() {
    if (hashcode == 0) {
      hashcode = (31 * key.hashCode()) + type.hashCode();
    }
    return hashcode;
  }

  /**
   * Create a new bending key by wrapping an existing key.
   * @param key the key to wrap
   * @param clazz the type
   * @param  the type of data
   * @return the constructed bending key
   */
  @SuppressWarnings("unchecked")
  public static  DataKey wrap(Key key, Class clazz) {
    if (key instanceof DataKey k && k.type() == clazz) {
      return (DataKey) k;
    }
    return new DataKey<>(key, clazz);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy