Gridarta Editor
Spells.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 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.spells;
21 
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.Iterator;
26 import java.util.List;
27 import org.jetbrains.annotations.NotNull;
28 
33 public class Spells<S extends Spell> implements Iterable<S> {
34 
38  @NotNull
39  private final List<S> spells = new ArrayList<>();
40 
44  private static final Comparator<Spell> SPELL_COMPARATOR = (o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.getName(), o2.getName());
45 
49  public void sort() {
51  }
52 
57  public int size() {
58  return spells.size();
59  }
60 
65  @NotNull
66  @Override
67  public Iterator<S> iterator() {
68  return Collections.unmodifiableList(spells).iterator();
69  }
70 
75  public void add(@NotNull final S spell) {
76  spells.add(spell);
77  }
78 
84  public S getSpell(final int index) {
85  try {
86  return spells.get(index);
87  } catch (final IndexOutOfBoundsException ignored) {
88  return spells.get(0);
89  }
90  }
91 
92 }
net.sf.gridarta.model.spells.Spells.SPELL_COMPARATOR
static final Comparator< Spell > SPELL_COMPARATOR
A comparator to sort spells by name.
Definition: Spells.java:44
net.sf.gridarta.model.spells.Spells.spells
final List< S > spells
All defined spells.
Definition: Spells.java:39
net.sf.gridarta.model.spells.Spell
Describes an in-game spell.
Definition: Spell.java:28
net.sf.gridarta.model.spells.Spells.add
void add(@NotNull final S spell)
Add a spell.
Definition: Spells.java:75
net.sf.gridarta.model.spells.Spells.iterator
Iterator< S > iterator()
Return all known spell objects.
Definition: Spells.java:67
net.sf.gridarta.model.spells.Spells.size
int size()
Return the number of existing spell objects.
Definition: Spells.java:57
net.sf.gridarta.model.spells.Spells.getSpell
S getSpell(final int index)
Return one spell object by index.
Definition: Spells.java:84
net.sf.gridarta.model.spells.Spells.sort
void sort()
Sort the added spells after loading is finished.
Definition: Spells.java:49
net.sf.gridarta.model.spells.Spells
Common base class for spells and spell lists.
Definition: Spells.java:33