|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007 Wind River Systems, Inc. and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Martin Oberhuber (Wind River) - initial API and implementation for [105554] |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.ui.internal.ide.filesystem; |
| 13 |
|
| 14 |
import java.util.Arrays; |
| 15 |
|
| 16 |
/** |
| 17 |
* A pool of Strings for doing prefix checks against multiple |
| 18 |
* candidates. |
| 19 |
* <p> |
| 20 |
* Allows to enter a list of Strings, and then perform the |
| 21 |
* following checks: |
| 22 |
* <ul> |
| 23 |
* <li>{@link #containsAsPrefix(String)} - check whether a given |
| 24 |
* String s is a prefix of any String in the pool.</li> |
| 25 |
* <li>{@link #hasPrefixOf(String)} - check whether any String |
| 26 |
* in the pool is a prefix of the given String s. |
| 27 |
* </ul> |
| 28 |
* The prefix pool is always kept normalized, i.e. no element of |
| 29 |
* the pool is a prefix of any other element in the pool. In order |
| 30 |
* to maintain this constraint, there are two methods for adding |
| 31 |
* Strings to the pool: |
| 32 |
* <ul> |
| 33 |
* <li>{@link #insertLonger(String)} - add a String s to the pool, |
| 34 |
* and remove any existing prefix of s from the pool.</li> |
| 35 |
* <li>{@link #insertShorter(String)} - add a String s to the pool, |
| 36 |
* and remove any existing Strings sx from the pool which |
| 37 |
* contain s as prefix.</li> |
| 38 |
* </ul> |
| 39 |
* The PrefixPool grows as needed when adding Strings. Typically, |
| 40 |
* it is used for prefix checks on absolute paths of a tree. |
| 41 |
* </p><p> |
| 42 |
* This class is not thread-safe: no two threads may add or |
| 43 |
* check items at the same time. |
| 44 |
* |
| 45 |
* @since 3.5.1 |
| 46 |
* |
| 47 |
* Class copied from org.eclipse.core.internal.localstore.PrefixPool |
| 48 |
* |
| 49 |
*/ |
| 50 |
public class PrefixPool { |
| 51 |
private String[] pool; |
| 52 |
private int size; |
| 53 |
|
| 54 |
/** |
| 55 |
* Constructor. |
| 56 |
* @param initialCapacity the initial size of the |
| 57 |
* internal array holding the String pool. Must |
| 58 |
* be greater than 0. |
| 59 |
*/ |
| 60 |
public PrefixPool(int initialCapacity) { |
| 61 |
if (initialCapacity <= 0) |
| 62 |
throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); //$NON-NLS-1$ |
| 63 |
pool = new String[initialCapacity]; |
| 64 |
size = 0; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Clears the prefix pool, allowing all items to be |
| 69 |
* garbage collected. Does not change the capacity |
| 70 |
* of the pool. |
| 71 |
*/ |
| 72 |
public/*synchronized*/void clear() { |
| 73 |
Arrays.fill(pool, 0, size, null); |
| 74 |
size = 0; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Return the current size of prefix pool. |
| 79 |
* @return the number of elements in the pool. |
| 80 |
*/ |
| 81 |
public/*synchronized*/int size() { |
| 82 |
return size; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Ensure that there is room for at least one more element. |
| 87 |
*/ |
| 88 |
private void checkCapacity() { |
| 89 |
if (size + 1 >= pool.length) { |
| 90 |
String[] newprefixList = new String[2 * pool.length]; |
| 91 |
System.arraycopy(pool, 0, newprefixList, 0, pool.length); |
| 92 |
Arrays.fill(pool, null); //help the garbage collector |
| 93 |
pool = newprefixList; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Insert a String s into the pool of known prefixes, removing |
| 99 |
* any existing prefix of it. |
| 100 |
* <p> |
| 101 |
* If any existing prefix of this String is found in the pool, |
| 102 |
* it is replaced by the new longer one in order to maintain |
| 103 |
* the constraint of keeping the pool normalized. |
| 104 |
* </p><p> |
| 105 |
* If it turns out that s is actually a prefix or equal to |
| 106 |
* an existing element in the pool (so it is essentially |
| 107 |
* shorter), this method returns with no operation in order |
| 108 |
* to maintain the constraint that the pool remains normalized. |
| 109 |
* </p> |
| 110 |
* @param s the String to insert. |
| 111 |
*/ |
| 112 |
public/*synchronized*/void insertLonger(String s) { |
| 113 |
//check in reverse order since we expect some locality |
| 114 |
for (int i = size - 1; i >= 0; i--) { |
| 115 |
if (pool[i].startsWith(s)) { |
| 116 |
//prefix of an existing String --> no-op |
| 117 |
return; |
| 118 |
} else if (s.startsWith(pool[i])) { |
| 119 |
//replace, since a longer s has more prefixes than a short one |
| 120 |
pool[i] = s; |
| 121 |
return; |
| 122 |
} |
| 123 |
} |
| 124 |
checkCapacity(); |
| 125 |
pool[size] = s; |
| 126 |
size++; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Insert a String s into the pool of known prefixes, removing |
| 131 |
* any Strings that have s as prefix. |
| 132 |
* <p> |
| 133 |
* If this String is a prefix of any existing String in the pool, |
| 134 |
* all elements that contain the new String as prefix are removed |
| 135 |
* and return value <code>true</code> is returned. |
| 136 |
* </p><p> |
| 137 |
* Otherwise, the new String is added to the pool unless an |
| 138 |
* equal String or e prefix of it exists there already (so |
| 139 |
* it is essentially equal or longer than an existing prefix). |
| 140 |
* In all these cases, <code>false</code> is returned since |
| 141 |
* no prefixes were replaced. |
| 142 |
* </p> |
| 143 |
* @param s the String to insert. |
| 144 |
* @return <code>true</code>if any longer elements have been |
| 145 |
* removed. |
| 146 |
*/ |
| 147 |
public/*synchronized*/boolean insertShorter(String s) { |
| 148 |
boolean replaced = false; |
| 149 |
//check in reverse order since we expect some locality |
| 150 |
for (int i = size - 1; i >= 0; i--) { |
| 151 |
if (s.startsWith(pool[i])) { |
| 152 |
//longer or equal to an existing prefix - nothing to do |
| 153 |
return false; |
| 154 |
} else if (pool[i].startsWith(s)) { |
| 155 |
if (replaced) { |
| 156 |
//replaced before, so shrink the array. |
| 157 |
//Safe since we are iterating in reverse order. |
| 158 |
System.arraycopy(pool, i + 1, pool, i, size - i - 1); |
| 159 |
size--; |
| 160 |
pool[size] = null; |
| 161 |
} else { |
| 162 |
//replace, since this is a shorter s |
| 163 |
pool[i] = s; |
| 164 |
replaced = true; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
if (!replaced) { |
| 169 |
//append at the end |
| 170 |
checkCapacity(); |
| 171 |
pool[size] = s; |
| 172 |
size++; |
| 173 |
} |
| 174 |
return replaced; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Check if the given String s is a prefix of any of Strings |
| 179 |
* in the pool. |
| 180 |
* @param s a s to check for being a prefix |
| 181 |
* @return <code>true</code> if the passed s is a prefix |
| 182 |
* of any of the Strings contained in the pool. |
| 183 |
*/ |
| 184 |
public/*synchronized*/boolean containsAsPrefix(String s) { |
| 185 |
//check in reverse order since we expect some locality |
| 186 |
for (int i = size - 1; i >= 0; i--) { |
| 187 |
if (pool[i].startsWith(s)) { |
| 188 |
return true; |
| 189 |
} |
| 190 |
} |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Test if the String pool contains any one that is a prefix |
| 196 |
* of the given String s. |
| 197 |
* @param s the String to test |
| 198 |
* @return <code>true</code> if the String pool contains a |
| 199 |
* prefix of the given String. |
| 200 |
*/ |
| 201 |
public/*synchronized*/boolean hasPrefixOf(String s) { |
| 202 |
for (int i = size - 1; i >= 0; i--) { |
| 203 |
if (s.startsWith(pool[i])) { |
| 204 |
return true; |
| 205 |
} |
| 206 |
} |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
} |