code:
package com.lilithsthrone.game.inventory;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.lilithsthrone.game.character.attributes.Attribute;
import com.lilithsthrone.game.inventory.enchanting.AbstractItemEffectType;
import com.lilithsthrone.game.inventory.enchanting.ItemEffect;
import com.lilithsthrone.game.inventory.enchanting.TFEssence;
import com.lilithsthrone.game.inventory.enchanting.TFModifier;
import com.lilithsthrone.utils.Colour;
import com.lilithsthrone.utils.XMLSaving;

/**
 * @since 0.1.0
 * @version 0.3.4
 * @author Innoxia
 */
public abstract class AbstractCoreItem implements XMLSaving {

	protected String name;
	protected String namePlural;
	protected String SVGString;
	protected Colour colourShade;
	protected Rarity rarity;

	protected Map<Attribute, Integer> attributeModifiers;
	protected TFEssence relatedEssence;

	protected Set<ItemTag> itemTags;

	public AbstractCoreItem(String name,
			String namePlural,
			String SVGString,
			Colour colour,
			Rarity rarity,
			Map<Attribute, Integer> attributeModifiers) {
		this(name,
				namePlural,
				SVGString,
				colour,
				rarity,
				attributeModifiers,
				new HashSet<>());
	}

	public AbstractCoreItem(String name,
			String namePlural,
			String SVGString,
			Colour colour,
			Rarity rarity,
			Map<Attribute, Integer> attributeModifiers,
			Set<ItemTag> itemTags) {
		super();
		this.name = name;
		this.namePlural = namePlural;
		this.colourShade = colour;
		this.rarity = rarity;
		this.SVGString = SVGString;

		this.attributeModifiers = new EnumMap<>(Attribute.class);
		this.itemTags = new HashSet<>();

		relatedEssence = null;

		if (attributeModifiers != null) {
			for (Entry<Attribute, Integer> e : attributeModifiers.entrySet()) {
				this.attributeModifiers.put(e.getKey(), e.getValue());
			}
		}

		if(itemTags != null) {
			this.itemTags.addAll(itemTags);
		}
	}

	public Element saveAsXML(Element parentElement, Document doc) {
		System.err.print("Eek! Tried to export an abstract item!");
		return null;
	}

	public static AbstractCoreItem loadFromXML(Element parentElement, Document doc) {
		System.err.print("Eek! Tried to import an abstract item!");
		return null;
	}

	// Enchantments:

	public boolean isAbleToBeEnchanted() {
		return getEnchantmentEffect() != null
				&& getEnchantmentItemType(null) != null;
	}

	public int getEnchantmentLimit() {
		return 100;
	}

	public AbstractItemEffectType getEnchantmentEffect() {
		return null;
	}

	public AbstractCoreType getEnchantmentItemType(List<ItemEffect> effects) {
		return null;
	}

	public AbstractCoreItem enchant(TFEssence essence, TFModifier primaryModifier, TFModifier secondaryModifier) {
		return this;
	}

	public TFEssence getRelatedEssence() {
		return relatedEssence;
	}
	public void setRelatedEssence(TFEssence relatedEssence) {
		this.relatedEssence = relatedEssence;
	}

	// Other:

