PageRenderTime 57ms CodeModel.GetById 23ms app.highlight 27ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 153 lines | 92 code | 29 blank | 32 comment | 6 complexity | c45cedf38c7269c9c77317b7b2087ee7 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 java.util.Arrays;
 26
 27import javolution.xml.XMLFormat;
 28import javolution.xml.XMLSerializable;
 29import javolution.xml.stream.XMLStreamException;
 30
 31import org.mobicents.protocols.ss7.m3ua.parameter.OPCList;
 32import org.mobicents.protocols.ss7.m3ua.parameter.Parameter;
 33
 34/**
 35 * 
 36 * @author amit bhayani
 37 * 
 38 */
 39public class OPCListImpl extends ParameterImpl implements OPCList, XMLSerializable {
 40
 41	private static final String OPC = "opc";
 42	private static final String MASK = "mask";
 43	private static final String ARRAY_SIZE = "size";
 44
 45	private byte[] value;
 46	private int[] pointCodes;
 47	private short[] masks;
 48	
 49	public OPCListImpl(){
 50		this.tag = Parameter.Originating_Point_Code_List;
 51	}
 52
 53	protected OPCListImpl(byte[] value) {
 54		this.tag = Parameter.Originating_Point_Code_List;
 55
 56		int count = 0;
 57		int arrSize = 0;
 58		pointCodes = new int[(value.length / 4)];
 59		masks = new short[(value.length / 4)];
 60
 61		while (count < value.length) {
 62			masks[arrSize] = value[count++];
 63
 64			pointCodes[arrSize] = 0;
 65			pointCodes[arrSize] |= value[count++] & 0xFF;
 66			pointCodes[arrSize] <<= 8;
 67			pointCodes[arrSize] |= value[count++] & 0xFF;
 68			pointCodes[arrSize] <<= 8;
 69			pointCodes[arrSize++] |= value[count++] & 0xFF;
 70		}
 71		this.value = value;
 72	}
 73
 74	protected OPCListImpl(int[] pointCodes, short[] masks) {
 75		this.tag = Parameter.Originating_Point_Code_List;
 76		this.pointCodes = pointCodes;
 77		this.masks = masks;
 78		encode();
 79	}
 80
 81	private void encode() {
 82		// create byte array taking into account data, point codes and
 83		// indicators;
 84
 85		this.value = new byte[(pointCodes.length * 4)];
 86
 87		int count = 0;
 88		int arrSize = 0;
 89		// encode routing context
 90		while (count < value.length) {
 91			value[count++] = (byte) (masks[arrSize]);
 92
 93			value[count++] = (byte) (pointCodes[arrSize] >>> 16);
 94			value[count++] = (byte) (pointCodes[arrSize] >>> 8);
 95			value[count++] = (byte) (pointCodes[arrSize++]);
 96		}
 97	}
 98
 99	@Override
100	protected byte[] getValue() {
101		return this.value;
102	}
103
104	public short[] getMasks() {
105		return this.masks;
106	}
107
108	public int[] getPointCodes() {
109		return this.pointCodes;
110	}
111
112	@Override
113	public String toString() {
114		return String.format("OPCList pointCode=%s mask=%s", Arrays.toString(this.pointCodes),
115				Arrays.toString(this.masks));
116	}
117
118	/**
119	 * XML Serialization/Deserialization
120	 */
121	protected static final XMLFormat<OPCListImpl> RC_XML = new XMLFormat<OPCListImpl>(OPCListImpl.class) {
122
123		@Override
124		public void read(javolution.xml.XMLFormat.InputElement xml, OPCListImpl opc) throws XMLStreamException {
125			int size = xml.getAttribute(ARRAY_SIZE).toInt();
126			opc.pointCodes = new int[size];
127			opc.masks = new short[size];
128
129			for (int i = 0; i < opc.pointCodes.length; i++) {
130				opc.pointCodes[i] = xml.get(OPC);
131			}
132
133			for (int i = 0; i < opc.masks.length; i++) {
134				opc.masks[i] = xml.get(MASK);
135			}
136			
137			opc.encode();
138
139		}
140
141		@Override
142		public void write(OPCListImpl opc, javolution.xml.XMLFormat.OutputElement xml) throws XMLStreamException {
143			xml.setAttribute(ARRAY_SIZE, opc.pointCodes.length);
144			for (int i : opc.pointCodes) {
145				xml.add(i, OPC);
146			}
147
148			for (short s : opc.masks) {
149				xml.add(s, MASK);
150			}
151		}
152	};
153}