001/* 002 * Copyright 2021 Siroshun09 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.github.siroshun09.mccommand.common; 018 019import org.jetbrains.annotations.NotNull; 020import org.jetbrains.annotations.Nullable; 021import org.jetbrains.annotations.Unmodifiable; 022 023import java.util.Collections; 024import java.util.Objects; 025import java.util.Set; 026 027/** 028 * The abstract class of {@link Command}. 029 */ 030public abstract class AbstractCommand implements Command { 031 032 private final String name; 033 private final String permission; 034 private final Set<String> aliases; 035 036 /** 037 * Creates an {@link AbstractCommand}. 038 * 039 * @param name the command name 040 * @param permission the permission to execute this command 041 * @param aliases the aliases of this command 042 */ 043 public AbstractCommand(@NotNull String name, @Nullable String permission, @Nullable Set<String> aliases) { 044 Objects.requireNonNull(name); 045 046 if (name.isEmpty()) { 047 throw new IllegalArgumentException("'name' must not be empty."); 048 } 049 050 this.name = name; 051 this.permission = permission != null ? permission : ""; 052 this.aliases = aliases != null ? Set.copyOf(aliases) : Collections.emptySet(); 053 } 054 055 /** 056 * Creates an {@link AbstractCommand}. 057 * 058 * @param name the command name 059 * @param permission the permission to execute this command 060 */ 061 public AbstractCommand(@NotNull String name, @Nullable String permission) { 062 this(name, permission, null); 063 } 064 065 /** 066 * Creates an {@link AbstractCommand}. 067 * 068 * @param name the command name 069 * @param aliases the aliases of this command 070 */ 071 public AbstractCommand(@NotNull String name, @Nullable Set<String> aliases) { 072 this(name, null, aliases); 073 } 074 075 /** 076 * Creates an {@link AbstractCommand}. 077 * 078 * @param name the command name 079 */ 080 public AbstractCommand(@NotNull String name) { 081 this(name, null, null); 082 } 083 084 /** 085 * {@inheritDoc} 086 */ 087 @NotNull 088 @Override 089 public String getName() { 090 return name; 091 } 092 093 /** 094 * {@inheritDoc} 095 */ 096 @NotNull 097 @Override 098 public String getPermission() { 099 return permission; 100 } 101 102 /** 103 * {@inheritDoc} 104 */ 105 @NotNull 106 @Unmodifiable 107 @Override 108 public Set<String> getAliases() { 109 return aliases; 110 } 111 112 @Override 113 public boolean equals(Object o) { 114 if (this == o) { 115 return true; 116 } 117 118 if (!(o instanceof Command)) { 119 return false; 120 } 121 122 Command that = (Command) o; 123 124 return getName().equals(that.getName()) && 125 getPermission().equals(that.getPermission()) && 126 getAliases().equals(that.getAliases()); 127 } 128 129 @Override 130 public int hashCode() { 131 return Objects.hash(getName(), getPermission(), getAliases()); 132 } 133 134 @Override 135 public String toString() { 136 return "AbstractCommand{" + 137 "name='" + name + '\'' + 138 ", permission='" + permission + '\'' + 139 ", aliases=" + aliases + 140 '}'; 141 } 142}