1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-08 08:23:11 +00:00

Refactored Filters

Signed-off-by: Rogiel <rogiel@rogiel.com>
This commit is contained in:
2011-05-16 15:08:30 -03:00
parent 242616d384
commit df36f9bb32
38 changed files with 322 additions and 80 deletions

View File

@@ -20,9 +20,9 @@ import java.util.Iterator;
import java.util.List;
import com.l2jserver.model.world.WorldObject;
import com.l2jserver.model.world.filter.WorldObjectFilter;
import com.l2jserver.service.Service;
import com.l2jserver.service.game.world.event.WorldEventDispatcher;
import com.l2jserver.service.game.world.filter.WorldObjectFilter;
/**
* Service responsible for managing {@link WorldObject} and dispatch events.

View File

@@ -25,13 +25,13 @@ import org.slf4j.LoggerFactory;
import com.google.inject.Inject;
import com.l2jserver.model.world.WorldObject;
import com.l2jserver.model.world.filter.WorldObjectFilter;
import com.l2jserver.model.world.filter.impl.InstanceFilter;
import com.l2jserver.model.world.iterator.FilterIterator;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.ServiceStopException;
import com.l2jserver.service.game.world.event.WorldEventDispatcher;
import com.l2jserver.service.game.world.filter.FilterIterator;
import com.l2jserver.service.game.world.filter.WorldObjectFilter;
import com.l2jserver.service.game.world.filter.impl.InstanceFilter;
import com.l2jserver.util.factory.CollectionFactory;
/**

View File

@@ -0,0 +1,32 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.event;
import com.l2jserver.model.world.WorldObject;
import com.l2jserver.model.world.capability.Listenable;
public interface WorldEvent {
/**
* @return the object that issued this event
*/
WorldObject getObject();
/**
* @return the list of objects that will be notified of this event
*/
Listenable<?, ?>[] getDispatchableObjects();
}

View File

@@ -18,8 +18,6 @@ package com.l2jserver.service.game.world.event;
import com.l2jserver.model.id.ObjectID;
import com.l2jserver.model.world.capability.Listenable;
import com.l2jserver.model.world.event.WorldEvent;
import com.l2jserver.model.world.event.WorldListener;
/**
* This event dispatcher notify listeners that an certain event occured in their

View File

@@ -25,8 +25,6 @@ import org.slf4j.LoggerFactory;
import com.l2jserver.model.id.ObjectID;
import com.l2jserver.model.world.capability.Listenable;
import com.l2jserver.model.world.event.WorldEvent;
import com.l2jserver.model.world.event.WorldListener;
import com.l2jserver.util.factory.CollectionFactory;
/**

View File

@@ -0,0 +1,38 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.event;
/**
* This is the most abstract listener for the listening engine.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <E>
* the received event type
*/
public interface WorldListener<E extends WorldEvent> {
/**
* Once the event call is dispatched the listener <b>WILL</b> be removed if
* false is returned. If you wish to keep this listener, you must return
* true.
*
* @param e
* the event
* @return true to keep listener alive
*/
boolean dispatch(E e);
}

View File

@@ -0,0 +1,54 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.filter;
import com.l2jserver.model.world.WorldObject;
/**
* <tt>AND</tt> filter that accepts all values in which all other
* <tt>filters</tt> return true.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the item type
*/
public class AndFilter<O extends WorldObject> implements WorldObjectFilter<O> {
/**
* The filters
*/
private WorldObjectFilter<O>[] filters;
/**
* Creates a new instance
*
* @param filters
* filters to be used with <tt>AND</tt> operator
*/
public AndFilter(WorldObjectFilter<O>... filters) {
this.filters = filters;
}
@Override
public boolean accept(O object) {
for (final WorldObjectFilter<O> filter : filters) {
if (!filter.accept(object))
return false;
}
return true;
}
}

View File

@@ -0,0 +1,101 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.filter;
import java.util.Iterator;
import com.l2jserver.model.world.WorldObject;
/**
* The {@link FilterIterator} takes an {@link WorldObject} and a
* {@link WorldObjectFilter} and dynamically iterates over the items and find
* the next matching object.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the object type
*/
public class FilterIterator<O extends WorldObject> implements Iterator<O> {
/**
* The unfiltered object iterator
*/
private final Iterator<WorldObject> objects;
/**
* The filter
*/
private final WorldObjectFilter<O> filter;
/**
* The next object found
*/
private O selected;
/**
* Creates a new instance
*
* @param filter
* the filter
* @param objects
* the unfiltered object iterator
*/
public FilterIterator(final WorldObjectFilter<O> filter,
Iterator<WorldObject> objects) {
this.filter = filter;
this.objects = objects;
}
@Override
public boolean hasNext() {
O next = findNext();
return (next != null);
}
@Override
public O next() {
try {
return findNext();
} finally {
selected = null;
}
}
@Override
public void remove() {
}
/**
* Locates the next matching object
*
* @return the next matching object
*/
private O findNext() {
if (selected != null)
return selected;
while (objects.hasNext()) {
try {
@SuppressWarnings("unchecked")
final O object = (O) objects.next();
if (filter.accept(object)) {
selected = object;
return selected;
}
} catch (ClassCastException e) {
}
}
return null;
}
}

View File

