Gridarta Editor
AutojoinList.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.autojoin;
21 
22 import java.io.Serializable;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.IdentityHashMap;
26 import java.util.List;
27 import java.util.Map;
31 import org.apache.log4j.Category;
32 import org.apache.log4j.Logger;
33 import org.jetbrains.annotations.NotNull;
34 
39 public class AutojoinList<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Serializable {
40 
44  private static final long serialVersionUID = 1L;
45 
49  @NotNull
50  private static final Category LOG = Logger.getLogger(AutojoinList.class);
51 
55  public static final int SIZE = 16;
56 
57  // bitmask constants for {@link #archetypes} index
58 
59  public static final int NORTH = 1;
60 
61  public static final int EAST = 2;
62 
63  public static final int SOUTH = 4;
64 
65  public static final int WEST = 8;
66 
83  @NotNull
84  private final List<List<R>> archetypes;
85 
92  public AutojoinList(@NotNull final Iterable<List<R>> archetypes) throws IllegalAutojoinListException {
93  final ArrayList<List<R>> newArchetypes = new ArrayList<>();
94 
95  for (final List<R> archetypeList : archetypes) {
96  newArchetypes.add(validate(archetypeList));
97  }
98 
99  if (newArchetypes.size() > SIZE) {
100  throw new IllegalAutojoinListException("autojoin list with more than " + SIZE + " valid entries");
101  } else if (newArchetypes.size() < SIZE) {
102  throw new IllegalAutojoinListException("autojoin list with less than " + SIZE + " valid entries");
103  }
104 
105  newArchetypes.trimToSize();
106  this.archetypes = newArchetypes;
107  }
108 
116  @NotNull
117  private static <R extends Archetype<?, ?, R>> List<R> validate(@NotNull final Iterable<R> archetypeList) throws IllegalAutojoinListException {
118  final ArrayList<R> result = new ArrayList<>();
119  final Map<R, R> tmp = new IdentityHashMap<>();
120  for (final R archetype : archetypeList) {
121  if (archetype == null) {
122  throw new IllegalArgumentException();
123  }
124 
125  if (tmp.put(archetype, archetype) != null) {
126  throw new IllegalAutojoinListException("autojoin list contains duplicate archetype '" + archetype.getArchetypeName() + "'");
127  }
128 
129  result.add(archetype);
130  }
131  if (result.isEmpty()) {
132  throw new IllegalAutojoinListException("autojoin list is empty");
133  }
134  result.trimToSize();
135  return result;
136  }
137 
143  public int getIndex(@NotNull final R archetype) {
144  for (int i = 0; i < SIZE; i++) {
145  if (archetypes.get(i).contains(archetype)) {
146  return i;
147  }
148  }
149 
150  LOG.warn("Error in AutojoinList.get_index: index not found");
151  return 0;
152  }
153 
160  public boolean isMainIndex(@NotNull final R archetype) {
161  for (int i = 0; i < SIZE; i++) {
162  if (archetypes.get(i).get(0) == archetype) {
163  return true;
164  }
165  }
166 
167  return false;
168  }
169 
176  public int getAlternativeIndex(@NotNull final R archetype) {
177  for (int i = 0; i < SIZE; i++) {
178  final List<R> tmp = archetypes.get(i);
179  if (tmp.get(0) != archetype && tmp.contains(archetype)) {
180  return i;
181  }
182  }
183 
184  return -1;
185  }
186 
190  @NotNull
191  public R getArchetype(final int index) {
192  return archetypes.get(index).get(0);
193  }
194 
200  @NotNull
201  public Iterable<R> getArchetypes(final int index) {
202  return Collections.unmodifiableCollection(archetypes.get(index));
203  }
204 
205 }
R getArchetype(final int index)
TypeMayBeWeakened
static final Category LOG
The Logger for printing log messages.
final List< List< R > > archetypes
Maps index to list of archetypes representing autojoin-able archetypes for this direction.
static< R extends Archetype<?, ?, R > List< R > validate(@NotNull final Iterable< R > archetypeList)
Validates a list of archetypes: no entry must be.
AutojoinList(@NotNull final Iterable< List< R >> archetypes)
Create an AutojoinList.
Contains a list of (typically wall-)arches which do autojoining.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
boolean isMainIndex(@NotNull final R archetype)
Returns the index of an Archetype if it is a main archetype for any direction.
GameObjects are the objects based on Archetypes found on maps.
int getAlternativeIndex(@NotNull final R archetype)
Returns the index of an Archetype if it is an alternative archetype for any direction.
int getIndex(@NotNull final R archetype)
Looks up the given node in the archetype array of this class.
Iterable< R > getArchetypes(final int index)
Returns all archetypes for an index.
static final int SIZE
The number of archetypes in an autojoin list.
static final long serialVersionUID
The serial version UID.