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

net.sf.staccatocommons.util.Strings Maven / Gradle / Ivy

There is a newer version: 2.3
Show newest version
/**
 *  Copyright (c) 2010-2012, The StaccatoCommons Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 */


package net.sf.staccatocommons.util;

import java.util.regex.Pattern;

import net.sf.staccatocommons.defs.function.Function;
import net.sf.staccatocommons.defs.predicate.Predicate;
import net.sf.staccatocommons.lang.function.AbstractFunction;
import net.sf.staccatocommons.lang.function.internal.TopLevelFunction;
import net.sf.staccatocommons.lang.predicate.AbstractPredicate;
import net.sf.staccatocommons.lang.predicate.internal.ContainsSubstringPredicate;
import net.sf.staccatocommons.lang.predicate.internal.EqualsIgnoreCase;
import net.sf.staccatocommons.lang.predicate.internal.Matches;
import net.sf.staccatocommons.lang.predicate.internal.TopLevelPredicate;
import net.sf.staccatocommons.lang.value.NamedTupleToStringStyle;
import net.sf.staccatocommons.restrictions.Constant;
import net.sf.staccatocommons.restrictions.check.NonNull;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * @author flbulgarelli
 * 
 */
public class Strings {

  /**
   * Returns a new {@link Predicate} that tests
   * argument.equalsIgnoreCase(value)
   * 
   * @param value
   * @return a new predicate
   */
  public static Predicate equalsIgnoreCase(@NonNull String value) {
    return new EqualsIgnoreCase(value);
  }

  /**
   * Returns a new {@link Predicate} that tests
   * argument.matches(value)
   * 
   * @param regexp
   * @return a new predicate
   */
  public static Predicate matches(@NonNull String regexp) {
    return new Matches(regexp);
  }

  /**
   * Returns a new {@link Predicate} that tests
   * pattern.matcher(value).matches()
   * 
   * @param pattern
   * @return a new predicate
   */
  public static Predicate matches(@NonNull Pattern pattern) {
    return new Matches(pattern);
  }

  /**
   * Returns a new {@link Predicate} that tests
   * argument.contains(substring)
   * 
   * @param substring
   *          the substring to test if it is contained
   * @return a new predicate
   */
  public static Predicate contains(@NonNull CharSequence substring) {
    return new ContainsSubstringPredicate(substring);
  }

  /**
   * Returns a new {@link Predicate} that answers if its {@link String} argument
   * starts with a given prefix
   * 
   * @param string
   * @return a new Predicate that evaluates args.startsWith(string)
   */
  public static Predicate startsWith(@NonNull final String string) {
    class StartsWith extends AbstractPredicate {
      @Override
      public boolean eval(String args) {
        return args.startsWith(string);
      }
    }
    return new StartsWith();
  }

  /**
   * Returns a {@link Predicate} that tells if its String argument is empty
   * 
   * @return a new {@link Predicate} that evaluates {@code argument.isEmpty()}
   */
  @Constant
  public static Predicate empty() {
    class EmptyStringPredicate extends TopLevelPredicate {
      private static final long serialVersionUID = -7092542419751229862L;

      public boolean eval(String argument) {
        return argument.isEmpty();
      }
    }
    return new EmptyStringPredicate();
  }

  /**
   * Returns a {@link Predicate} that tells if its String argument is not empty
   * 
   * @return a new {@link Predicate} that evaluates {@code !argument.isEmpty()}
   */
  @Constant
  public static Predicate notEmpty() {
    class NotEmptyPredicate extends TopLevelPredicate {
      private static final long serialVersionUID = -6617185455944513316L;

      public boolean eval(String argument) {
        return !argument.isEmpty();
      }
    }
    return new NotEmptyPredicate();
  }

  /**
   * Returns a {@link Function} that answers the char sequence length
   * 
   * @param value
   * @return a constant function
   */
  @Constant
  public static Function length() {
    class StringLengthFunction extends TopLevelFunction {
      private static final long serialVersionUID = -1564325023701893305L;

      public Integer apply(CharSequence arg) {
        return arg.length();
      }
    }
    return new StringLengthFunction();
  }

  /**
   * Returns a function that returns the result of sending
   * {@link Object#toString()} to its argument
   * 
   * @param 
   * @return a function that returns arg.toString()
   */
  @Constant
  public static  Function toString_() {
    return new AbstractFunction() {
      public String apply(A arg) {
        return arg.toString();
      }
    };
  }

  /**
   * Returns a function that returns the result of sending
   * {@link ToStringBuilder#reflectionToString(Object, org.apache.commons.lang.builder.ToStringStyle)}
   * to its argument, using the given {@link ToStringStyle}
   * 
   * @param 
   * @return a function that returns toString() of the argument using reflection
   */
  public static  Function reflectionToString(final ToStringStyle toStringStyle) {
    return new AbstractFunction() {
      public String apply(A arg) {
        return ToStringBuilder.reflectionToString(arg, toStringStyle);
      }
    };
  }

  /**
   * Returns a function that returns the result of sending
   * {@link ToStringBuilder#reflectionToString(Object, org.apache.commons.lang.builder.ToStringStyle)}
   * to its argument, using the {@link NamedTupleToStringStyle}
   * 
   * @param 
   * @return a function that returns toString() of the argument using reflection
   */
  public static  Function reflectionToString() {
    return reflectionToString(NamedTupleToStringStyle.getInstance());
  }
  
  //TODO move toString to Functions

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy