PageRenderTime 28ms CodeModel.GetById 8ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/Address.java

http://mobicents.googlecode.com/
Java | 170 lines | 73 code | 20 blank | 77 comment | 11 complexity | 8c0f476e6b7a882e7079988c02e085c8 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.smpp;
 24
 25import org.mobicents.protocols.smpp.util.PacketDecoder;
 26import org.mobicents.protocols.smpp.util.PacketEncoder;
 27
 28/**
 29 * Object representing a Short Message Entity's address. An address consists of
 30 * a Type Of Number, a Numbering Plan Indicator and an address.
 31 * 
 32 * @see com.adenki.smpp.util.GSMConstants
 33 */
 34public class Address implements java.io.Serializable {
 35    private static final long serialVersionUID = 2L;
 36    
 37    /** Type of number. */
 38    private int ton = Ton.UNKNOWN;
 39
 40    /** Numbering plan indicator. */
 41    private int npi = Npi.UNKNOWN;
 42
 43    /** The address. */
 44    private String address = "";
 45
 46    /**
 47     * Create a new Address with all nul values. TON will be 0, NPI will be 0
 48     * and the address field will be blank.
 49     */
 50    public Address() {
 51    }
 52
 53    /**
 54     * Create a new Address.
 55     * 
 56     * @param ton
 57     *            The Type Of Number.
 58     * @param npi
 59     *            The Numbering Plan Indicator.
 60     * @param address
 61     *            The address.
 62     */
 63    public Address(int ton, int npi, String address) {
 64        this.ton = ton;
 65        this.npi = npi;
 66        this.address = address;
 67    }
 68
 69    /**
 70     * Get the Type Of Number.
 71     */
 72    public int getTON() {
 73        return ton;
 74    }
 75
 76    /**
 77     * Set the Type of Number.
 78     */
 79    public void setTON(int ton) {
 80        this.ton = ton;
 81    }
 82
 83    /**
 84     * Get the Numbering Plan Indicator.
 85     */
 86    public int getNPI() {
 87        return npi;
 88    }
 89
 90    /**
 91     * Set the Numbering Plan Indicator.
 92     */
 93    public void setNPI(int npi) {
 94        this.npi = npi;
 95    }
 96
 97    /**
 98     * Get the address.
 99     */
100    public String getAddress() {
101        return address;
102    }
103
104    /**
105     * Set the address.
106     */
107    public void setAddress(String address) {
108        this.address = (address != null) ? address : "";
109    }
110
111    /**
112     * Get a unique hash code for this address.
113     */
114    public int hashCode() {
115        StringBuilder buf = new StringBuilder();
116        buf.append(Integer.toString(ton)).append(':');
117        buf.append(Integer.toString(npi)).append(':');
118        if (address != null) {
119            buf.append(address);
120        }
121        return buf.hashCode();
122    }
123
124    /**
125     * Test for equality. Two address objects are equal if their TON, NPI and
126     * address fields are equal.
127     */
128    public boolean equals(Object obj) {
129        if (obj == null) {
130            return false;
131        }
132        if (obj instanceof Address) {
133            Address a = (Address) obj;
134            return (a.ton == ton) && (a.npi == npi) && (a.address.equals(address));
135        } else {
136            return false;
137        }
138    }
139
140    /**
141     * Get the number of bytes this object would encode to.
142     */
143    public int getLength() {
144        return 3 + address.length();
145    }
146
147    /**
148     */
149    public void writeTo(PacketEncoder encoder) throws java.io.IOException {
150        encoder.writeUInt1(ton);
151        encoder.writeUInt1(npi);
152        encoder.writeCString(address);
153    }
154
155    /**
156     * TODO: doc
157     */
158    public void readFrom(PacketDecoder decoder) {
159        ton = decoder.readUInt1();
160        npi = decoder.readUInt1();
161        address = decoder.readCString();
162    }
163
164    public String toString() {
165        return new StringBuffer(25)
166        .append(Integer.toString(ton)).append(':')
167        .append(Integer.toString(npi)).append(':')
168        .append(address).toString();
169    }
170}