mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-11-27 21:26:17 +01:00
Add ClassSource.empty, ClassSource.attemptLoadFrom will also not
tolerate nulls
This commit is contained in:
parent
95087a5b9f
commit
0f5be7f1f1
@ -1,5 +1,6 @@
|
||||
package com.comphenix.protocol.utility;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -57,18 +58,32 @@ public abstract class ClassSource {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A ClassLoader which will never successfully load a class.
|
||||
*/
|
||||
public static ClassSource empty(){
|
||||
return fromMap(Collections.<String, Class<?>>emptyMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a class source that will attempt lookups in each of the given sources in the order they are in the array, and return the first value that is found.
|
||||
* If the sources array is empty, null, or composed of all null elements, the returned ClassSource will be null. Null elements in the array are ignored.
|
||||
* If the sources array is null or composed of any null elements, an exception will be thrown.
|
||||
* @param sources - the class sources.
|
||||
* @return A new class source.
|
||||
*/
|
||||
public static ClassSource attemptLoadFrom(final ClassSource... sources) {
|
||||
ClassSource source = sources != null && sources.length >= 1 ? sources[0] : null;
|
||||
if(sources.length == 0){ // Throws NPE if sources is null, which is what we want
|
||||
return ClassSource.empty();
|
||||
}
|
||||
|
||||
ClassSource source = sources[0];
|
||||
for(int i = 1; i < sources.length; i++){
|
||||
source = sources[i] == null ? source :
|
||||
source == null ? sources[i] : source.retry(sources[i]);
|
||||
if(source == null || sources[i] == null){
|
||||
throw new IllegalArgumentException("Null values are not permitted as ClassSources.");
|
||||
}
|
||||
|
||||
source = source.retry(sources[i]);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user