PageRenderTime 46ms CodeModel.GetById 30ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/parameter/DestinationPointCodeImpl.java

http://mobicents.googlecode.com/
Java | 121 lines | 70 code | 19 blank | 32 comment | 0 complexity | 107c4640715dfc259c2398eb4f245add MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.ss7.m3ua.impl.parameter;
 24
 25import javolution.xml.XMLFormat;
 26import javolution.xml.XMLSerializable;
 27import javolution.xml.stream.XMLStreamException;
 28
 29import org.mobicents.protocols.ss7.m3ua.parameter.DestinationPointCode;
 30import org.mobicents.protocols.ss7.m3ua.parameter.Parameter;
 31
 32/**
 33 * 
 34 * @author amit bhayani
 35 * 
 36 */
 37public class DestinationPointCodeImpl extends ParameterImpl implements DestinationPointCode, XMLSerializable {
 38
 39	private static final String DPC = "dpc";
 40	private static final String MASK = "mask";
 41
 42	private int destPC = 0;
 43	private short mask = 0;
 44	private byte[] value;
 45
 46	public DestinationPointCodeImpl(){
 47		this.tag = Parameter.Destination_Point_Code;
 48	}
 49	protected DestinationPointCodeImpl(byte[] value) {
 50		this.tag = Parameter.Destination_Point_Code;
 51		this.value = value;
 52		this.mask = value[0];
 53
 54		destPC = 0;
 55		destPC |= value[1] & 0xFF;
 56		destPC <<= 8;
 57		destPC |= value[2] & 0xFF;
 58		destPC <<= 8;
 59		destPC |= value[3] & 0xFF;
 60	}
 61
 62	protected DestinationPointCodeImpl(int pc, short mask) {
 63		this.tag = Parameter.Destination_Point_Code;
 64		this.destPC = pc;
 65		this.mask = mask;
 66		encode();
 67	}
 68
 69	private void encode() {
 70		// create byte array taking into account data, point codes and
 71		// indicators;
 72		this.value = new byte[4];
 73		// encode point code with mask
 74		value[0] = (byte) this.mask;// Mask
 75
 76		value[1] = (byte) (destPC >> 16);
 77		value[2] = (byte) (destPC >> 8);
 78		value[3] = (byte) (destPC);
 79	}
 80
 81	public int getPointCode() {
 82		return destPC;
 83	}
 84
 85	@Override
 86	protected byte[] getValue() {
 87		return value;
 88	}
 89
 90	public short getMask() {
 91		return this.mask;
 92	}
 93
 94	@Override
 95	public String toString() {
 96		return String.format("DestinationPointCode dpc=%d mask=%d", destPC, mask);
 97	}
 98
 99	/**
100	 * XML Serialization/Deserialization
101	 */
102	protected static final XMLFormat<DestinationPointCodeImpl> RC_XML = new XMLFormat<DestinationPointCodeImpl>(
103			DestinationPointCodeImpl.class) {
104
105		@Override
106		public void read(javolution.xml.XMLFormat.InputElement xml, DestinationPointCodeImpl dpc)
107				throws XMLStreamException {
108			dpc.destPC = xml.getAttribute(DPC).toInt();
109			dpc.mask = (short) xml.getAttribute(MASK).toInt();
110			dpc.encode();
111		}
112
113		@Override
114		public void write(DestinationPointCodeImpl dpc, javolution.xml.XMLFormat.OutputElement xml)
115				throws XMLStreamException {
116			xml.setAttribute(DPC, dpc.destPC);
117			xml.setAttribute(MASK, dpc.mask);
118		}
119	};
120
121}