@@ -0,0 +1,50 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.filter;
import com.l2jserver.model.world.WorldObject;
/**
* And filter that accepts all values in which the other <tt>filter</tt> return
* false.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the item type
*/
public class NotFilter<O extends WorldObject> implements WorldObjectFilter<O> {
/**
* The filter
*/
private WorldObjectFilter<O> filter;
/**
* Creates a new instance
*
* @param filter
* the filter
*/
public NotFilter(WorldObjectFilter<O> filter) {
this.filter = filter;
}
@Override
public boolean accept(O object) {
return !filter.accept(object);
}
}

View File

@@ -0,0 +1,54 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.filter;
import com.l2jserver.model.world.WorldObject;
/**
* <tt>OR</tt> filter that accepts all values in which at least one of the
* <tt>filters</tt> return true.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
* @param <O>
* the item type
*/
public class OrFilter<O extends WorldObject> implements WorldObjectFilter<O> {
/**
* The filters
*/
private WorldObjectFilter<O>[] filters;
/**
* Creates a new instance
*
* @param filters
* filters to be used with <tt>OR</tt> operator
*/
public OrFilter(WorldObjectFilter<O>... filters) {
this.filters = filters;
}
@Override
public boolean accept(O object) {
for (final WorldObjectFilter<O> filter : filters) {
if (filter.accept(object))
return true;
}
return false;
}
}

View File

@@ -0,0 +1,68 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.filter;
import com.l2jserver.model.world.WorldObject;
/**
* Utility class for common filter types
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public final class WorldFilters {
/**
* Performs an <tt>AND</tt> operation
*
* @param <O>
* the object type
* @param filters
* the filters
* @return the {@link AndFilter}
*/
public static final <O extends WorldObject> WorldObjectFilter<O> and(
WorldObjectFilter<O>... filters) {
return new AndFilter<O>(filters);
}
/**
* Performs an <tt>OR</tt> operation
*
* @param <O>
* the object type
* @param filters
* the filters
* @return the {@link OrFilter}
*/
public static final <O extends WorldObject> WorldObjectFilter<O> or(
WorldObjectFilter<O>... filters) {
return new OrFilter<O>(filters);
}
/**
* Performs an <tt>NOTA</tt> operation
*
* @param <O>
* the object type
* @param filters
* the filters
* @return the {@link NotFilter}
*/
public static final <O extends WorldObject> WorldObjectFilter<O> not(
WorldObjectFilter<O> filter) {
return new NotFilter<O>(filter);
}
}

View File

@@ -0,0 +1,35 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.filter;
import com.l2jserver.model.world.WorldObject;
/**
* Filter an object in a world
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface WorldObjectFilter<O extends WorldObject> {
/**
* Test if <tt>object</tt> matches the filter requirements
*
* @param object
* the object
* @return true if object match requirements
*/
boolean accept(O object);
}

View File

@@ -0,0 +1,50 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.filter.impl;
import com.l2jserver.model.id.ObjectID;
import com.l2jserver.model.world.capability.Positionable;
import com.l2jserver.service.game.world.filter.WorldObjectFilter;
/**
* Filter objects based on its ID.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class IDFilter implements WorldObjectFilter<Positionable> {
/**
* The object id
*/
private final ObjectID<?> id;
/**
* Creates a new instance
*
* @param id
* the desired object ID
*/
public IDFilter(final ObjectID<?> id) {
this.id = id;
}
@Override
public boolean accept(Positionable other) {
if (other == null)
return false;
return other.getID().equals(id);
}
}

View File

@@ -0,0 +1,52 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.filter.impl;
import com.l2jserver.model.world.WorldObject;
import com.l2jserver.service.game.world.filter.WorldObjectFilter;
/**
* Filter object based on their types
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
* @param <T>
* the instance type
*/
public class InstanceFilter<T extends WorldObject> implements
WorldObjectFilter<T> {
/**
* The object's type
*/
private final Class<?> type;
/**
* Creates a new instance
*
* @param instance
* the instance type
*/
public InstanceFilter(Class<?> instance) {
this.type = instance;
}
@Override
public boolean accept(T other) {
if (other == null)
return false;
return type.isInstance(other);
}
}

View File

@@ -0,0 +1,69 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.filter.impl;
import com.l2jserver.model.world.capability.Positionable;
import com.l2jserver.service.game.world.filter.WorldObjectFilter;
import com.l2jserver.util.dimensional.Coordinate;
/**
* Filter objects that are in the <tt>range</tt> of <tt>coordinate</tt>
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class RangeFilter implements WorldObjectFilter<Positionable> {
/**
* The coordinate point
*/
private final Coordinate coordinate;
/**
* The desired maximum distance of the object
*/
private final int range;
/**
* Creates a new instance
*
* @param coordinate
* the coordinate as base for range search
* @param range
* the desired maximum distance of the object
*/
public RangeFilter(final Coordinate coordinate, final int range) {
this.coordinate = coordinate;
this.range = range;
}
/**
* Creates a new instance
*
* @param positionable
* the base object
* @param range
* the desired maximum distance of the object
*/
public RangeFilter(final Positionable positionable, final int range) {
this(positionable.getPosition(), range);
}
@Override
public boolean accept(Positionable other) {
if (other == null)
return false;
return other.getPosition().getDistance(coordinate) <= range;
}
}