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

org.terracotta.context.query.Matchers Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
/*
 * All content copyright Terracotta, Inc., unless otherwise indicated.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terracotta.context.query;

import java.util.Map;

import org.terracotta.context.ContextElement;
import org.terracotta.context.TreeNode;

/**
 * A static library of {@link Matcher} implementations that can be used with the
 * {@link QueryBuilder#filter(Matcher)} method.
 */
public final class Matchers {
  
  private Matchers() {
    //static class
  }

  /**
   * Returns a matcher that matches tree nodes whose {@link TreeNode#getContext()}
   * match against the supplied matcher.
   * 
   * @param matcher {@code ContextElement} matcher
   * @return a {@code TreeNode} matcher
   */
  public static Matcher context(final Matcher matcher) {
    return new Matcher() {

      @Override
      protected boolean matchesSafely(TreeNode t) {
        return matcher.matches(t.getContext());
      }

      @Override
      public String toString() {
        return "a context that has " + matcher;
      }
    };
  }
  
  /**
   * Returns a matcher that matches context elements whose {@link ContextElement#attributes()}
   * match against the supplied matcher.
   * 
   * @param matcher a {@code Map} (attributes) matcher
   * @return a {@code ContextElement} matcher
   */
  public static Matcher attributes(final Matcher> matcher) {
    return new Matcher() {

      @Override
      protected boolean matchesSafely(ContextElement t) {
        return matcher.matches(t.attributes());
      }

      @Override
      public String toString() {
        return "an attributes " + matcher;
      }
    };
  }
  
  /**
   * Returns a matcher that matches context elements whose {@link ContextElement#identifier()}
   * match against the supplied matcher.
   * 
   * @param matcher {@code Class} matcher
   * @return a {@code ContextElement} matcher
   */
  public static Matcher identifier(final Matcher> matcher) {
    return new Matcher() {

      @Override
      protected boolean matchesSafely(ContextElement t) {
        return matcher.matches(t.identifier());
      }

      @Override
      public String toString() {
        return "an identifier that is " + matcher;
      }
    };
    
  }
  
  /**
   * Returns a matcher that matches classes that are sub-types of the  supplied
   * class.
   * 
   * @param klazz a potential super-type
   * @return a {@code Class} matcher
   */
  public static Matcher> subclassOf(final Class klazz) {
    return new Matcher>() {

      @Override
      protected boolean matchesSafely(Class t) {
        return klazz.isAssignableFrom(t);
      }

      @Override
      public String toString() {
        return "a subtype of " + klazz;
      }
    };
  }
  
  /**
   * Returns a matcher that matches attribute maps that include the given
   * attribute entry.
   * 
   * @param key attribute name
   * @param value attribute value
   * @return a {@code Map} matcher
   */
  public static Matcher> hasAttribute(final String key, final Object value) {
    return new Matcher>() {

      @Override
      protected boolean matchesSafely(Map object) {
        return object.containsKey(key) && value.equals(object.get(key));
      }
    };
  }
  
  /**
   * Returns a matcher that matches attribute maps the include an attribute with
   * the given name, and whose value matches the given matcher.
   * 
   * @param key attribute name
   * @param value attribute value matcher
   * @return a {@code Map} matcher
   */
  public static Matcher> hasAttribute(final String key, final Matcher value) {
    return new Matcher>() {

      @Override
      protected boolean matchesSafely(Map object) {
        return object.containsKey(key) && value.matches(object.get(key));
      }
    };
  }
  
  /**
   * Returns a matcher that matches when against objects which match any
   * of the supplied matchers.
   * 
   * @param  type of the object to be matched
   * @param matchers list of matchers to match
   * @return a compound matcher
   */
  public static  Matcher anyOf(final Matcher ... matchers) {
    return new Matcher() {

      @Override
      protected boolean matchesSafely(T object) {
        for (Matcher matcher : matchers) {
          if (matcher.matches(object)) {
            return true;
          }
        }
        return false;
      }
    };
  }

  /**
   * Returns a matcher that matches when against objects which match all
   * of the supplied matchers.
   * 
   * @param  type of the object to be matched
   * @param matchers list of matchers to match
   * @return a compound matcher
   */
  public static  Matcher allOf(final Matcher ... matchers) {
    return new Matcher() {

      @Override
      protected boolean matchesSafely(T object) {
        for (Matcher matcher : matchers) {
          if (!matcher.matches(object)) {
            return false;
          }
        }
        return true;
      }
    };
  }
  
  public static  Matcher not(final Matcher matcher) {
    return new Matcher() {

      @Override
      protected boolean matchesSafely(T object) {
        return !matcher.matches(object);
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy