Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Minor formatting updates to aling PR #178 with formatting norms
  • Loading branch information
timpokorny committed May 19, 2016
1 parent 7732f89 commit f1a7906
Show file tree
Hide file tree
Showing 3 changed files with 578 additions and 603 deletions.
Expand Up @@ -114,33 +114,27 @@ public void decode( byte[] bytes ) throws DecoderException
this.value = bytes[0];
}

/**
* hashCode is required so that HLA1516eOctet can be used as a key in
* java.util.HashMap, which is used by HLA1516eVariantRecord to store
* discriminant/variant pairs.
*
* @return
*/
@Override
public int hashCode() {
return this.getValue();
}
/**
* hashCode is required so that HLA1516eOctet can be used as a key in java.util.HashMap, which
* is used by HLA1516eVariantRecord to store discriminant/variant pairs.
*/
@Override
public int hashCode()
{
return this.getValue();
}

/**
* equals is required so that HLA1516eOctet can be used as a key in
* java.util.HashMap, which is used by HLA1516eVariantRecord to store
* discriminant/variant pairs.
*
* @return
*/
/**
* equals is required so that HLA1516eOctet can be used as a key in java.util.HashMap, which
* is used by HLA1516eVariantRecord to store discriminant/variant pairs.
*/
@Override
public boolean equals(Object other) {
return (this == other) ||
((other != null) &&
(other instanceof HLA1516eOctet) &&
(this.getValue() == ((HLA1516eOctet)other).getValue())
);
}
public boolean equals( Object other )
{
return (this == other) || ((other != null) &&
(other instanceof HLA1516eOctet) &&
(this.getValue() == ((HLA1516eOctet)other).getValue()));
}

//----------------------------------------------------------
// STATIC METHODS
Expand Down
Expand Up @@ -14,6 +14,9 @@
*/
package org.portico.impl.hla1516e.types.encoding;

import java.util.HashMap;
import java.util.Map;

import hla.rti1516e.encoding.ByteWrapper;
import hla.rti1516e.encoding.DataElement;
import hla.rti1516e.encoding.DecoderException;
Expand All @@ -34,24 +37,24 @@ public class HLA1516eVariantRecord<T extends DataElement>
//----------------------------------------------------------
// INSTANCE VARIABLES
//----------------------------------------------------------
// an invariant of this class probably should be that if
// _discriminant_ is not null then it exists as a key in _variants_.
// It is currently possible to break this invariant at construction
// and through the call to setDiscriminant.
// an invariant of this class probably should be that if
// _discriminant_ is not null then it exists as a key in _variants_.
// It is currently possible to break this invariant at construction
// and through the call to setDiscriminant.
private T discriminant = null;
private java.util.Map<T, DataElement> variants = new java.util.HashMap<>();
private Map<T,DataElement> variants = new HashMap<>();

//----------------------------------------------------------
// CONSTRUCTORS
//----------------------------------------------------------
HLA1516eVariantRecord()
{
}
HLA1516eVariantRecord()
{
}

HLA1516eVariantRecord(T discriminant)
{
this.discriminant = discriminant;
}
HLA1516eVariantRecord( T discriminant )
{
this.discriminant = discriminant;
}

//----------------------------------------------------------
// INSTANCE METHODS
Expand Down Expand Up @@ -95,11 +98,10 @@ public T getDiscriminant()
*/
public DataElement getValue()
{
if (!this.variants.containsKey(this.discriminant)) {
return null;
}
if( !this.variants.containsKey(this.discriminant) )
return null;

return this.variants.get(this.discriminant);
return this.variants.get( this.discriminant );
}

/////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -108,124 +110,127 @@ public DataElement getValue()
@Override
public int getOctetBoundary()
{
if (this.discriminant == null) {
return 1;
}

// the octet boundary of a HLAvariantRecord is the maximum of the octet boundary
// of the discriminant and each of the possible alternatives. See section 4.13.9.2
// of IEEE Std 1516-2010.2.
int maxOctetBoundary = 1;
for (DataElement variant: this.variants.values()) {
maxOctetBoundary = Math.max(maxOctetBoundary, variant.getOctetBoundary());
}

return maxOctetBoundary;
if (this.discriminant == null)
return 1;

// the octet boundary of a HLAvariantRecord is the maximum of the octet boundary
// of the discriminant and each of the possible alternatives. See section 4.13.9.2
// of IEEE Std 1516-2010.2.
int maxOctetBoundary = 1;
for( DataElement variant : this.variants.values() )
{
maxOctetBoundary = Math.max( maxOctetBoundary, variant.getOctetBoundary() );
}

return maxOctetBoundary;
}