	@Override
	public boolean equals(Object o) {
		if(o instanceof AbstractCoreItem){
			if(((AbstractCoreItem)o).getName().equals(this.getName())
				&& ((AbstractCoreItem)o).getColour() == this.getColour()
				&& ((AbstractCoreItem)o).getRarity() == this.getRarity()
				&& ((AbstractCoreItem)o).getAttributeModifiers().equals(this.getAttributeModifiers())
				&& ((AbstractCoreItem)o).getEnchantmentEffect() == getEnchantmentEffect()
				&& ((AbstractCoreItem)o).getEnchantmentItemType(null) == getEnchantmentItemType(null)
				&& ((AbstractCoreItem)o).getRelatedEssence() == getRelatedEssence()
				&& ((AbstractCoreItem)o).getItemTags().equals(getItemTags())){
					return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode() {
		int result = 17;
		result = 31 * result + this.getName().hashCode();
		result = 31 * result + this.getColour().hashCode();
		result = 31 * result + this.getRarity().hashCode();
		result = 31 * result + this.getAttributeModifiers().hashCode();
		if(getEnchantmentEffect()!=null) {
			result = 31 * result + getEnchantmentEffect().hashCode();
		}
		if(getEnchantmentItemType(null)!=null) {
			result = 31 * result + getEnchantmentItemType(null).hashCode();
		}
		if(getRelatedEssence()!=null) {
			result = 31 * result + getRelatedEssence().hashCode();
		}
		if(getItemTags()!=null) {
			result = 31 * result + getItemTags().hashCode();
		}
		return result;
	}

	public String getName() {
		return name;
	}

	public String getNamePlural() {
		return namePlural;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSVGString() {
		return SVGString;
	}

	public void setSVGString(String SVGString) {
		this.SVGString = SVGString;
	}

	public abstract String getDescription();

	public abstract int getValue();

	public int getPrice(float modifier) {
		return (int) (getValue() * modifier);
	}

	public void setRarity(Rarity rarity) {
		this.rarity = rarity;
	}

	public Rarity getRarity() {
		return rarity;
	}

	public Colour getColour() {
		return colourShade;
	}

	public void setColour(Colour Colour) {
		this.colourShade = Colour;
	}

	public Map<Attribute, Integer> getAttributeModifiers() {
		return attributeModifiers;
	}

	public void setAttributeModifiers(Map<Attribute, Integer> attributeModifiers) {
		this.attributeModifiers = attributeModifiers;
	}

	public List<ItemEffect> getEffects() {
		return new ArrayList<ItemEffect>();
	}

	public Set<ItemTag> getItemTags() {
		return itemTags;
	}
}


code:
package com.lilithsthrone.game.inventory;

/**
 * @since 0.1.0
 * @version 0.2.0
 * @author Innoxia
 */
public class AbstractCoreType {

	@Override
	public boolean equals(Object o) {
		return true;
	}

	@Override
	public int hashCode() {
		return 1;
	}

	public Rarity getRarity() {
		return Rarity.COMMON;
	}

}

repo_name: klorpa/Pedo

license: unknown


code:
package com.lilithsthrone.game.inventory;

import java.util.*;
import java.util.function.Function;

/**
 * AbstractInventory serves to deduplicate code between the handling of Item, Clothing and Weapon in CharacterInventory.
 *
 * Package-privateness is a feature in that case.
 * @param <T>
 */
class AbstractInventory<T extends AbstractCoreItem, U extends AbstractCoreType> {
	private final Comparator<T> comparator;
	private final Function<T, U> typeRetriever;
	private Map<T, Integer> duplicateCounts;

	AbstractInventory(Comparator<T> comparator, Function<T, U> typeRetriever) {
		this.comparator = comparator;
		this.typeRetriever = typeRetriever;
		duplicateCounts = new LinkedHashMap<>();
	}

	public void clear() {
		duplicateCounts.clear();
	}

	boolean isEmpty() {
		return duplicateCounts.isEmpty();
	}

	public void sort() {
		if (duplicateCounts.size() < 2) {
			return;
		}
		List<T> itemsToSort = new ArrayList<>(duplicateCounts.keySet());
		itemsToSort.sort(comparator);

		Map<T, Integer> newlySortedMap = new LinkedHashMap<>();
		for(T item : itemsToSort) {
			newlySortedMap.put(item, duplicateCounts.get(item));
		}
		duplicateCounts = newlySortedMap;
	}

	public void transform(Function<T, T> transformFunction) {
		Map<T, Integer> oldMap = new LinkedHashMap<>(duplicateCounts);
		duplicateCounts.clear();

		for(Map.Entry<T, Integer> e : oldMap.entrySet()) {
			T oldItem = e.getKey();
			T newItem = transformFunction.apply(oldItem);
			addItem(newItem, e.getValue());
		}
	}

	/**
	 * @return a Non-modifiable Map
	 */
	Map<T, Integer> getDuplicateCounts() {
		return Collections.unmodifiableMap(duplicateCounts);
	}

	int getQuestEntryCount() {
		return (int) duplicateCounts.keySet().stream().filter(e -> e.getRarity().equals(Rarity.QUEST)).count();
	}

	public int getTotalItemCount() {
		return duplicateCounts.values().stream().mapToInt(e -> e).sum();
	}

	public int getItemCount(T item) {
		return duplicateCounts.getOrDefault(item, 0);
	}

	void addFromMap(Map<T, Integer> map) {
		map.forEach(this::addItem);
	}

	void addItem(T item, int count) {
		duplicateCounts.merge(item, count, Integer::sum);
	}

	boolean hasItem(T item) {
		return duplicateCounts.containsKey(item);
	}

	private boolean removeItem(T item) {
		return removeItem(item, 1);
	}

	boolean removeItem(T item, int count) {
		boolean hasItem = hasItem(item);
		if (hasItem) {
			int newValue = duplicateCounts.get(item) - count;
			if (newValue <= 0) {
				duplicateCounts.remove(item);
			} else {
				duplicateCounts.put(item, newValue);
			}
		}
		return hasItem;
	}

	private Optional<T> getItemByType(U type) {
		return duplicateCounts.keySet().stream().filter(item -> typeRetriever.apply(item).equals(type)).findAny();
	}

	private Optional<T> getItemByRarity(Rarity rarity) {
		return duplicateCounts.keySet().stream().filter(item -> typeRetriever.apply(item).getRarity().equals(rarity)).findAny();
	}

	boolean hasItemType(U itemType) {
		return getItemByType(itemType).isPresent();
	}

	boolean removeAllItemsByRarity(Rarity rarity) {
		boolean removed = getItemByRarity(rarity).map(this::removeItem).orElse(false);
		while(removed) {
			removed = getItemByRarity(rarity).map(this::removeItem).orElse(false);
		}
		return removed;
	}

	boolean removeItemByType(U itemType) {
		return getItemByType(itemType).map(this::removeItem).orElse(false);
	}
}