PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/message/DestinationTable.java

http://mobicents.googlecode.com/
Java | 162 lines | 104 code | 22 blank | 36 comment | 21 complexity | 39ea238ab4546ad55b27ddd7a3629c9a 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.message;
 24
 25import java.io.Serializable;
 26import java.util.ArrayList;
 27import java.util.Collection;
 28import java.util.Collections;
 29import java.util.List;
 30
 31import org.slf4j.LoggerFactory;
 32
 33import org.mobicents.protocols.smpp.Address;
 34import org.mobicents.protocols.smpp.util.PacketDecoder;
 35import org.mobicents.protocols.smpp.util.PacketEncoder;
 36
 37/**
 38 * A table of destinations, primarily used in
 39 * {@link org.mobicents.protocols.smpp.message.SubmitMulti}.
 40 * @version $Id: DestinationTable.java 457 2009-01-15 17:37:42Z orank $
 41 */
 42public class DestinationTable implements Serializable {
 43    private static final long serialVersionUID = 2L;
 44    
 45    private List<Address> addresses = new ArrayList<Address>();
 46    private List<String> distributionLists = new ArrayList<String>();
 47
 48    /**
 49     * The length is the total number of bytes the table would encode as.
 50     */
 51    private int length;
 52
 53    public DestinationTable() {
 54    }
 55
 56    public void add(Address addr) {
 57        addresses.add(addr);
 58        // Plus 1 for the dest type flag.
 59        length += addr.getLength() + 1;
 60    }
 61
 62    public void add(String distributionList) {
 63        distributionLists.add(distributionList);
 64        // nul byte plus dest type flag
 65        length += distributionList.length() + 2;
 66    }
 67
 68    public void remove(Address addr) {
 69        int i = addresses.indexOf(addr);
 70        if (i > -1) {
 71            length -= addresses.remove(i).getLength() + 1;
 72        }
 73    }
 74
 75    public void remove(String distributionList) {
 76        int i = distributionLists.indexOf(distributionList);
 77        if (i > -1) {
 78            length -= distributionLists.remove(i).length() + 2;
 79        }
 80    }
 81
 82    public int getLength() {
 83        return length;
 84    }
 85
 86    public int size() {
 87        return addresses.size() + distributionLists.size();
 88    }
 89
 90    public Collection<Address> getAddresses() {
 91        return Collections.unmodifiableCollection(addresses);
 92    }
 93    
 94    public Collection<String> getDistributionLists() {
 95        return Collections.unmodifiableCollection(distributionLists);
 96    }
 97    
 98    public void writeTo(PacketEncoder encoder) throws java.io.IOException {
 99        for (Address address : addresses) {
100            encoder.writeUInt1(1);
101            encoder.writeAddress(address);
102        }
103        for (String list : distributionLists) {
104            encoder.writeUInt1(2);
105            encoder.writeCString(list);
106        }
107    }
108
109    public void readFrom(PacketDecoder decoder, int count) {
110        for (int i = 0; i < count; i++) {
111            int type = decoder.readUInt1();
112            if (type == 1) {
113                // SME address..
114                addresses.add(decoder.readAddress());
115            } else if (type == 2) {
116                // Distribution list name
117                distributionLists.add(decoder.readCString());
118            } else {
119                LoggerFactory.getLogger(DestinationTable.class).warn(
120                        "Unidentified destination type on input.");
121            }
122        }
123        calculateLength();
124    }
125
126    public boolean equals(Object obj) {
127        if (this == obj) {
128            return true;
129        }
130        if (obj == null || !(obj instanceof DestinationTable)) {
131            return false;
132        }
133        DestinationTable other = (DestinationTable) obj;
134        return length == other.length
135                && addresses.equals(other.addresses)
136                && distributionLists.equals(other.distributionLists); 
137    }
138    
139    public int hashCode() {
140        return addresses.hashCode() + distributionLists.hashCode();
141    }
142    
143    public String toString() {
144        List<Object> list = new ArrayList<Object>();
145        list.addAll(addresses);
146        list.addAll(distributionLists);
147        return list.toString();
148    }
149    
150    private void calculateLength() {
151        // One byte for all type flags, plus 1 (null) byte for each distribution
152        // list string
153        length = addresses.size() + (distributionLists.size() * 2);
154        for (Address address : addresses) {
155            // For the destination type flag
156            length += address.getLength();
157        }
158        for (String list : distributionLists) {
159            length += list.length();
160        }
161    }
162}