View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *
19   */
20  
21  package org.apache.directory.server.dhcp.options;
22  
23  
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  
29  /**
30   * The Dynamic Host Configuration Protocol (DHCP) provides a framework
31   * for passing configuration information to hosts on a TCP/IP network.  
32   * Configuration parameters and other control information are carried in
33   * tagged data items that are stored in the 'options' field of the DHCP
34   * message.  The data items themselves are also called "options."
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class OptionsField
39  {
40      /**
41       * A map of option code (Integer)->DhcpOption. FIXME: use IntHashtable from
42       * commons collections
43       */
44      private Map options = new HashMap();
45  
46  
47      public void add( DhcpOption option )
48      {
49          options.put( Integer.valueOf( option.getTag() ), option );
50      }
51  
52  
53      public boolean isEmpty()
54      {
55          return options.isEmpty();
56      }
57  
58  
59      public Iterator iterator()
60      {
61          return options.values().iterator();
62      }
63  
64  
65      /**
66       * Return the (first) DHCP option matching a given option class or
67       * <code>null</code> of the option isn't set.
68       * 
69       * @param optionClass
70       */
71      public DhcpOption get( Class optionClass )
72      {
73          Integer key = Integer.valueOf( DhcpOption.getTagByClass( optionClass ) );
74          return ( DhcpOption ) options.get( key );
75      }
76  
77  
78      /**
79       * Return the (first) DHCP option matching a given tag or <code>null</code>
80       * of the option isn't set.
81       * 
82       * @param tag
83       */
84      public DhcpOption get( int tag )
85      {
86          Integer key = Integer.valueOf( tag );
87          return ( DhcpOption ) options.get( key );
88      }
89  
90  
91      /**
92       * Merge the options from the given options field into my options. Existing
93       * options are replaced by the ones from the supplied options field.
94       * 
95       * @param options
96       */
97      public void merge( OptionsField options )
98      {
99          if ( null == options )
100         {
101             return;
102         }
103 
104         for ( Iterator i = options.iterator(); i.hasNext(); )
105         {
106             DhcpOption/../../../../org/apache/directory/server/dhcp/options/DhcpOption.html#DhcpOption">DhcpOption option = ( DhcpOption ) i.next();
107             this.options.put( Integer.valueOf( option.getTag() ), option );
108         }
109     }
110 
111 
112     /**
113      * Remove instances of the given option class.
114      * 
115      * @param c
116      */
117     public void remove( Class c )
118     {
119         Integer key = Integer.valueOf( DhcpOption.getTagByClass( c ) );
120         options.remove( key );
121     }
122 
123 
124     /**
125      * Remove options matching the given tag
126      * 
127      * @param tag
128      */
129     public void remove( int tag )
130     {
131         Integer key = Integer.valueOf( tag );
132         options.remove( key );
133     }
134 
135 
136     /**
137      * @see Map#clear()
138      */
139     public void clear()
140     {
141         options.clear();
142     }
143 }