Skip to content
Snippets Groups Projects
Commit d1e72b85 authored by J. M. Bailey's avatar J. M. Bailey
Browse files

Fix equals method

parent 30acbbbe
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,6 @@
* Copyright (c) 2008-2019, MOVES Institute, Naval Postgraduate School. All rights reserved.
* This work is licensed under the BSD open source license, available at https://www.movesinstitute.org/licenses/bsd.html
*/
package edu.nps.moves.dis7;
import java.io.DataInputStream;
......@@ -14,9 +13,10 @@ import java.lang.reflect.InvocationTargetException;
import edu.nps.moves.dis7.enumerations.*;
/**
* An example approximating a Java enum "superclass". Can't use an interface, because we need the two
* An example approximating a Java enum "superclass". Can't use an interface, because we need the two
* static methods, which can't go into an interface
*/
//@formatter:off
public class Domain
{
private Domain() { }
......@@ -30,10 +30,8 @@ public class Domain
private Method unmarshalDis;
private Method getValue;
private Method getDescription;
//@formatter:off
//@formatter:off
public static Domain inst(PlatformDomain d) { return _inst(d); } // uid 8
public static Domain inst(MunitionDomain d) { return _inst(d); } // uid 14
......@@ -41,7 +39,6 @@ public class Domain
public static Domain inst(SupplyDomain d) { return _inst(d); } // uid 600
//@formatter:on
private static Domain _inst(Object o)
{
Domain d = new Domain();
......@@ -49,24 +46,26 @@ public class Domain
d.init();
return d;
}
private void init()
{
Class c = enumInst.getClass();
try {
//@formatter:off
marshalBuff = c.getDeclaredMethod("marshal", new Class[]{ByteBuffer.class});
marshalDos = c.getDeclaredMethod("marshal", new Class[]{DataOutputStream.class});
unmarshalBuff = c.getDeclaredMethod("unmarshalEnum", new Class[]{ByteBuffer.class});
unmarshalDis = c.getDeclaredMethod("unmarshalEnum", new Class[]{DataInputStream.class});
mSize = c.getDeclaredMethod("getMarshalledSize", (Class[]) null);
getValue = c.getDeclaredMethod("getValue", (Class[]) null);
getDescription =c.getDeclaredMethod("getDescription", (Class[]) null);
marshalBuff = c.getDeclaredMethod("marshal", new Class[]{ByteBuffer.class});
marshalDos = c.getDeclaredMethod("marshal", new Class[]{DataOutputStream.class});
unmarshalBuff = c.getDeclaredMethod("unmarshalEnum", new Class[]{ByteBuffer.class});
unmarshalDis = c.getDeclaredMethod("unmarshalEnum", new Class[]{DataInputStream.class});
mSize = c.getDeclaredMethod("getMarshalledSize", (Class[]) null);
getValue = c.getDeclaredMethod("getValue", (Class[]) null);
getDescription = c.getDeclaredMethod("getDescription", (Class[]) null);
//@formatter:on
}
catch (NoSuchMethodException ex) {
System.err.println("Can't find methods in "+c.getSimpleName());
}
System.err.println("Can't find methods in " + c.getSimpleName());
}
}
public int getValue()
{
return (Integer) invoke(getValue, (Object[]) null);
......@@ -123,5 +122,36 @@ public class Domain
{
return getDescription();
}
}
/*
* Override of default equals method. Calls equalsImpl() for content comparison.
*/
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
return equalsImpl(obj);
}
/**
* Compare all fields that contribute to the state, ignoring
* transient and static fields, for <code>this</code> and the supplied object
*
* @param obj the object to compare to
* @return true if the objects are equal, false otherwise.
*/
public boolean equalsImpl(Object obj)
{
final Domain rhs = (Domain) obj;
return enumInst.equals(rhs.enumInst);
}
//@formatter:on
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment