Plex-FAWE/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Registry.java

79 lines
2.4 KiB
Java

/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.registry;
import com.google.common.collect.Maps;
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
public class Registry<V extends Keyed> implements IRegistry<V> {
private final Map<String, V> map;
private final String name;
public Registry(String name, Map<String, V> map) {
this.name = name;
this.map = map;
}
public Registry(final String name) {
this(name, Maps.newHashMap());
}
public String getName() {
return name;
}
public Map<String, V> getMap() {
return map;
}
@Nullable
public V get(final String key) {
checkState(key.equals(key.toLowerCase(Locale.ROOT)), "key must be lowercase");
return this.map.get(key);
}
public V register(final String key, final V value) {
requireNonNull(key, "key");
requireNonNull(value, "value");
checkState(key.equals(key.toLowerCase(Locale.ROOT)), "key must be lowercase");
checkState(!this.map.containsKey(key), "key '%s' already has an associated %s", key, this.name);
this.map.put(key, value);
return value;
}
public Set<String> keySet() {
return Collections.unmodifiableSet(this.map.keySet());
}
public Collection<V> values() {
return Collections.unmodifiableCollection(this.map.values());
}
}