001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020
021 package org.apache.directory.server.dhcp.options;
022
023
024 import java.util.HashMap;
025 import java.util.Iterator;
026 import java.util.Map;
027
028
029 /**
030 * The Dynamic Host Configuration Protocol (DHCP) provides a framework
031 * for passing configuration information to hosts on a TCP/IP network.
032 * Configuration parameters and other control information are carried in
033 * tagged data items that are stored in the 'options' field of the DHCP
034 * message. The data items themselves are also called "options."
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 * @version $Rev: 642496 $, $Date: 2008-03-29 05:09:22 +0200 (Sat, 29 Mar 2008) $
038 */
039 public class OptionsField
040 {
041 /**
042 * A map of option code (Integer)->DhcpOption. FIXME: use IntHashtable from
043 * commons collections
044 */
045 private Map options = new HashMap();
046
047
048 public void add( DhcpOption option )
049 {
050 options.put( new Integer( option.getTag() ), option );
051 }
052
053
054 public boolean isEmpty()
055 {
056 return options.isEmpty();
057 }
058
059
060 public Iterator iterator()
061 {
062 return options.values().iterator();
063 }
064
065
066 /**
067 * Return the (first) DHCP option matching a given option class or
068 * <code>null</code> of the option isn't set.
069 *
070 * @param optionClass
071 */
072 public DhcpOption get( Class optionClass )
073 {
074 Integer key = new Integer( DhcpOption.getTagByClass( optionClass ) );
075 return ( DhcpOption ) options.get( key );
076 }
077
078
079 /**
080 * Return the (first) DHCP option matching a given tag or <code>null</code>
081 * of the option isn't set.
082 *
083 * @param tag
084 */
085 public DhcpOption get( int tag )
086 {
087 Integer key = new Integer( tag );
088 return ( DhcpOption ) options.get( key );
089 }
090
091
092 /**
093 * Merge the options from the given options field into my options. Existing
094 * options are replaced by the ones from the supplied options field.
095 *
096 * @param options
097 */
098 public void merge( OptionsField options )
099 {
100 if ( null == options )
101 return;
102
103 for ( Iterator i = options.iterator(); i.hasNext(); )
104 {
105 DhcpOption option = ( DhcpOption ) i.next();
106 this.options.put( new Integer( option.getTag() ), option );
107 }
108 }
109
110
111 /**
112 * Remove instances of the given option class.
113 *
114 * @param c
115 */
116 public void remove( Class c )
117 {
118 Integer key = new Integer( DhcpOption.getTagByClass( c ) );
119 options.remove( key );
120 }
121
122
123 /**
124 * Remove options matching the given tag
125 *
126 * @param tag
127 */
128 public void remove( int tag )
129 {
130 Integer key = new Integer( tag );
131 options.remove( key );
132 }
133
134
135 /**
136 * @see Map#clear()
137 */
138 public void clear()
139 {
140 options.clear();
141 }
142 }