001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.common.filters;
042:
043: import java.util.Map;
044: import java.util.Vector;
045:
046: /**
047: * This class encapsulates globally defined filters (name-value pairs) and the functionality around
048: *
049: * @author Tomas Hurka
050: * @author Jiri Sedlacek
051: */
052: public final class GlobalFilters {
053: //~ Instance fields ----------------------------------------------------------------------------------------------------------
054:
055: private String[] filterNames;
056: private String[] filterValues;
057:
058: //~ Constructors -------------------------------------------------------------------------------------------------------------
059:
060: /** Creates a new instance of GlobalFilters */
061: public GlobalFilters() {
062: clear();
063: }
064:
065: //~ Methods ------------------------------------------------------------------------------------------------------------------
066:
067: public int getFilterIndex(final String filterName) {
068: for (int i = 0; i < filterNames.length; i++) {
069: if (filterNames[i].equals(filterName)) {
070: return i;
071: }
072: }
073:
074: return -1;
075: }
076:
077: public void setFilterNames(final String[] names) {
078: if (names == null) {
079: return;
080: }
081:
082: filterNames = names;
083: }
084:
085: public String[] getFilterNames() {
086: return filterNames;
087: }
088:
089: public String getFilterValue(final String filterName) {
090: final int filterIndex = getFilterIndex(filterName);
091:
092: if (filterIndex == -1) {
093: return null;
094: }
095:
096: return filterValues[filterIndex];
097: }
098:
099: public void setFilterValues(final String[] values) {
100: if (values == null) {
101: return;
102: }
103:
104: filterValues = values;
105: }
106:
107: public String[] getFilterValues() {
108: return filterValues;
109: }
110:
111: public void clear() {
112: initBuffers(0);
113: }
114:
115: public void load(final Map props) {
116: final Vector filterNamesVect = new Vector();
117: final Vector filterValuesVect = new Vector();
118:
119: String filterName;
120:
121: while ((filterName = (String) props.get("Filter-"
122: + filterNamesVect.size() + "-name")) != null) { //NOI18N
123: filterNamesVect.add(filterName);
124: filterValuesVect.add(getProperty(props, "Filter-"
125: + filterValuesVect.size() + "-value", "")); //NOI18N
126: }
127:
128: filterNames = new String[filterNamesVect.size()];
129: filterValues = new String[filterValuesVect.size()];
130:
131: for (int i = 0; i < filterNamesVect.size(); i++) {
132: filterNames[i] = (String) filterNamesVect.get(i);
133: filterValues[i] = (String) filterValuesVect.get(i);
134: }
135: }
136:
137: public void store(final Map props) {
138: String itemPrefix;
139:
140: for (int i = 0; i < filterNames.length; i++) {
141: itemPrefix = "Filter-" + i + "-"; //NOI18N
142: props.put(itemPrefix + "name", filterNames[i]); //NOI18N
143: props.put(itemPrefix + "value", filterValues[i]); //NOI18N
144: }
145: }
146:
147: private static String getProperty(final Map props,
148: final Object key, final String defaultValue) {
149: final Object ret = props.get(key);
150:
151: return (ret != null) ? (String) ret : defaultValue;
152: }
153:
154: private void initBuffers(final int capacity) {
155: filterNames = new String[capacity];
156: filterValues = new String[capacity];
157: }
158: }
|