Add PredicateVisibleTo and improve PredicateAnd
This commit is contained in:
parent
eaeccbf036
commit
6f73837b4f
@ -1,36 +1,35 @@
|
|||||||
package com.massivecraft.massivecore;
|
package com.massivecraft.massivecore;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
|
||||||
public class PredicateAnd<T> implements Predicate<T>
|
public class PredicateAnd<T> implements Predicate<T>
|
||||||
{
|
{
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// INSTANCE & CONSTRUCT
|
// INSTANCE & CONSTRUCT
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public static <T> PredicateAnd<T> get(Predicate<T>... predicates) { return new PredicateAnd<T>(predicates); }
|
public static <T> PredicateAnd<T> get(Predicate<? super T>... predicates) { return new PredicateAnd<T>(predicates); }
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public PredicateAnd(Predicate<T>... predicates)
|
public PredicateAnd(Predicate<? super T>... predicates)
|
||||||
{
|
{
|
||||||
this(Arrays.asList(predicates));
|
this(ImmutableList.copyOf(predicates));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> PredicateAnd<T> get(Collection<Predicate<T>> predicates) { return new PredicateAnd<T>(predicates); }
|
public static <T> PredicateAnd<T> get(Collection<Predicate<? super T>> predicates) { return new PredicateAnd<T>(predicates); }
|
||||||
public PredicateAnd(Collection<Predicate<T>> predicates)
|
public PredicateAnd(Collection<Predicate<? super T>> predicates)
|
||||||
{
|
{
|
||||||
this.predicates = Collections.unmodifiableList(new ArrayList<Predicate<T>>(predicates));
|
this.predicates = ImmutableList.copyOf(predicates);
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// FIELDS
|
// FIELDS
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
private final List<Predicate<T>> predicates;
|
private final List<Predicate<? super T>> predicates;
|
||||||
public List<Predicate<T>> getPredicates() { return this.predicates; }
|
public List<Predicate<? super T>> getPredicates() { return this.predicates; }
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// OVERRIDE
|
// OVERRIDE
|
||||||
@ -39,7 +38,7 @@ public class PredicateAnd<T> implements Predicate<T>
|
|||||||
@Override
|
@Override
|
||||||
public boolean apply(T type)
|
public boolean apply(T type)
|
||||||
{
|
{
|
||||||
for (Predicate<T> predicate : this.getPredicates())
|
for (Predicate<? super T> predicate : this.getPredicates())
|
||||||
{
|
{
|
||||||
if (!predicate.apply(type)) return false;
|
if (!predicate.apply(type)) return false;
|
||||||
}
|
}
|
||||||
|
38
src/com/massivecraft/massivecore/PredicateVisibleTo.java
Normal file
38
src/com/massivecraft/massivecore/PredicateVisibleTo.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package com.massivecraft.massivecore;
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
import com.massivecraft.massivecore.mixin.Mixin;
|
||||||
|
import com.massivecraft.massivecore.util.IdUtil;
|
||||||
|
|
||||||
|
public class PredicateVisibleTo implements Predicate<Object>
|
||||||
|
{
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// FIELDS
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
private final WeakReference<CommandSender> watcher;
|
||||||
|
public CommandSender getWatcher() { return this.watcher.get(); }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// INSTANCE & CONSTRUCT
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
public static PredicateVisibleTo get(Object watcherObject) { return new PredicateVisibleTo(watcherObject); }
|
||||||
|
public PredicateVisibleTo(Object watcherObject)
|
||||||
|
{
|
||||||
|
this.watcher = new WeakReference<>(IdUtil.getSender(watcherObject));
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// OVERRIDE
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(Object watcheeObject)
|
||||||
|
{
|
||||||
|
return Mixin.isVisible(watcheeObject, this.getWatcher());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user