Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions code/src/java/pcgen/cdom/enumeration/DisplayLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collections;
import java.util.Objects;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import pcgen.base.enumeration.TypeSafeConstant;
import pcgen.base.util.CaseInsensitiveMap;

Expand Down Expand Up @@ -161,15 +162,11 @@ public static void clearConstants()
}

@Override
@SuppressFBWarnings(value = "EQ_COMPARETO_USE_OBJECT_EQUALS",
justification = "Singleton-by-construction via getConstant intern map; "
+ "no .equals() callers, not used as a collection key. Identity equality is correct here.")
public int compareTo(DisplayLocation type)
{
/*
* Note: Some tools will report a problem here because Type implements
* compareTo, but does not implement custom implementations of hashCode
* or equals(). Because this is intended as a TypeSafeConstant, and Type
* has a private constructor, it is unnecessary to implement a custom
* hashCode or equals.
*/
return fieldName.compareTo(type.fieldName);
}

Expand Down
38 changes: 25 additions & 13 deletions code/src/java/pcgen/cdom/enumeration/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Objects;

import pcgen.base.enumeration.TypeSafeConstant;
Expand All @@ -28,13 +29,23 @@
import pcgen.cdom.base.Constants;

/**
*
* This Class is a Type Safe Constant. It is designed to hold Regions in a
* type-safe fashion, so that they can be quickly compared and use less memory
* when identical Regions exist in two CDOMObjects.
*
* <p>{@code equals} and {@code hashCode} are case-insensitive on
* {@code fieldName} to match {@link #compareTo} (Comparable contract) and to
* support use as a {@code HashMap} key — see {@code BioSet}'s
* {@code DoubleKeyMap<Optional<Region>, …>} and the {@code .equals()} calls
* in {@code RegionFacet.matchesRegion}.</p>
*/
public final class Region implements TypeSafeConstant, Comparable<Region>
{
/**
* This is used to provide a unique ordinal to each constant in this class
*/
private static int ordinalCount = 0;

/**
* A "None" region for universal use.
*/
Expand All @@ -45,11 +56,6 @@ public final class Region implements TypeSafeConstant, Comparable<Region>
*/
private static CaseInsensitiveMap<Region> typeMap;

/**
* This is used to provide a unique ordinal to each constant in this class
*/
private static int ordinalCount = 0;

/**
* The name of this Constant
*/
Expand Down Expand Up @@ -202,14 +208,20 @@ public static void clearConstants()
@Override
public int compareTo(Region region)
{
/*
* Note: Some tools will report a problem here because Region implements
* compareTo, but does not implement custom implementations of hashCode
* or equals(). Because this is intended as a TypeSafeConstant, and Type
* has a private constructor, it is unnecessary to implement a custom
* hashCode or equals.
*/
return fieldName.compareToIgnoreCase(region.fieldName);
}

@Override
public boolean equals(Object o)
{
return o instanceof Region other
&& String.CASE_INSENSITIVE_ORDER.compare(fieldName, other.fieldName) == 0;
}

@Override
public int hashCode()
{
return fieldName.toLowerCase(Locale.ROOT).hashCode();
}

}
26 changes: 18 additions & 8 deletions code/src/java/pcgen/cdom/enumeration/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@
import pcgen.cdom.base.Constants;

/**
*
* This Class is a Type Safe Constant. It is designed to hold Types in a
* type-safe fashion, so that they can be quickly compared and use less memory
* when identical Types exist in two CDOMObjects.
*
* <p>{@code equals} and {@code hashCode} are value-based on {@code fieldName}
* (case-sensitive, matching {@link #compareTo}). Type is used as a
* {@code HashSet}/{@code HashMap} key across the codebase (DataSet, GameMode,
* BodyStructure, spell/skill lists, …); the value-equals contract holds even
* when an instance is constructed outside the {@link #getConstant} intern map.</p>
*/
public final class Type implements TypeSafeConstant, Comparable<Type>
{
Expand Down Expand Up @@ -219,16 +224,21 @@ public static Collection<Type> getAllConstants()
@Override
public int compareTo(Type type)
{
/*
* Note: Some tools will report a problem here because Type implements
* compareTo, but does not implement custom implementations of hashCode
* or equals(). Because this is intended as a TypeSafeConstant, and Type
* has a private constructor, it is unnecessary to implement a custom
* hashCode or equals.
*/
return fieldName.compareTo(type.fieldName);
}

@Override
public boolean equals(Object o)
{
return o instanceof Type other && fieldName.equals(other.fieldName);
}

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

public static void buildMap()
{
TYPE_MAP.clear();
Expand Down
Loading