blob: 1e2dfb01758f1309e7181851be040d0e847f8d55 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
/*******************************************************************************
* Copyright (c) 2010, 2017 Cloudsmith Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Cloudsmith Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.equinox.internal.p2.metadata.expression;
import java.io.Serializable;
/**
* Map a string for an LDAP APPROX (~=) comparison.
* This implementation removes white spaces and transforms everything to lower case.
*/
public final class LDAPApproximation implements Serializable, Comparable<LDAPApproximation> {
private static final long serialVersionUID = 4782295637798543587L;
private final String pattern;
private transient String approxPattern;
public LDAPApproximation(String pattern) {
this.pattern = pattern;
}
@Override
public int compareTo(LDAPApproximation o) {
return pattern.compareTo(o.pattern);
}
@Override
public boolean equals(Object o) {
return o == this || (o instanceof LDAPApproximation && ((LDAPApproximation) o).pattern.equals(pattern));
}
@Override
public int hashCode() {
return 3 * pattern.hashCode();
}
/**
* Matches the <code>value</code> with the compiled expression. The value
* is considered matching if all characters are matched by the expression. A
* partial match is not enough.
* @param value The value to match
* @return <code>true</code> if the value was a match.
*/
public boolean isMatch(CharSequence value) {
if (value == null)
return false;
if (approxPattern == null)
approxPattern = approxString(pattern);
return approxString(value).equals(approxPattern);
}
@Override
public String toString() {
return pattern;
}
private static String approxString(CharSequence input) {
boolean changed = false;
char[] output = new char[input.length()];
int cursor = 0;
for (int i = 0, length = output.length; i < length; i++) {
char c = input.charAt(i);
if (Character.isWhitespace(c)) {
changed = true;
continue;
}
if (Character.isUpperCase(c)) {
changed = true;
c = Character.toLowerCase(c);
}
output[cursor++] = c;
}
return changed ? new String(output, 0, cursor) : input.toString();
}
}
|