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 * @version $Rev: 642496 $, $Date: 2008-03-29 04:09:22 +0100 (Sa, 29 Mär 2008) $ 38 */ 39 public class OptionsField 40 { 41 /** 42 * A map of option code (Integer)->DhcpOption. FIXME: use IntHashtable from 43 * commons collections 44 */ 45 private Map options = new HashMap(); 46 47 48 public void add( DhcpOption option ) 49 { 50 options.put( new Integer( option.getTag() ), option ); 51 } 52 53 54 public boolean isEmpty() 55 { 56 return options.isEmpty(); 57 } 58 59 60 public Iterator iterator() 61 { 62 return options.values().iterator(); 63 } 64 65 66 /** 67 * Return the (first) DHCP option matching a given option class or 68 * <code>null</code> of the option isn't set. 69 * 70 * @param optionClass 71 */ 72 public DhcpOption get( Class optionClass ) 73 { 74 Integer key = new Integer( DhcpOption.getTagByClass( optionClass ) ); 75 return ( DhcpOption ) options.get( key ); 76 } 77 78 79 /** 80 * Return the (first) DHCP option matching a given tag or <code>null</code> 81 * of the option isn't set. 82 * 83 * @param tag 84 */ 85 public DhcpOption get( int tag ) 86 { 87 Integer key = new Integer( tag ); 88 return ( DhcpOption ) options.get( key ); 89 } 90 91 92 /** 93 * Merge the options from the given options field into my options. Existing 94 * options are replaced by the ones from the supplied options field. 95 * 96 * @param options 97 */ 98 public void merge( OptionsField options ) 99 { 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 }