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

com.github.juliarn.npclib.api.protocol.meta.DefaultEntityMetadataFactoryBuilder Maven / Gradle / Ivy

The newest version!
/*
 * This file is part of npc-lib, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2022-2023 Julian M., Pasqual K. and contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.github.juliarn.npclib.api.protocol.meta;

import com.github.juliarn.npclib.api.PlatformVersionAccessor;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.function.Function;
import org.jetbrains.annotations.NotNull;

final class DefaultEntityMetadataFactoryBuilder implements EntityMetadataFactory.Builder {

  private int baseIndex = 0;
  private int[] indexShitVersions = new int[0];

  private Type type;
  private Function inputConverter;

  private Collection> relatedMetadata;
  private Function availabilityChecker;

  @Override
  public @NotNull EntityMetadataFactory.Builder baseIndex(int index) {
    this.baseIndex = index;
    return this;
  }

  @Override
  public @NotNull EntityMetadataFactory.Builder indexShiftVersions(int... versions) {
    this.indexShitVersions = versions;
    return this;
  }

  @Override
  public @NotNull EntityMetadataFactory.Builder type(@NotNull Type type) {
    this.type = Objects.requireNonNull(type, "type");
    return this;
  }

  @Override
  public @NotNull EntityMetadataFactory.Builder inputConverter(@NotNull Function mapper) {
    this.inputConverter = Objects.requireNonNull(mapper, "mapper");
    return this;
  }

  @Override
  public @NotNull EntityMetadataFactory.Builder addRelatedMetadata(
    @NotNull EntityMetadataFactory relatedMetadata
  ) {
    if (this.relatedMetadata == null) {
      this.relatedMetadata = new HashSet<>();
    }

    this.relatedMetadata.add(relatedMetadata);
    return this;
  }

  @Override
  public @NotNull EntityMetadataFactory.Builder availabilityChecker(
    @NotNull Function checker
  ) {
    this.availabilityChecker = Objects.requireNonNull(checker, "checker");
    return this;
  }

  @Override
  public @NotNull EntityMetadataFactory build() {
    // fill in default empty values
    if (this.relatedMetadata == null) {
      this.relatedMetadata = Collections.emptySet();
    }

    if (this.availabilityChecker == null) {
      this.availabilityChecker = accessor -> true;
    }

    return new DefaultEntityMetadataFactory<>(
      this.baseIndex,
      this.indexShitVersions,
      Objects.requireNonNull(this.type, "type"),
      Objects.requireNonNull(this.inputConverter, "inputConverter"),
      this.relatedMetadata,
      this.availabilityChecker
    );
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy