Favor Composition in CompareFilter

Closes gh-1060
This commit is contained in:
Josh Cummings
2025-04-09 14:14:29 -06:00
parent 7951253454
commit 47be9845bb
5 changed files with 73 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2013 the original author or authors.
* Copyright 2005-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.ldap.filter;
import org.springframework.ldap.support.LdapEncoder;
import org.springframework.util.Assert;
/**
* Abstract superclass for filters that compare values.
@@ -31,6 +32,12 @@ public abstract class CompareFilter extends AbstractFilter {
private final String encodedValue;
private String operator;
/**
* @deprecated please use the {@code protected} constructor instead
*/
@Deprecated(since = "3.3")
public CompareFilter(String attribute, String value) {
this.attribute = attribute;
this.value = value;
@@ -49,7 +56,9 @@ public abstract class CompareFilter extends AbstractFilter {
* Override to perform special encoding in subclass.
* @param value the value to encode.
* @return properly escaped value.
* @deprecated please provide the encoded value in the constructor
*/
@Deprecated(forRemoval = true, since = "3.3")
protected String encodeValue(String value) {
return LdapEncoder.filterEncode(value);
}
@@ -58,13 +67,30 @@ public abstract class CompareFilter extends AbstractFilter {
* Convenience constructor for <code>int</code> values.
* @param attribute Name of attribute in filter.
* @param value The value of the attribute in the filter.
* @deprecated please use the {@code protected} constructor instead
*/
@Deprecated(since = "3.3")
public CompareFilter(String attribute, int value) {
this.attribute = attribute;
this.value = String.valueOf(value);
this.encodedValue = LdapEncoder.filterEncode(this.value);
}
/**
* Construct a filter, specifying the comparison {@code operator} as well as the
* already-encoded value
* @param attribute the attribute name
* @param operator the comparison operator; for example, {@code =}, {@code ~=}
* @param encodedValue the already-encoded value
* @since 3.3
*/
protected CompareFilter(String attribute, String operator, String value, String encodedValue) {
this.attribute = attribute;
this.value = value;
this.encodedValue = encodedValue;
this.operator = operator;
}
/*
* @see org.springframework.ldap.filter.AbstractFilter#encode(java.lang.StringBuffer)
*/
@@ -109,7 +135,13 @@ public abstract class CompareFilter extends AbstractFilter {
* {@link EqualsFilter#getCompareString()} would for example return an equals sign,
* &quot;=&quot;.
* @return the String to use as operator in the comparison for the specific subclass.
* @deprecated please specify the operator in the constructor
*/
protected abstract String getCompareString();
@Deprecated(forRemoval = true, since = "3.3")
protected String getCompareString() {
Assert.notNull(this.operator,
"Please supply the operator in the constructor. If needed, override getCompareString instead.");
return this.operator;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,6 +40,10 @@ public class EqualsFilter extends CompareFilter {
super(attribute, value);
}
EqualsFilter(String attribute, String value, String encodedValue) {
super(attribute, EQUALS_SIGN, value, encodedValue);
}
/**
* Convenience constructor for int values.
* @param attribute Name of attribute in filter.
@@ -49,9 +53,11 @@ public class EqualsFilter extends CompareFilter {
super(attribute, value);
}
/*
/**
* @deprecated please extend {@link CompareFilter} instead
* @see org.springframework.ldap.filter.CompareFilter#getCompareString()
*/
@Deprecated(forRemoval = true, since = "3.3")
protected String getCompareString() {
return EQUALS_SIGN;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2013 the original author or authors.
* Copyright 2005-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.ldap.filter;
import org.springframework.ldap.support.LdapEncoder;
/**
* A filter to compare &gt;=. LDAP RFC does not allow &gt; comparison. The following code:
*
@@ -37,13 +39,18 @@ public class GreaterThanOrEqualsFilter extends CompareFilter {
private static final String GREATER_THAN_OR_EQUALS = ">=";
public GreaterThanOrEqualsFilter(String attribute, String value) {
super(attribute, value);
super(attribute, GREATER_THAN_OR_EQUALS, value, LdapEncoder.filterEncode(value));
}
public GreaterThanOrEqualsFilter(String attribute, int value) {
super(attribute, value);
this(attribute, String.valueOf(value));
}
/**
* @deprecated please extend {@link CompareFilter} instead
* @see org.springframework.ldap.filter.CompareFilter#getCompareString()
*/
@Deprecated(forRemoval = true, since = "3.3")
protected String getCompareString() {
return GREATER_THAN_OR_EQUALS;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2013 the original author or authors.
* Copyright 2005-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.ldap.filter;
import org.springframework.ldap.support.LdapEncoder;
/**
* A filter to compare &lt;=. LDAP RFC does not allow &lt; comparison. The following code:
*
@@ -37,13 +39,18 @@ public class LessThanOrEqualsFilter extends CompareFilter {
private static final String LESS_THAN_OR_EQUALS = "<=";
public LessThanOrEqualsFilter(String attribute, String value) {
super(attribute, value);
super(attribute, LESS_THAN_OR_EQUALS, value, LdapEncoder.filterEncode(value));
}
public LessThanOrEqualsFilter(String attribute, int value) {
super(attribute, value);
this(attribute, String.valueOf(value));
}
/**
* @deprecated please extend {@link CompareFilter} instead
* @see org.springframework.ldap.filter.CompareFilter#getCompareString()
*/
@Deprecated(forRemoval = true, since = "3.3")
protected String getCompareString() {
return LESS_THAN_OR_EQUALS;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2013 the original author or authors.
* Copyright 2005-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,10 +39,10 @@ import org.springframework.ldap.support.LdapEncoder;
public class LikeFilter extends EqualsFilter {
public LikeFilter(String attribute, String value) {
super(attribute, value);
super(attribute, value, encodeValueInternal(value));
}
protected String encodeValue(String value) {
private static String encodeValueInternal(String value) {
// just return if blank string
if (value == null) {
return "";
@@ -65,4 +65,12 @@ public class LikeFilter extends EqualsFilter {
return buff.toString();
}
/**
* @deprecated please extend {@link CompareFilter} instead
*/
@Deprecated(forRemoval = true, since = "3.3")
protected String encodeValue(String value) {
return encodeValueInternal(value);
}
}