All Downloads are FREE. Search and download functionalities are using the official Maven repository.

brave.sampler.Matchers Maven / Gradle / Ivy

There is a newer version: 6.3.0
Show newest version
/*
 * Copyright The OpenZipkin Authors
 * SPDX-License-Identifier: Apache-2.0
 */
package brave.sampler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * Convenience functions to compose matchers for {@link ParameterizedSampler}.
 *
 * @see Matcher
 * @see ParameterizedSampler
 * @since 5.8
 */
public final class Matchers {

  /** @since 5.8 */
  public static 

Matcher

alwaysMatch() { return (Matcher

) Constants.ALWAYS_MATCH; } /** @since 5.8 */ public static

Matcher

neverMatch() { return (Matcher

) Constants.NEVER_MATCH; } enum Constants implements Matcher { ALWAYS_MATCH { @Override public boolean matches(Object parameters) { return true; } @Override public String toString() { return "matchAll()"; } }, NEVER_MATCH { @Override public boolean matches(Object parameters) { return false; } @Override public String toString() { return "neverMatch()"; } } } /** @since 5.8 */ public static

Matcher

and(Iterable> matchers) { return and(toArray(matchers)); } /** @since 5.8 */ public static

Matcher

and(Matcher

... matchers) { return composite(matchers, true); } /** @since 5.8 */ public static

Matcher

or(Iterable> matchers) { return or(toArray(matchers)); } /** @since 5.8 */ public static

Matcher

or(Matcher

... matchers) { return composite(matchers, false); } static

Matcher[] toArray(Iterable> matchers) { if (matchers == null) throw new NullPointerException("matchers == null"); if (matchers instanceof Collection) { return (Matcher[]) ((Collection) matchers).toArray(new Matcher[0]); } List> result = new ArrayList>(); for (Matcher

matcher : matchers) result.add(matcher); return result.toArray(new Matcher[0]); } static

Matcher

composite(Matcher

[] matchers, boolean and) { if (matchers == null) throw new NullPointerException("matchers == null"); if (matchers.length == 0) return neverMatch(); for (int i = 0; i < matchers.length; i++) { if (matchers[i] == null) throw new NullPointerException("matchers[" + i + "] == null"); } if (matchers.length == 1) return matchers[0]; return and ? new And

(matchers) : new Or

(matchers); } static class And

implements Matcher

{ final Matcher

[] matchers; // Array ensures no iterators are created at runtime And(Matcher

[] matchers) { this.matchers = Arrays.copyOf(matchers, matchers.length); } @Override public boolean matches(P parameters) { for (Matcher

matcher : matchers) { if (!matcher.matches(parameters)) return false; } return true; } @Override public String toString() { return "And(" + Arrays.toString(matchers) + ")"; } } static class Or

implements Matcher

{ final Matcher

[] matchers; // Array ensures no iterators are created at runtime Or(Matcher

[] matchers) { this.matchers = Arrays.copyOf(matchers, matchers.length); } @Override public boolean matches(P parameters) { for (Matcher

matcher : matchers) { if (matcher.matches(parameters)) return true; } return false; } @Override public String toString() { return "Or(" + Arrays.toString(matchers) + ")"; } } }