All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softwaremagico.tm.character.planets.Planet Maven / Gradle / Ivy

package com.softwaremagico.tm.character.planets;

/*-
 * #%L
 * Think Machine 4E (Rules)
 * %%
 * Copyright (C) 2017 - 2024 Softwaremagico
 * %%
 * This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
 *  Valencia (Spain).
 *
 * This program 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 2 of the License, or (at your option) any later
 * version.
 *
 * This program 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
 * this program; If not, see .
 * #L%
 */


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.softwaremagico.tm.Element;
import com.softwaremagico.tm.exceptions.InvalidXmlElementException;
import com.softwaremagico.tm.TranslatedText;
import com.softwaremagico.tm.character.Name;
import com.softwaremagico.tm.character.Surname;
import com.softwaremagico.tm.character.factions.Faction;
import com.softwaremagico.tm.character.factions.FactionFactory;
import com.softwaremagico.tm.character.factions.random.RandomFactionFactory;
import com.softwaremagico.tm.log.MachineXmlReaderLog;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

@JacksonXmlRootElement(localName = "planet")
public class Planet extends Element {
    @JsonProperty("factions")
    private String factionData;
    @JsonIgnore
    private Set factions;
    private Set humanFactions;
    private Set names;
    private Set surnames;

    public Planet() {
        super();
    }

    public Planet(String id, TranslatedText name, TranslatedText description, String language, String moduleName, Set factions) {
        super(id, name, description, language, moduleName);
        this.factions = factions;
    }

    protected String getFactionData() {
        return factionData;
    }

    protected void setFactionData(String factionData) {
        this.factionData = factionData;
    }

    public void setFactions(Set factions) {
        this.factions = factions;
    }

    public Set getFactions() {
        if (factions == null) {
            factions = new HashSet<>();
            readCommaSeparatedTokens(factions, factionData);
        }
        return factions;
    }

    public Set getHumanFactions() {
        // Get only human factions from the planet. Ignore Xeno factions.
        if (humanFactions == null) {
            humanFactions = new HashSet<>();
            try {
                this.humanFactions = FactionFactory.getInstance().getElements(factions).stream().filter(Faction::isOnlyForHuman).
                        collect(Collectors.toSet());
            } catch (InvalidXmlElementException e) {
                MachineXmlReaderLog.errorMessage(this.getClass(), e);
            }
        }
        return humanFactions;
    }

    public Set getNames() {
        if (names == null) {
            names = new HashSet<>();
            for (final Faction faction : getHumanFactions()) {
                try {
                    if (RandomFactionFactory.getInstance().getElement(faction.getId()) != null) {
                        names.addAll(RandomFactionFactory.getInstance().getElement(faction.getId()).getAllNames());
                    }
                } catch (InvalidXmlElementException e) {
                    MachineXmlReaderLog.errorMessage(this.getName(), e);
                }
            }
        }
        return names;
    }

    public Set getSurnames() {
        if (surnames == null) {
            surnames = new HashSet<>();
            for (final Faction faction : getHumanFactions()) {
                try {
                    if (RandomFactionFactory.getInstance().getElement(faction.getId()) != null) {
                        surnames.addAll(RandomFactionFactory.getInstance().getElement(faction.getId()).getSurnames());
                    }
                } catch (InvalidXmlElementException e) {
                    MachineXmlReaderLog.errorMessage(this.getName(), e);
                }
            }
        }
        return surnames;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy