Skip to content

Commit

Permalink
Continue move to "nullpo"
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Oct 23, 2021
1 parent 384fa8d commit be43ac3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 48 deletions.
31 changes: 15 additions & 16 deletions GalaxiAPI/global/src/net/ME1312/Galaxi/Library/Util.java
Expand Up @@ -14,21 +14,6 @@
public final class Util {
private Util(){}

/**
* Checks values to make sure they're not null
*
* @param values Values to check
* @return If any are null
*/
public static boolean isNull(Object... values) {
for (Object value : values) {
if (value == null) {
return true;
}
}
return false;
}


/**
* Checks values to make sure they're not null
Expand All @@ -41,7 +26,6 @@ public static <T> T nullpo(T value) {
return value;
}


/**
* Checks values to make sure they're not null
*
Expand All @@ -54,6 +38,21 @@ public static void nullpo(Object... values) {
}
}

/**
* Checks values to make sure they're not null
*
* @param values Values to check
* @return If any are null
*/
public static boolean isNull(Object... values) {
for (Object value : values) {
if (value == null) {
return true;
}
}
return false;
}

/**
* Get keys by value from map
*
Expand Down
6 changes: 3 additions & 3 deletions GalaxiAPI/src/net/ME1312/Galaxi/Log/Logger.java
Expand Up @@ -30,7 +30,7 @@ public final class Logger {
* @param prefix Log Prefix
*/
public Logger(String prefix) {
if (Util.isNull(prefix)) throw new NullPointerException();
Util.nullpo(prefix);
if (prefix.length() == 0) throw new StringIndexOutOfBoundsException("Cannot use an empty prefix");
debug = new LogStream(this, DEBUG);
message = new LogStream(this, MESSAGE);
Expand Down Expand Up @@ -106,7 +106,7 @@ public String getPrefix() {
* @param filter Log Filter
*/
public void addFilter(LogFilter filter) {
if (Util.isNull(filter)) throw new NullPointerException();
Util.nullpo(filter);
lFilters.add(filter);
}

Expand All @@ -126,7 +126,7 @@ public void removeFilter(LogFilter filter) {
* @param filter Static Log Filter
*/
public static void addStaticFilter(LogFilter filter) {
if (Util.isNull(filter)) throw new NullPointerException();
Util.nullpo(filter);
gFilters.add(filter);
}

Expand Down
2 changes: 1 addition & 1 deletion GalaxiAPI/src/net/ME1312/Galaxi/Log/TextElement.java
Expand Up @@ -45,7 +45,7 @@ private static ObjectMap<String> generate(String text) {
* @param element Raw Element
*/
public TextElement(ObjectMap<String> element) {
if (Util.isNull(element)) throw new NullPointerException();
Util.nullpo(element);
this.element = element;
load();
}
Expand Down
14 changes: 7 additions & 7 deletions GalaxiAPI/src/net/ME1312/Galaxi/Plugin/PluginInfo.java
Expand Up @@ -64,7 +64,7 @@ public static class Dependency {
* @param required Required Status
*/
protected Dependency(String name, Version minversion, Version maxversion, boolean required) {
if (Util.isNull(name, required)) throw new NullPointerException();
Util.nullpo(name, required);
this.name = name;
this.minversion = minversion;
this.maxversion = maxversion;
Expand Down Expand Up @@ -208,7 +208,7 @@ public static PluginInfo get(Class<?> main) {
* @param dependencies Dependencies List
*/
protected PluginInfo(Object plugin, String name, Version version, List<String> authors, String description, URL website, List<String> loadBefore, List<Dependency> dependencies) {
if (Util.isNull(plugin, name, version, authors)) throw new NullPointerException();
Util.nullpo(plugin, name, version, authors);
if (name.length() == 0) throw new StringIndexOutOfBoundsException("Cannot use an empty name");
if (version.toString().length() == 0) throw new StringIndexOutOfBoundsException("Cannot use an empty version");
if (authors.size() == 0) throw new ArrayIndexOutOfBoundsException("Cannot use an empty authors list");
Expand Down Expand Up @@ -460,7 +460,7 @@ public void setEnabled(boolean value) {
* @param value Value
*/
public void setLogger(Logger value) {
if (Util.isNull(value)) throw new NullPointerException();
Util.nullpo(value);
logger = value;
}

Expand Down Expand Up @@ -504,19 +504,19 @@ public File getDataFolder() {

@Override
public void addExtra(String handle, Object value) {
if (Util.isNull(handle, value)) throw new NullPointerException();
Util.nullpo(handle, value);
extra.set(handle, value);
}

@Override
public boolean hasExtra(String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
Util.nullpo(handle);
return extra.getKeys().contains(handle);
}

@Override
public ObjectMapValue<String> getExtra(String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
Util.nullpo(handle);
return extra.get(handle);
}

Expand All @@ -527,7 +527,7 @@ public ObjectMap<String> getExtra() {

@Override
public void removeExtra(String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
Util.nullpo(handle);
extra.remove(handle);
}
}
17 changes: 10 additions & 7 deletions GalaxiAPI/src/net/ME1312/Galaxi/Plugin/PluginManager.java
Expand Up @@ -31,7 +31,7 @@ public abstract class PluginManager {
* @return PluginInfo
*/
public PluginInfo getPlugin(String name) {
if (Util.isNull(name)) throw new NullPointerException();
Util.nullpo(name);
return getPlugins().get(name.toLowerCase());
}

Expand All @@ -42,7 +42,7 @@ public PluginInfo getPlugin(String name) {
* @return PluginInfo
*/
public PluginInfo getPlugin(Class<?> main) {
if (Util.isNull(main)) throw new NullPointerException();
Util.nullpo(main);
return PluginInfo.get(main);
}

Expand All @@ -53,7 +53,7 @@ public PluginInfo getPlugin(Class<?> main) {
* @return PluginInfo
*/
public PluginInfo getPlugin(Object main) {
if (Util.isNull(main)) throw new NullPointerException();
Util.nullpo(main);
return PluginInfo.get(main);
}

Expand Down Expand Up @@ -89,7 +89,8 @@ public void removeCommand(String... handles) {
*/
@SuppressWarnings("unchecked")
public void registerListeners(PluginInfo plugin, Object... listeners) {
if (plugin == null || Util.isNull(listeners)) throw new NullPointerException();
Util.nullpo(plugin);
Util.nullpo(listeners);
synchronized (this.listeners) {
LinkedList<Class<? extends Event>> update = new LinkedList<>();
for (Object listener : listeners) {
Expand Down Expand Up @@ -147,7 +148,8 @@ public final <T extends Event> void registerListener(PluginInfo plugin, Class<T>
@SuppressWarnings({"unchecked", "rawtypes"})
@SafeVarargs
public final <T extends Event> void registerListener(PluginInfo plugin, Class<T> event, Number order, Listener<? extends T>... listeners) {
if (Util.isNull(plugin, event) || Util.isNull((Object[]) listeners)) throw new NullPointerException();
Util.nullpo(plugin, event, listeners);
Util.nullpo((Object[]) listeners);
synchronized (this.listeners) {
for (Listener listener : listeners) {
try {
Expand Down Expand Up @@ -198,7 +200,8 @@ public void unregisterListeners(PluginInfo plugin) {
* @param listeners Listeners
*/
public void unregisterListeners(PluginInfo plugin, Object... listeners) {
if (plugin == null || (listeners != null && Util.isNull(listeners))) throw new NullPointerException();
Util.nullpo(plugin, listeners);
Util.nullpo(listeners);
synchronized (this.listeners) {
LinkedList<Class<? extends Event>> update = new LinkedList<>();
for (Map.Entry<Class<? extends Event>, Map<Short, Map<PluginInfo, Map<Object, List<BakedListener>>>>> event : this.listeners.entrySet()) {
Expand Down Expand Up @@ -290,7 +293,7 @@ public void executeEvent(Event event) {
*/
@SuppressWarnings("unchecked")
public <T extends Event> void executeEvent(Class<T> type, T event) {
if (Util.isNull(event)) throw new NullPointerException();
Util.nullpo(event);
if (!type.isInstance(event)) throw new ClassCastException(event.getClass().getCanonicalName() + " cannot be cast to " + type.getCanonicalName());
List<BakedListener> listeners = this.baked.get(type);

Expand Down
4 changes: 2 additions & 2 deletions GalaxiEngine/src/net/ME1312/Galaxi/Engine/GalaxiEngine.java
Expand Up @@ -22,7 +22,7 @@ public abstract class GalaxiEngine extends Galaxi {
* @return The GalaxiEngine
*/
public static GalaxiEngine init(Object app) throws Exception {
if (Util.isNull(app)) throw new NullPointerException();
Util.nullpo(app);
if (instance == null) {
return Util.reflect(Class.forName("net.ME1312.Galaxi.Engine.Runtime.Engine").getDeclaredConstructor(PluginInfo.class), PluginInfo.load(app));
} else throw new IllegalStateException("Engine already initialized");
Expand All @@ -35,7 +35,7 @@ public static GalaxiEngine init(Object app) throws Exception {
* @return The GalaxiEngine
*/
public static GalaxiEngine init(PluginInfo app) throws Exception {
if (Util.isNull(app)) throw new NullPointerException();
Util.nullpo(app);
if (instance == null) {
return Util.reflect(Class.forName("net.ME1312.Galaxi.Engine.Runtime.Engine").getDeclaredConstructor(PluginInfo.class), app);
} else throw new IllegalStateException("Engine already initialized");
Expand Down
24 changes: 12 additions & 12 deletions GalaxiRT/Engine/src/net/ME1312/Galaxi/Engine/Runtime/Console.java
Expand Up @@ -129,13 +129,13 @@ static Color parse256(int color) {

@Override
public List<String> complete(CommandSender sender, String command) {
if (Util.isNull(sender, command)) throw new NullPointerException();
Util.nullpo(sender, command);
return complete(sender, parseCommand(command));
}

@Override
public List<String> complete(CommandSender sender, Parsed command) {
if (Util.isNull(sender, command)) throw new NullPointerException();
Util.nullpo(sender, command);

LinkedList<String> candidates = new LinkedList<String>();
if (command != null && command.line().codePoints().count() > 0 && (!(command instanceof ParsedInput) || ((ParsedInput) command).isCommand())) {
Expand All @@ -155,7 +155,7 @@ public List<String> complete(CommandSender sender, Parsed command) {
CompletionHandler autocompletor = commands.get(label.toLowerCase()).autocomplete();
if (autocompletor != null)
for (String autocomplete : autocompletor.complete(sender, label, args))
if (!Util.isNull(autocomplete) && autocomplete.length() > 0)
if (autocomplete != null && autocomplete.length() > 0)
candidates.add(autocomplete);
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ private void read() {
jstatus = false;
}
void read(String line) {
if (Util.isNull(line)) throw new NullPointerException();
Util.nullpo(line);

ConsoleInputEvent ie = new ConsoleInputEvent(engine, line);
engine.code.executeEvent(ie);
Expand All @@ -208,13 +208,13 @@ void read(String line) {

@Override
public Status runCommand(CommandSender sender, String command) {
if (Util.isNull(sender, command)) throw new NullPointerException();
Util.nullpo(sender, command);
return runCommand(sender, parseCommand(command));
}

@Override
public Status runCommand(CommandSender sender, Parsed command) {
if (Util.isNull(sender, command)) throw new NullPointerException();
Util.nullpo(sender, command);

LinkedList<String> arguments = command.words();
String label = arguments.getFirst();
Expand Down Expand Up @@ -245,7 +245,7 @@ public Status runCommand(CommandSender sender, Parsed command) {

@Override
public String escapeCommand(String label, String[] args, boolean literal, boolean whitespaced) {
if (Util.isNull(label, args)) throw new NullPointerException();
Util.nullpo(label, args);

StringBuilder builder = new StringBuilder();
builder.append('/');
Expand All @@ -259,7 +259,7 @@ public String escapeCommand(String label, String[] args, boolean literal, boolea

@Override
public String escapeArguments(String[] args, boolean literal, boolean whitespaced) {
if (Util.isNull((Object) args)) throw new NullPointerException();
Util.nullpo((Object) args);

StringBuilder builder = new StringBuilder();
for (int i = 0; i < args.length; i++) {
Expand All @@ -270,7 +270,7 @@ public String escapeArguments(String[] args, boolean literal, boolean whitespace
}

private String escapeArgument(Pair<String, String> start, String arg, boolean literal, boolean whitespaced, boolean complete) {
if (Util.isNull((Object) arg)) throw new NullPointerException();
Util.nullpo((Object) arg);
boolean append = start != null && arg.startsWith(start.value());
if (append) arg = arg.substring(start.value().length());

Expand Down Expand Up @@ -304,17 +304,17 @@ private String escapeArgument(Pair<String, String> start, String arg, boolean li

@Override
public ParsedInput parseCommand(String command) {
if (Util.isNull(command)) throw new NullPointerException();
Util.nullpo(command);
return parser.parse(command, (int) command.codePoints().count(), true);
}

ParsedInput parse(String input, boolean command) {
if (Util.isNull(input)) throw new NullPointerException();
Util.nullpo(input);
return parser.parse(input, (int) input.codePoints().count(), command);
}

ParsedInput parse(String input) {
if (Util.isNull(input)) throw new NullPointerException();
Util.nullpo(input);
return parser.parse(input, (int) input.codePoints().count(), null);
}

Expand Down

0 comments on commit be43ac3

Please sign in to comment.