View Javadoc

1   /*
2    * jSDP: A Java implementation of SDP protocol Copyright (C) 2007 Claudio Di
3    * Vita
4    */
5   package net.sourceforge.jsdp.util;
6   
7   import net.sourceforge.jsdp.SDPException;
8   
9   /**
10   * A network resource specified in a SDP message.
11   * 
12   * @since 0.1.0
13   * 
14   * @version 1.0
15   * 
16   * @author <a href="mailto:cdivita@users.sourceforge.net">Claudio Di Vita</a>
17   */
18  public class Resource extends Address {
19  
20      /** The class Stream Unique Identifier, SUID */
21      private static final long serialVersionUID = 1512624495750991087L;
22  
23      /** The resource TTL */
24      protected int ttl;
25  
26      /** The number of addresses available for the resource */
27      protected int addresses;
28  
29      /**
30       * Creates a new <tt>Resource</tt>.
31       */
32      protected Resource() {
33  
34          super();
35      }
36  
37      /**
38       * Creates a new <tt>Resource</tt>.
39       * 
40       * @param address the resource address. The address may contains ttl and the
41       *        number of addresses too
42       * 
43       * @throws SDPException if the resource informations are not valid
44       */
45      public Resource(final String address) throws SDPException {
46  
47          if (address == null) {
48              throw new SDPException("Invalid network resource");
49          }
50  
51          String[] values = address.split("/");
52          switch (values.length) {
53              case 1:
54                  setAddress(values[0]);
55                  this.ttl = 0;
56                  this.addresses = 1;
57                  break;
58              case 2:
59                  setAddress(values[0]);
60                  setTTL(Integer.parseInt(values[1]));
61                  this.addresses = 1;
62                  break;
63              case 3:
64                  setAddress(values[0]);
65                  setTTL(Integer.parseInt(values[1]));
66                  setAddresses(Integer.parseInt(values[2]));
67                  break;
68              default:
69                  throw new SDPException("Invalid network resource");
70          }
71      }
72  
73      /**
74       * Creates a new <tt>Resource</tt>.
75       * 
76       * @param address the resource address. The address cannot contains ttl and
77       *        the number of addresses
78       * 
79       * @param ttl The resource ttl
80       * 
81       * @throws SDPException if the resource informations are not valid
82       */
83      public Resource(final String address, final int ttl) throws SDPException {
84  
85          super(address);
86          setTTL(ttl);
87          this.addresses = 1;
88      }
89  
90      /**
91       * Creates a new <tt>Resource</tt>.
92       * 
93       * @param address the resource address. The address cannot contains ttl and
94       *        the number of addresses
95       * 
96       * @param ttl the resource ttl
97       * 
98       * @param addresses the number of addresses available for the resource
99       * 
100      * @throws SDPException if the resource informations are not valid
101      */
102     public Resource(final String address, final int ttl, final int addresses) throws SDPException {
103 
104         super(address);
105         setTTL(ttl);
106         setAddresses(addresses);
107     }
108 
109     /**
110      * Returns a clone of this resource.
111      * 
112      * @return a clone of this resource
113      */
114     public Object clone() {
115 
116         Resource host = new Resource();
117 
118         host.addressType = this.addressType;
119         host.address = new String(this.address);
120         host.ttl = this.ttl;
121         host.addresses = this.addresses;
122 
123         return host;
124     }
125 
126     /**
127      * Returns the TTL.
128      * 
129      * @return the TTL
130      */
131     public int getTTL() {
132 
133         return ttl;
134     }
135 
136     /**
137      * Indicates if the resource has a TTL.
138      * 
139      * @return <tt>true</tt> if the resource has a TTL, <tt>false</tt>
140      *         otherwise
141      */
142     public boolean hasTTL() {
143 
144         return ttl > 0;
145     }
146 
147     /**
148      * Clears the resource's TTL.
149      */
150     public void removeTTL() {
151 
152         this.ttl = 0;
153         this.addresses = 1;
154     }
155 
156     /**
157      * Sets the number of addresses of the resource.
158      * 
159      * @param addresses the number of addresses
160      * 
161      * @throws SDPException if the number of addesses is negative
162      */
163     public void setAddresses(final int addresses) throws SDPException {
164 
165         if (addresses < 1) {
166             throw new SDPException();
167         }
168 
169         this.addresses = addresses;
170     }
171 
172     /**
173      * Sets the resource TTL.
174      * 
175      * @param ttl the TTL
176      * 
177      * @throws SDPException if the TTL is minor than 1 or greater than 255
178      */
179     public void setTTL(final int ttl) throws SDPException {
180 
181         if (ttl < 1 || ttl > 255) {
182             throw new SDPException("Invalid TTL");
183         }
184 
185         this.ttl = ttl;
186     }
187 
188     /**
189      * Returns a string representation of the resource.
190      */
191     public String toString() {
192 
193         StringBuffer result = new StringBuffer(super.toString());
194 
195         if (hasTTL()) {
196             result.append("/" + ttl);
197             if (this.addresses > 1) {
198                 result.append("/" + addresses);
199             }
200         }
201 
202         return result.toString();
203     }
204 }