Gridarta Editor
FilterGameObjectIterator.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.mapmodel;
21 
22 import java.util.Iterator;
23 import java.util.NoSuchElementException;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
36 public class FilterGameObjectIterator<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Iterator<G> {
37 
41  @NotNull
42  private final Iterator<G> iterator;
43 
47  @NotNull
48  private final GameObjectMatcher matcher;
49 
54  @Nullable
55  private G next;
56 
60  private boolean findNextPending = true;
61 
67  public FilterGameObjectIterator(@NotNull final Iterator<G> iterator, @NotNull final GameObjectMatcher matcher) {
68  this.iterator = iterator;
69  this.matcher = matcher;
70  }
71 
72  @Override
73  public boolean hasNext() {
74  findNext();
75  return next != null;
76  }
77 
78  @Override
79  public G next() {
80  findNext();
81  if (next == null) {
82  throw new NoSuchElementException();
83  }
84  findNextPending = true;
85  return next;
86  }
87 
88  @Override
89  public void remove() {
90  if (findNextPending) {
91  throw new IllegalStateException();
92  }
93  findNextPending = true;
94  iterator.remove();
95  }
96 
101  private void findNext() {
102  if (!findNextPending) {
103  return;
104  }
105  findNextPending = false;
106 
107  while (true) {
108  if (!iterator.hasNext()) {
109  next = null;
110  return;
111  }
112 
113  final G tmp = iterator.next();
114  if (matcher.isMatching(tmp)) {
115  next = tmp;
116  return;
117  }
118  }
119  }
120 
121 }
FilterGameObjectIterator(@NotNull final Iterator< G > iterator, @NotNull final GameObjectMatcher matcher)
Creates a new instance.
Interface for classes that match GameObjects.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
boolean findNextPending
Whether findNext() has to be called.
void findNext()
Updates next to hold the next matching game object.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
An Iterator that filters another iterator according to a GameObjectMatcher.
GameObjects are the objects based on Archetypes found on maps.
final Iterator< G > iterator
The Iterator being filtered.
final GameObjectMatcher matcher
The GameObjectMatcher for filtering returned game objects.
boolean isMatching(@NotNull GameObject<?, ?, ?> gameObject)
Matches an GameObject.