Add PredicateVisibleTo and improve PredicateAnd

This commit is contained in:
BuildTools 2016-01-25 09:48:58 +01:00 committed by Olof Larsson
parent eaeccbf036
commit 6f73837b4f
2 changed files with 49 additions and 12 deletions

View File

@ -1,36 +1,35 @@
package com.massivecraft.massivecore;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import com.google.common.collect.ImmutableList;
public class PredicateAnd<T> implements Predicate<T>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
@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
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 PredicateAnd(Collection<Predicate<T>> predicates)
public static <T> PredicateAnd<T> get(Collection<Predicate<? super T>> predicates) { return new PredicateAnd<T>(predicates); }
public PredicateAnd(Collection<Predicate<? super T>> predicates)
{
this.predicates = Collections.unmodifiableList(new ArrayList<Predicate<T>>(predicates));
this.predicates = ImmutableList.copyOf(predicates);
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final List<Predicate<T>> predicates;
public List<Predicate<T>> getPredicates() { return this.predicates; }
private final List<Predicate<? super T>> predicates;
public List<Predicate<? super T>> getPredicates() { return this.predicates; }
// -------------------------------------------- //
// OVERRIDE
@ -39,7 +38,7 @@ public class PredicateAnd<T> implements Predicate<T>
@Override
public boolean apply(T type)
{
for (Predicate<T> predicate : this.getPredicates())
for (Predicate<? super T> predicate : this.getPredicates())
{
if (!predicate.apply(type)) return false;
}

View 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());
}
}