1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * ident "%Z%%M% %I% %E% SMI"
24 *
25 * Copyright (c) 1998-2001 by Sun Microsystems, Inc.
26 * All rights reserved.
27 */
28 package com.sun.dhcpmgr.data;
29
30 import java.util.*;
31 import java.io.Serializable;
32
33 /**
34 * This class provides a global table of all the options currently known.
35 * It is implemented as a singleton as there should be no need for more
36 * than a single instance of this table. It includes both the standard
37 * options and any vendor or site options defined in the current environment.
38 */
39 public class OptionsTable implements Serializable {
40 private Hashtable options;
41 private static OptionsTable table = null;
42
43 protected OptionsTable() {
44 // Get the standard options we know about
45 StandardOptions stdopts = new StandardOptions();
46
47 // Initialize hash table with extra size we will probably need.
48 options = new Hashtable(stdopts.size() + 20);
49
50 // Add the standard options to the table
51 add(stdopts.getAllOptions());
52 }
53
54 /**
55 * Add an array of options to the table.
56 * @param opts An array of Options
57 */
58 public void add(Option [] opts) {
59 for (int i = 0; opts != null && i < opts.length; ++i) {
60 add(opts[i]);
61 }
62 }
63
64 /**
65 * Add a single option to the table.
66 * @param o The option to add.
67 */
68 public void add(Option o) {
69 // Don't add unless it is a valid option.
70 if (o.isValid()) {
71 options.put(o.getKey(), o);
72 }
73 }
74
75 /**
76 * Retrieve an option from the table by name
77 * @param opt the name of the option to retrieve
78 * @return the option found, or null if the option is not in the table
79 */
80 public Option get(String opt) {
81 return (Option)options.get(opt);
82 }
83
84 /**
85 * Retrieve an option from the table by its code
86 * @param code the code of the option to retrieve
87 * @return the option found, or null if the option is not in the table
88 */
89 public Option getByCode(short code) {
90
91 Option option = null;
92 for (Enumeration e = elements(); e.hasMoreElements(); ) {
93 if (((Option)e.nextElement()).getCode() == code) {
94 option = (Option)e.nextElement();
95 }
96 }
97 return (option);
98 }
99
100 /**
101 * Enumerate the options in this table for those that might need to walk it.
102 * @return an Enumeration of the options
103 */
104 public Enumeration elements() {
105 return options.elements();
106 }
107
108 /**
109 * Return the global table, create it if not already in existence.
110 * @return the current options table
111 */
112 public static OptionsTable getTable() {
113 if (table == null) {
114 table = new OptionsTable();
115 }
116 return table;
117 }
118 }