001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2010 The Gridarta Developers.
004 *
005 * This program is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 2 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License along
016 * with this program; if not, write to the Free Software Foundation, Inc.,
017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018 */
019
020package net.sf.gridarta.model.autojoin;
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.regex.Pattern;
025import net.sf.gridarta.model.archetype.TestArchetype;
026import net.sf.gridarta.model.gameobject.TestGameObject;
027import net.sf.gridarta.model.maparchobject.TestMapArchObject;
028import net.sf.gridarta.model.mapmodel.TestMapModelCreator;
029import org.jetbrains.annotations.NotNull;
030import org.junit.Assert;
031
032/**
033 * Implements {@link AutojoinList} related functions.
034 * @author Andreas Kirschbaum
035 */
036public class AutojoinListsHelper {
037
038    /**
039     * A {@link Pattern} that matches alternative archetypes.
040     */
041    @NotNull
042    private static final Pattern ALTERNATIVES_PATTERN = Pattern.compile("\\|");
043
044    /**
045     * The {@link TestMapModelCreator} instance.
046     */
047    @NotNull
048    private final TestMapModelCreator mapModelCreator;
049
050    /**
051     * The {@link AutojoinLists} instance.
052     */
053    @NotNull
054    private final AutojoinLists<TestGameObject, TestMapArchObject, TestArchetype> autojoinLists;
055
056    /**
057     * Creates a new instance.
058     */
059    public AutojoinListsHelper() {
060        this(new TestMapModelCreator(false));
061    }
062
063    /**
064     * Creates a new instance.
065     * @param mapModelCreator the map model creator instance
066     */
067    public AutojoinListsHelper(@NotNull final TestMapModelCreator mapModelCreator) {
068        this.mapModelCreator = mapModelCreator;
069        autojoinLists = mapModelCreator.getAutojoinLists();
070    }
071
072    /**
073     * Creates a new {@link AutojoinLists} instance.
074     * @param archetypeNames the names of the archetypes
075     * @throws IllegalAutojoinListException if the autojoin lists instance
076     * cannot be created
077     */
078    public void newAutojoinLists(@NotNull final String... archetypeNames) throws IllegalAutojoinListException {
079        final List<List<TestArchetype>> archetypes = new ArrayList<List<TestArchetype>>();
080        for (final String archetypeNameList : archetypeNames) {
081            final List<TestArchetype> archetypeList = new ArrayList<TestArchetype>();
082            if (!archetypeNameList.isEmpty()) {
083                for (final String archetypeName : ALTERNATIVES_PATTERN.split(archetypeNameList, -1)) {
084                    archetypeList.add(mapModelCreator.getArchetype(archetypeName));
085                }
086            }
087            archetypes.add(archetypeList);
088        }
089        autojoinLists.addAutojoinList(new AutojoinList<TestGameObject, TestMapArchObject, TestArchetype>(archetypes));
090    }
091
092    /**
093     * Creates a new {@link AutojoinLists} instance and expects an error.
094     * @param expectedException the expected error message
095     * @param archetypeNames the archetype names
096     */
097    public void newAutojoinListsFail(@NotNull final String expectedException, @NotNull final String... archetypeNames) {
098        try {
099            newAutojoinLists(archetypeNames);
100            Assert.fail();
101        } catch (final IllegalAutojoinListException ex) {
102            Assert.assertEquals(expectedException, ex.getMessage());
103        }
104    }
105
106}