001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.lib.web.arch.util;
027:
028: import java.util.ArrayList;
029: import java.util.Collections;
030: import java.util.List;
031:
032: /**
033: * Implementation of PrefixTable API.
034: * <p>
035: * This is a lazy implementation with a flat list of (String, Object)
036: * pairs. It's probably fine, since we expect these tables to be
037: * relatively small (~10 entries).
038: */
039: public class PrefixTableImpl implements PrefixTable {
040:
041: // List<Entry>
042: private final List l = new ArrayList();
043:
044: public Object match(String input) {
045: if (input != null) {
046: int il = input.length();
047: synchronized (l) {
048: for (int i = 0, n = l.size(); i < n; i++) {
049: Entry ei = (Entry) l.get(i);
050: String pi = ei.prefix;
051: int pil = pi.length();
052: if (il >= pil && input.startsWith(pi)) {
053: if (il == pil) {
054: return ei.value;
055: } else if (input.charAt(pil) == '/') {
056: return new PrefixMatch(input.substring(0,
057: pil), input.substring(pil),
058: ei.value);
059: }
060: }
061: }
062: }
063: }
064: return null;
065: }
066:
067: public List list() {
068: synchronized (l) {
069: List ret = new ArrayList(l.size());
070: for (int i = 0, n = l.size(); i < n; i++) {
071: Entry ei = (Entry) l.get(i);
072: ret.add(ei.prefix);
073: }
074: return ret;
075: }
076: }
077:
078: public boolean add(String prefix, Object value) {
079: if (prefix == null || value == null) {
080: throw new IllegalArgumentException("null arg");
081: }
082: synchronized (l) {
083: if (match(prefix) != null) {
084: return false;
085: }
086: l.add(new Entry(prefix, value));
087: return true;
088: }
089: }
090:
091: public Object remove(String prefix) {
092: if (prefix != null) {
093: synchronized (l) {
094: for (int i = 0, n = l.size(); i < n; i++) {
095: Entry ei = (Entry) l.get(i);
096: if (prefix.equals(ei.prefix)) {
097: l.remove(i);
098: return ei.value;
099: }
100: }
101: }
102: }
103: return null;
104: }
105:
106: public List removeAll() {
107: synchronized (l) {
108: int n = l.size();
109: if (n == 0) {
110: return Collections.EMPTY_LIST;
111: } else {
112: List ret = new ArrayList(n);
113: for (int i = 0; i < n; i++) {
114: Entry ei = (Entry) l.get(i);
115: ret.add(ei.value);
116: }
117: l.clear();
118: return ret;
119: }
120: }
121: }
122:
123: public String toString() {
124: synchronized (l) {
125: return l.toString();
126: }
127: }
128:
129: private static class Entry {
130: public final String prefix;
131: public final Object value;
132:
133: public Entry(String prefix, Object value) {
134: this .prefix = prefix;
135: this .value = value;
136: }
137:
138: public String toString() {
139: return "(" + prefix + ", " + value + ")";
140: }
141: }
142: }
|