com.crankuptheamps.client.fields.OptionsField Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of amps-client Show documentation
Show all versions of amps-client Show documentation
AMPS Java client by 60East Technologies, Inc.
////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2010-2020 60East Technologies Inc., All Rights Reserved.
//
// This computer software is owned by 60East Technologies Inc. and is
// protected by U.S. copyright laws and other laws and by international
// treaties. This computer software is furnished by 60East Technologies
// Inc. pursuant to a written license agreement and may be used, copied,
// transmitted, and stored only in accordance with the terms of such
// license agreement and with the inclusion of the above copyright notice.
// This computer software or any other copies thereof may not be provided
// or otherwise made available to any other person.
//
// U.S. Government Restricted Rights. This computer software: (a) was
// developed at private expense and is in all respects the proprietary
// information of 60East Technologies Inc.; (b) was not developed with
// government funds; (c) is a trade secret of 60East Technologies Inc.
// for all purposes of the Freedom of Information Act; and (d) is a
// commercial item and thus, pursuant to Section 12.212 of the Federal
// Acquisition Regulations (FAR) and DFAR Supplement Section 227.7202,
// Government's use, duplication or disclosure of the computer software
// is subject to the restrictions set forth by 60East Technologies Inc..
//
////////////////////////////////////////////////////////////////////////////
package com.crankuptheamps.client.fields;
import java.io.UnsupportedEncodingException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
/**
* Field data for a {@link com.crankuptheamps.client.Message} which consists of options.
* These options represent the possible values for message options in an
* AMPS command.
*/
public class OptionsField extends StringField
{
protected OptionsField(byte[] buffer, int position, int length)
{
super(buffer, position, length);
if(this.buffer != null)
{
int len = this.length;
if (len > 0 && buffer[this.position+len-1] == ',') --this.length;
}
}
public OptionsField(String value)
{
super(value);
}
public OptionsField()
{
}
@Override
public void setValue(byte[] v,int offset,int length)
{
super.setValue(v, offset, length);
if(this.buffer != null)
{
int len = this.length;
if (len > 0 && buffer[this.position+len-1] == ',') --this.length;
}
}
@Override
public void setValue(String v, CharsetEncoder encoder)
{
super.setValue(v, encoder);
if(this.buffer != null)
{
int len = this.length;
if (len > 0 && buffer[this.position+len-1] == ',') --this.length;
}
}
@Override
public String getValue(CharsetDecoder decoder)
{
if(this.buffer != null)
{
int len = this.length;
if (len > 0 && buffer[this.position+len-1] == ',') --this.length;
return new String(this.buffer,this.position,this.length,decoder.charset());
}
return null;
}
@Override
public boolean getValue(ByteBuffer v)
{
if(this.buffer != null)
{
int len = this.length;
if (len > 0 && buffer[this.position+len-1] == ',') --this.length;
v.put(this.buffer,this.position,this.length);
return true;
}
return false;
}
/**
* Implementation for {@link Object#equals} for an OptionsField. For two OptionsFields to be
* considered equal, their values must have the same length and all bytes in the
* value must be equal, other than a possible trailing comma.
* @param obj The object to compare this with.
*/
public boolean equals(Object obj)
{
if(obj == null || !(obj instanceof OptionsField))
{
if (obj instanceof Field) return super.equals(obj);
return false;
}
int len = this.length;
if (len > 0 && buffer[this.position+len-1] == ',') --len;
OptionsField o = (OptionsField)obj;
int oLen = o.length;
if (oLen > 0 && o.buffer[o.position+oLen-1] == ',') --oLen;
if(oLen != len) return false;
for(int i=0; i< len; i++)
{
if(o.buffer[o.position+i] != buffer[position+i]) return false;
}
return true;
}
/**
* Implementation of {@link Object#hashCode} for an OptionsField.
*/
public int hashCode()
{
// for large sets of short, similar byte sequences this can produce lots of collision
int result = 0;
int len = this.length;
if (len > 0 && buffer[this.position+len-1] == ',') --len;
for(int i = 0; i < len; i++)
{
result += buffer[position+i];
}
return result;
}
}