@Override
public void encode( ByteWrapper byteWrapper ) throws EncoderException
{
if (this.discriminant == null) {
throw new EncoderException("No discriminant set in HLAvariantRecord");
}

if (this.getEncodedLength() > byteWrapper.remaining()) {
throw new EncoderException("Not enought space in ByteWrapper to encode HLAvariantRecord");
}

this.discriminant.encode(byteWrapper);
// ignoring padding after the discriminant for now

if (this.variants.containsKey(this.discriminant)) {
// encode the variant if the discriminant exists ...
DataElement variant = this.variants.get(this.discriminant);
if (variant != null) {
// ... and has a value assigned.
this.variants.get(this.discriminant).encode(byteWrapper);
}
} else {
throw new EncoderException("Discriminant is unknown to this HLAvariantRecord");
}
if( this.discriminant == null )
throw new EncoderException( "No discriminant set in HLAvariantRecord" );

if( this.getEncodedLength() > byteWrapper.remaining() )
throw new EncoderException( "Not enought space in ByteWrapper to encode HLAvariantRecord" );

this.discriminant.encode( byteWrapper );
// ignoring padding after the discriminant for now

if( this.variants.containsKey( this.discriminant ) )
{
// encode the variant if the discriminant exists ...
DataElement variant = this.variants.get( this.discriminant );
if( variant != null )
{
// ... and has a value assigned.
this.variants.get( this.discriminant ).encode( byteWrapper );
}
}
else
{
throw new EncoderException( "Discriminant is unknown to this HLAvariantRecord" );
}
}

@Override
public int getEncodedLength()
{
if (this.discriminant == null) {
return 0;
}

if (!this.variants.containsKey(this.discriminant)) {
return 0;
}

// the encoded length is the encoded length of the discriminant ...
int encodedLength = this.discriminant.getEncodedLength();

// ... plus any padding that comes after the discriminant (ignore for now) ...

if (this.variants.containsKey(this.discriminant)) {
DataElement variant = this.variants.get(this.discriminant);
if (variant != null) {
// ... plus the encoded length of the variant if the discriminant
// exists and has an assigned variant.
encodedLength += this.variants.get(this.discriminant).getEncodedLength();
}
}

return encodedLength;
if( this.discriminant == null )
return 0;

if( !this.variants.containsKey( this.discriminant ) )
return 0;

// the encoded length is the encoded length of the discriminant ...
int encodedLength = this.discriminant.getEncodedLength();

// ... plus any padding that comes after the discriminant (ignore for now) ...
if( this.variants.containsKey( this.discriminant ) )
{
DataElement variant = this.variants.get( this.discriminant );
if( variant != null )
{
// ... plus the encoded length of the variant if the discriminant
// exists and has an assigned variant.
encodedLength += this.variants.get(this.discriminant).getEncodedLength();
}
}

return encodedLength;
}

@Override
public byte[] toByteArray() throws EncoderException
{
ByteWrapper byteWrapper = new ByteWrapper(this.getEncodedLength());
this.encode(byteWrapper);
ByteWrapper byteWrapper = new ByteWrapper( this.getEncodedLength() );
this.encode( byteWrapper );

return byteWrapper.array();
}

@Override
public void decode( ByteWrapper byteWrapper ) throws DecoderException
{
if (this.discriminant == null) {
throw new DecoderException("No space to decode the discriminant");
}

if (this.discriminant.getEncodedLength() > byteWrapper.remaining()) {
throw new DecoderException("Not enough data in ByteWrapper to decode a discriminant");
}

this.discriminant.decode(byteWrapper);

// consume padding (ignored for now)

if (this.variants.containsKey(this.discriminant)) {
// if the decoded discriminant is known ...
DataElement variant = this.variants.get(this.discriminant);

if (variant == null) {
// we're done decoding, there is no variant assigned to this discriminant
return;
}

if (variant.getEncodedLength() > byteWrapper.remaining()) {
throw new DecoderException("Not enough data in ByteWrapper to decode variant associated with discriminant.");
}
// variant is not null and there is enough data left in the byte wrapper, decode the variant.
variant.decode(byteWrapper);
} else {
throw new DecoderException("Decoded discriminant is unknown to this HLAvariantRecord");
}
if( this.discriminant == null )
throw new DecoderException( "No space to decode the discriminant" );

if( this.discriminant.getEncodedLength() > byteWrapper.remaining() )
throw new DecoderException( "Not enough data in ByteWrapper to decode a discriminant" );

this.discriminant.decode( byteWrapper );

// consume padding (ignored for now)

if( this.variants.containsKey( this.discriminant ) )
{
// if the decoded discriminant is known ...
DataElement variant = this.variants.get( this.discriminant );

if( variant == null )
{
// we're done decoding, there is no variant assigned to this discriminant
return;
}

if( variant.getEncodedLength() > byteWrapper.remaining() )
throw new DecoderException( "Not enough data in ByteWrapper to decode variant associated with discriminant." );

// variant not null and enough data left in the byte wrapper, decode the variant.
variant.decode( byteWrapper );
}
else
{
throw new DecoderException( "Decoded discriminant is unknown to this HLAvariantRecord" );
}
}

@Override
public void decode( byte[] bytes ) throws DecoderException
{
ByteWrapper byteWrapper = new ByteWrapper(bytes);
ByteWrapper byteWrapper = new ByteWrapper( bytes );

this.decode(byteWrapper);
this.decode( byteWrapper );
}

//----------------------------------------------------------
Expand Down

0 comments on commit f1a7906

Please sign in to comment.