AbilityInfo.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
pcgen.core |
![]() |
![]() |
PCGen |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* AbilityInfo.java
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Current Version: $Revision: 6423 $
* Last Editor: $Author: jdempsey $
* Last Edited: $Date: 2008-06-01 01:20:59 -0400 (Sun, 01 Jun 2008) $
* Copyright 2005 Andrew Wilson <nuance@sourceforge.net>
*/
package pcgen.core;
import pcgen.core.prereq.PrereqHandler;
import pcgen.core.prereq.Prerequisite;
import pcgen.persistence.PersistenceLayerException;
import pcgen.persistence.lst.prereq.PreParserFactory;
import pcgen.util.Logging;
import java.util.*;
/**
* This tiny little class replaces a simple string representation of an Ability.
* Since the move to Abilities, we can no longer look up these up based solely
* on name, we now also need category. This also allows for a set of
* Prerequisites to be associated with this AbilityInfo Object and can check
* whether a given PC qualifies.
*
* @author Andrew Wilson <nuance@sourceforge.net>
*/
public class AbilityInfo implements Comparable<Object>, Categorisable
{
protected String keyName;
protected String category;
private Ability realThing;
private List<Prerequisite> prereqList;
private List<String> decorations;
protected char delim = '<';
private static final String split1 = "[<>\\|]";
private static final String split2 = "[\\[\\]\\|]";
/**
* Make a new object to hold minimal info about Abilities
*
* @param category
* the Ability's category
* @param key
* the Key of the Ability
*/
public AbilityInfo(final String category, final String key)
{
super();
this.category = category;
this.extractPrereqs(key);
}
/**
* Creates a new AbilityInfo object.
*/
public AbilityInfo()
{
super();
this.category = "";
this.keyName = "";
}
/**
* Get the Ability Object that this was a proxy for
*
* @return Returns the Ability.
*/
public Ability getAbility()
{
if (realThing == null)
{
realThing = AbilityUtilities.retrieveAbilityKeyed(this.category,
this.keyName);
decorations = new ArrayList<String>();
if ((realThing != null)
&& (!realThing.getKeyName().equals(this.keyName)))
{
// get the decorations, throw away the name (because we already
// have it in keyname)
AbilityUtilities.getUndecoratedName(this.keyName, decorations);
}
}
return realThing;
}
/**
* Return an iterator over any Choices made for the Ability represented
*
* @return an iterator
*/
public Iterator<String> getChoicesIterator()
{
final List<String> returnList;
if (getAbility() != null)
{
returnList = decorations;
}
else
{
returnList = Collections.emptyList();
}
return returnList.iterator();
}
/**
* Get the category of the Ability this AbilityInfo object represents
*
* @return Returns the category.
*/
public final String getCategory()
{
return category;
}
/**
* Get the key of the Ability this AbilityInfo object represents
*
* @return Returns the key.
*/
public final String getKeyName()
{
return keyName;
}
/**
* (non-Javadoc)
*
* @return a String representation of this AbilityInfo
*
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return this.keyName;
}
/**
* Extract the key and any prerequisites that this Ability has, store them
* in the object's fields
*
* @param unparsed the name of the Ability with any prereqs
*/
protected void extractPrereqs(final String unparsed)
{
final int start = unparsed.indexOf(delim);
if ((start < 0))
{
// no Prereqs, assign directly to key field
this.keyName = unparsed;
}
else
{
if (prereqList == null)
{
prereqList = new ArrayList<Prerequisite>();
}
final List<String> tokens = Arrays.asList(unparsed.split(delim == '<' ? split1 : split2));
final Iterator<String> tokIt = tokens.iterator();
// extract and assign the choice from the unparsed string
this.keyName = tokIt.next();
try
{
final PreParserFactory factory = PreParserFactory.getInstance();
for (; tokIt.hasNext();)
{
final Prerequisite prereq = factory.parse(tokIt.next());
if (prereq != null)
{
prereqList.add(prereq);
}
}
}
catch (PersistenceLayerException e)
{
Logging.errorPrint("Failed to parse prereq of " + keyName
+ " in category " + category + " due to :", e);
Logging.errorPrint("Ignoring further prereqs for this object.");
}
}
}
/**
* Does the PC qualify to take this Ability
*
* @param pc
* The Player Character to test the prerequisites against.
*
* @return whether the PC qualifies
*/
public boolean qualifies(final PlayerCharacter pc)
{
return prereqList == null || PrereqHandler.passesAll(prereqList, pc, this.getAbility());
}
/**
* Compares this AbilityInfo Object with an Object passed in. The object
* passed in should be either an AbilityInfo Object or a PObject.
*
* @param obj
* the object to test against
*
* @return the result of the compare, negative integer if this should sort
* before
*/
public int compareTo(final Object obj)
{
try
{
final String otherCat = ((Categorisable) obj).getCategory();
if (otherCat.compareTo(this.category) != 0)
{
return otherCat.compareTo(this.getCategory());
}
return ((Categorisable) obj).getKeyName().compareTo(this.getKeyName());
}
catch (ClassCastException e)
{
try
{
final Ability ab = (Ability) obj;
return this.getAbility().compareTo(ab);
}
catch (ClassCastException ex)
{
// If this can't be converted to a PObject then they aren't
// comparable and
// Should throw the ClassCastException
PObject pObj = (PObject) obj;
return this.keyName.compareTo(pObj.getKeyName());
}
}
}
/**
* this is only here so that this implements all the methods of
* Categorisable
*
* @return the name of the object
*/
public String getDisplayName()
{
return this.getKeyName();
}
}
The table below shows all metrics for AbilityInfo.java.




