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
021package org.apache.directory.server.dhcp.options;
022
023
024import java.util.HashMap;
025import java.util.Iterator;
026import 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 */
038public class OptionsField
039{
040    /**
041     * A map of option code (Integer)->DhcpOption. FIXME: use IntHashtable from
042     * commons collections
043     */
044    private Map options = new HashMap();
045
046
047    public void add( DhcpOption option )
048    {
049        options.put( Integer.valueOf( option.getTag() ), option );
050    }
051
052
053    public boolean isEmpty()
054    {
055        return options.isEmpty();
056    }
057
058
059    public Iterator iterator()
060    {
061        return options.values().iterator();
062    }
063
064
065    /**
066     * Return the (first) DHCP option matching a given option class or
067     * <code>null</code> of the option isn't set.
068     * 
069     * @param optionClass
070     */
071    public DhcpOption get( Class optionClass )
072    {
073        Integer key = Integer.valueOf( DhcpOption.getTagByClass( optionClass ) );
074        return ( DhcpOption ) options.get( key );
075    }
076
077
078    /**
079     * Return the (first) DHCP option matching a given tag or <code>null</code>
080     * of the option isn't set.
081     * 
082     * @param tag
083     */
084    public DhcpOption get( int tag )
085    {
086        Integer key = Integer.valueOf( tag );
087        return ( DhcpOption ) options.get( key );
088    }
089
090
091    /**
092     * Merge the options from the given options field into my options. Existing
093     * options are replaced by the ones from the supplied options field.
094     * 
095     * @param options
096     */
097    public void merge( OptionsField options )
098    {
099        if ( null == options )
100        {
101            return;
102        }
103
104        for ( Iterator i = options.iterator(); i.hasNext(); )
105        {
106            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}