Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#11543] Remove unnecessary EnumerationFilter #11544

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.navercorp.pinpoint.bootstrap.context;

import com.navercorp.pinpoint.common.util.DelegateEnumeration;
import java.util.function.Predicate;

/**
* @author emeroad
Expand Down Expand Up @@ -54,9 +54,10 @@ public static boolean startWithPinpointHeader(final String name) {
return name.regionMatches(true, 0, FILTER_PATTERN_PREFIX, 0, FILTER_PATTERN_PREFIX_LENGTH);
}

public static DelegateEnumeration.Filter FILTER = new DelegateEnumeration.Filter() {
@SuppressWarnings("rawtypes")
public static Predicate FILTER = new Predicate() {
@Override
public boolean filter(Object o) {
public boolean test(Object o) {
if (o instanceof String) {
return startWithPinpointHeader((String) o);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.navercorp.pinpoint.common.util;

import java.util.Enumeration;
import java.util.function.Predicate;

/**
* @author emeroad
Expand All @@ -25,26 +26,17 @@ public class DelegateEnumeration<E> implements Enumeration<E> {
private static final Object NULL_OBJECT = new Object();

private final Enumeration<E> delegate;
private final Filter<E> filter;
private final Predicate<E> filter;

private boolean hasMoreElements;
private E nextElement;
private Exception nextException;


private static final Filter SKIP_FILTER = new Filter() {
@Override
public boolean filter(Object o) {
return false;
}
};

@SuppressWarnings("unchecked")
public DelegateEnumeration(Enumeration<E> delegate) {
this(delegate, SKIP_FILTER);
this(delegate, Predicates.isFalse());
}

public DelegateEnumeration(Enumeration<E> delegate, Filter<E> filter) {
public DelegateEnumeration(Enumeration<E> delegate, Predicate<E> filter) {
this.delegate = delegate;
this.filter = filter;
}
Expand Down Expand Up @@ -109,7 +101,7 @@ private void next(final boolean hasMoreElementMethod) {
break;
}

if (filter.filter(nextElement)) {
if (filter.test(nextElement)) {
continue;
}

Expand All @@ -130,7 +122,4 @@ Exception _getNextException() {
return nextException;
}

public interface Filter<E> {
boolean filter(E e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @author emeroad
*/
public class EmptyEnumeration<E> implements Enumeration<E> {
@SuppressWarnings("rawtypes")
private static final NextElementAction NULL_NEXT_ELEMENT_ACTION = new NullNextElementAction();

private final NextElementAction<E> nextElementAction;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.navercorp.pinpoint.common.util;

import java.util.function.Predicate;


public final class Predicates {
private Predicates() {
}

@SuppressWarnings("rawtypes")
private static final Predicate IS_TRUE = new Predicate() {
@Override
public boolean test(Object o) {
return true;
}

@Override
public String toString() {
return "Predicates.isTrue()";

Check warning on line 19 in commons/src/main/java/com/navercorp/pinpoint/common/util/Predicates.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/com/navercorp/pinpoint/common/util/Predicates.java#L19

Added line #L19 was not covered by tests
}
};

@SuppressWarnings("rawtypes")
private static final Predicate IS_FALSE = new Predicate() {
@Override
public boolean test(Object o) {
return false;
}

@Override
public String toString() {
return "Predicates.isFalse()";

Check warning on line 32 in commons/src/main/java/com/navercorp/pinpoint/common/util/Predicates.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/com/navercorp/pinpoint/common/util/Predicates.java#L32

Added line #L32 was not covered by tests
}
};

@SuppressWarnings("unchecked")
public static <T> Predicate<T> isTrue() {
return (Predicate<T>) IS_TRUE;
}

@SuppressWarnings("unchecked")
public static <T> Predicate<T> isFalse() {
return IS_FALSE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Hashtable;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Predicate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -104,13 +105,10 @@ public void testSkip() {
List<String> valueList = new ArrayList<>(hashTable.values());

Enumeration<String> enumeration = hashTable.elements();
DelegateEnumeration<String> delegateEnumeration = new DelegateEnumeration<>(enumeration, new DelegateEnumeration.Filter<String>() {
DelegateEnumeration<String> delegateEnumeration = new DelegateEnumeration<>(enumeration, new Predicate<String>() {
@Override
public boolean filter(String s) {
if ("bb".equals(s)) {
return true;
}
return false;
public boolean test(String s) {
return "bb".equals(s);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.navercorp.pinpoint.common.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.function.Predicate;


class PredicatesTest {

@Test
void isTrue() {
Predicate<String> predicate = Predicates.isTrue();
Assertions.assertTrue(predicate.test("test"));

Assertions.assertSame(predicate, Predicates.isTrue());
}

@Test
void isFalse() {
Predicate<String> predicate = Predicates.isFalse();
Assertions.assertFalse(predicate.test("test"));

Assertions.assertSame(predicate, Predicates.isFalse());
}

}