📄 mapcompositiondata.java
字号:
package com.esri.solutions.jitk.services.personalization.data;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.esri.solutions.jitk.personalization.data.beans.v1.MapCompositionType;
import com.esri.solutions.jitk.services.common.ServicesException;
/**
* Serializable Java Bean representation of a Map Composition.
*/
public class MapCompositionData extends PersonalizationData implements java.io.Serializable {
private static final long serialVersionUID = 1L;
/**
* {@code byte[]} representation of the actual item
*/
private byte[] _data;
/**
* {@link Object} used by the {@link #equals(Object)} method to improve the
* performance of the calculations.
*/
private Object _equalsCalc = null;
/**
* {@code boolean} used by the {@link #hashCode()} method to improve the
* performance of the hash algorithm.
*/
private boolean _hashCodeCalc = false;
/**
* {@link MapCompositionType}
*/
private MapCompositionType _mapComposition = null;
private static final String ERROR_DATA_DOES_NOT_REPRESENT_A_MAP_COMPOSITION = "The byte array provided does not unmarshall to a Map Composition.";
/**
* Default constructor
*/
public MapCompositionData() {
}
/**
* @return The xml data for a MapComposition Record as a byte array.
*/
public byte[] getData() {
return _data;
}
/**
* Sets the Map Composition information, in the form of a {@code byte[]}.
* <p>
* The {@code byte[]} parameter must unmarshall into a valid {@link MapCompositionType}. If the parameter is {@code null},
* then the property in which {@code data} is stored, as well as the internal {@link MapCompositionType} representation, will both
* be set to {@code null}, thus causing {@code null} to be returned from {@link #dataAsMapComposition()}.
* </p>
* <p>
* If {@code data} is not {@code null}, this method will unmarshall the byte array into a {@link MapCompositionType} that
* can be retrieved from {@link #dataAsMapComposition()}.
* </p>
* @param data {@code byte[]} representation of a {@link MapCompositionType}. The parameter can be {@code null}
* or of zero length, but if it is not, it must unmarshall to a {@link MapCompositionType}.
* @throws ServicesException If {@code data} does not unmarshall to a valid {@link MapCompositionType}.
*/
public void setData(byte[] data) throws ServicesException {
MapCompositionType t = null;
if (data != null && data.length>0 ) {
try {
t = unmarshall(data);
/*
* t will be null if the unmarshall was successful but the data
* could not be converted to a MapCompositionType
*/
if (t == null) {
throw new ServicesException(
ERROR_DATA_DOES_NOT_REPRESENT_A_MAP_COMPOSITION);
}
_mapComposition = t;
} catch (JAXBException e) {
throw new ServicesException(
ERROR_DATA_DOES_NOT_REPRESENT_A_MAP_COMPOSITION);
}
}
/*
* Set both the map composition and the data array, that way if one is
* nulled out, the other will be as well.
*/
_mapComposition = t;
_data = data;
}
/**
* Retrieves the Map Compostion, set through a call to {@link #setData(byte[])}, in the form of a
* {@link MapCompositionType} bean.
* <p>
* The {@link MapCompositionType} returned by this method will always reflect the most-recently-set
* {@code byte[]}. See {@link #setData(byte[])} for more information
* @return {@link MapCompositionType}
* @see #setData(byte[])
*/
public MapCompositionType dataAsMapComposition() {
return _mapComposition;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public synchronized boolean equals(Object obj) {
if (!(obj instanceof MapCompositionData))
return false;
MapCompositionData other = (MapCompositionData) obj;
if (obj == null)
return false;
if (this == obj)
return true;
if (_equalsCalc != null) {
return (_equalsCalc == obj);
}
_equalsCalc = obj;
boolean _equals;
_equals = true
&& ((_creator == null && other.getCreator() == null) || (_creator != null && _creator
.equals(other.getCreator())))
&& ((_mapComposition == null && other.dataAsMapComposition() == null) || (_mapComposition != null && _mapComposition
.equals(other.dataAsMapComposition())));
_equalsCalc = null;
return _equals;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
public synchronized int hashCode() {
if (_hashCodeCalc) {
return 0;
}
_hashCodeCalc = true;
int _hashCode = 1;
if (getCreator() != null) {
_hashCode += getCreator().hashCode();
}
if (dataAsMapComposition() != null) {
_hashCode += dataAsMapComposition().hashCode();
}
_hashCodeCalc = false;
return _hashCode;
}
/**
* Unmarshalls a {@code byte[]} into a {@link MapCompositionType}
*
* @param data
* @throws JAXBException
*/
private MapCompositionType unmarshall(byte[] data) throws JAXBException {
JAXBContext ctx = null;
ctx = JAXBContext.newInstance(JAXB_BEAN_PACKAGE_NAME);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
Object o = unmarshaller.unmarshal(new ByteArrayInputStream(data));
MapCompositionType t = null;
if (o instanceof MapCompositionType) {
t = (MapCompositionType) o;
}
return t;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -