Skip to content

Commit 47be984

Browse files
committed
Favor Composition in CompareFilter
Closes gh-1060
1 parent 7951253 commit 47be984

File tree

5 files changed

+73
-13
lines changed

5 files changed

+73
-13
lines changed

core/src/main/java/org/springframework/ldap/filter/CompareFilter.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2013 the original author or authors.
2+
* Copyright 2005-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.ldap.filter;
1818

1919
import org.springframework.ldap.support.LdapEncoder;
20+
import org.springframework.util.Assert;
2021

2122
/**
2223
* Abstract superclass for filters that compare values.
@@ -31,6 +32,12 @@ public abstract class CompareFilter extends AbstractFilter {
3132

3233
private final String encodedValue;
3334

35+
private String operator;
36+
37+
/**
38+
* @deprecated please use the {@code protected} constructor instead
39+
*/
40+
@Deprecated(since = "3.3")
3441
public CompareFilter(String attribute, String value) {
3542
this.attribute = attribute;
3643
this.value = value;
@@ -49,7 +56,9 @@ String getEncodedValue() {
4956
* Override to perform special encoding in subclass.
5057
* @param value the value to encode.
5158
* @return properly escaped value.
59+
* @deprecated please provide the encoded value in the constructor
5260
*/
61+
@Deprecated(forRemoval = true, since = "3.3")
5362
protected String encodeValue(String value) {
5463
return LdapEncoder.filterEncode(value);
5564
}
@@ -58,13 +67,30 @@ protected String encodeValue(String value) {
5867
* Convenience constructor for <code>int</code> values.
5968
* @param attribute Name of attribute in filter.
6069
* @param value The value of the attribute in the filter.
70+
* @deprecated please use the {@code protected} constructor instead
6171
*/
72+
@Deprecated(since = "3.3")
6273
public CompareFilter(String attribute, int value) {
6374
this.attribute = attribute;
6475
this.value = String.valueOf(value);
6576
this.encodedValue = LdapEncoder.filterEncode(this.value);
6677
}
6778

79+
/**
80+
* Construct a filter, specifying the comparison {@code operator} as well as the
81+
* already-encoded value
82+
* @param attribute the attribute name
83+
* @param operator the comparison operator; for example, {@code =}, {@code ~=}
84+
* @param encodedValue the already-encoded value
85+
* @since 3.3
86+
*/
87+
protected CompareFilter(String attribute, String operator, String value, String encodedValue) {
88+
this.attribute = attribute;
89+
this.value = value;
90+
this.encodedValue = encodedValue;
91+
this.operator = operator;
92+
}
93+
6894
/*
6995
* @see org.springframework.ldap.filter.AbstractFilter#encode(java.lang.StringBuffer)
7096
*/
@@ -109,7 +135,13 @@ public int hashCode() {
109135
* {@link EqualsFilter#getCompareString()} would for example return an equals sign,
110136
* &quot;=&quot;.
111137
* @return the String to use as operator in the comparison for the specific subclass.
138+
* @deprecated please specify the operator in the constructor
112139
*/
113-
protected abstract String getCompareString();
140+
@Deprecated(forRemoval = true, since = "3.3")
141+
protected String getCompareString() {
142+
Assert.notNull(this.operator,
143+
"Please supply the operator in the constructor. If needed, override getCompareString instead.");
144+
return this.operator;
145+
}
114146

115147
}

core/src/main/java/org/springframework/ldap/filter/EqualsFilter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,10 @@ public EqualsFilter(String attribute, String value) {
4040
super(attribute, value);
4141
}
4242

43+
EqualsFilter(String attribute, String value, String encodedValue) {
44+
super(attribute, EQUALS_SIGN, value, encodedValue);
45+
}
46+
4347
/**
4448
* Convenience constructor for int values.
4549
* @param attribute Name of attribute in filter.
@@ -49,9 +53,11 @@ public EqualsFilter(String attribute, int value) {
4953
super(attribute, value);
5054
}
5155

52-
/*
56+
/**
57+
* @deprecated please extend {@link CompareFilter} instead
5358
* @see org.springframework.ldap.filter.CompareFilter#getCompareString()
5459
*/
60+
@Deprecated(forRemoval = true, since = "3.3")
5561
protected String getCompareString() {
5662
return EQUALS_SIGN;
5763
}

core/src/main/java/org/springframework/ldap/filter/GreaterThanOrEqualsFilter.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2013 the original author or authors.
2+
* Copyright 2005-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.ldap.filter;
1818

19+
import org.springframework.ldap.support.LdapEncoder;
20+
1921
/**
2022
* A filter to compare &gt;=. LDAP RFC does not allow &gt; comparison. The following code:
2123
*
@@ -37,13 +39,18 @@ public class GreaterThanOrEqualsFilter extends CompareFilter {
3739
private static final String GREATER_THAN_OR_EQUALS = ">=";
3840

3941
public GreaterThanOrEqualsFilter(String attribute, String value) {
40-
super(attribute, value);
42+
super(attribute, GREATER_THAN_OR_EQUALS, value, LdapEncoder.filterEncode(value));
4143
}
4244

4345
public GreaterThanOrEqualsFilter(String attribute, int value) {
44-
super(attribute, value);
46+
this(attribute, String.valueOf(value));
4547
}
4648

49+
/**
50+
* @deprecated please extend {@link CompareFilter} instead
51+
* @see org.springframework.ldap.filter.CompareFilter#getCompareString()
52+
*/
53+
@Deprecated(forRemoval = true, since = "3.3")
4754
protected String getCompareString() {
4855
return GREATER_THAN_OR_EQUALS;
4956
}

core/src/main/java/org/springframework/ldap/filter/LessThanOrEqualsFilter.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2013 the original author or authors.
2+
* Copyright 2005-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.ldap.filter;
1818

19+
import org.springframework.ldap.support.LdapEncoder;
20+
1921
/**
2022
* A filter to compare &lt;=. LDAP RFC does not allow &lt; comparison. The following code:
2123
*
@@ -37,13 +39,18 @@ public class LessThanOrEqualsFilter extends CompareFilter {
3739
private static final String LESS_THAN_OR_EQUALS = "<=";
3840

3941
public LessThanOrEqualsFilter(String attribute, String value) {
40-
super(attribute, value);
42+
super(attribute, LESS_THAN_OR_EQUALS, value, LdapEncoder.filterEncode(value));
4143
}
4244

4345
public LessThanOrEqualsFilter(String attribute, int value) {
44-
super(attribute, value);
46+
this(attribute, String.valueOf(value));
4547
}
4648

49+
/**
50+
* @deprecated please extend {@link CompareFilter} instead
51+
* @see org.springframework.ldap.filter.CompareFilter#getCompareString()
52+
*/
53+
@Deprecated(forRemoval = true, since = "3.3")
4754
protected String getCompareString() {
4855
return LESS_THAN_OR_EQUALS;
4956
}

core/src/main/java/org/springframework/ldap/filter/LikeFilter.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2013 the original author or authors.
2+
* Copyright 2005-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,10 +39,10 @@
3939
public class LikeFilter extends EqualsFilter {
4040

4141
public LikeFilter(String attribute, String value) {
42-
super(attribute, value);
42+
super(attribute, value, encodeValueInternal(value));
4343
}
4444

45-
protected String encodeValue(String value) {
45+
private static String encodeValueInternal(String value) {
4646
// just return if blank string
4747
if (value == null) {
4848
return "";
@@ -65,4 +65,12 @@ protected String encodeValue(String value) {
6565
return buff.toString();
6666
}
6767

68+
/**
69+
* @deprecated please extend {@link CompareFilter} instead
70+
*/
71+
@Deprecated(forRemoval = true, since = "3.3")
72+
protected String encodeValue(String value) {
73+
return encodeValueInternal(value);
74+
}
75+
6876
}

0 commit comments

Comments
 (0)