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

org.pitest.functional.Prelude Maven / Gradle / Ivy

There is a newer version: 1.16.1
Show newest version
/*
 * Copyright 2010 Henry Coles
 * 
 * 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.pitest.functional;

import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.pitest.functional.predicate.And;
import org.pitest.functional.predicate.Not;
import org.pitest.functional.predicate.Or;
import org.pitest.functional.predicate.Predicate;

/**
 * @author henry
 * 
 */
public abstract class Prelude {

  public final static  And and(final F... ps) {
    return new And(Arrays.asList(ps));
  }

  public final static  And and(final Iterable> ps) {
    return new And(ps);
  }

  public final static  Not not(final F p) {
    return new Not(p);
  }

  public final static  Or or(final Predicate... ps) {
    return new Or(Arrays.asList(ps));
  }

  public final static  Or or(final Iterable> ps) {
    return new Or(ps);
  }

  public static  Predicate isInstanceOf(final Class clazz) {
    return new Predicate() {
      public Boolean apply(final T a) {
        return clazz.isAssignableFrom(a.getClass());
      }
    };
  };

  public final static  SideEffect1 accumulateTo(
      final Collection collection) {
    return new SideEffect1() {

      public void apply(final A a) {
        collection.add(a);
      }

    };

  }

  public static  SideEffect1 putToMap(final Map map,
      final B value) {
    return new SideEffect1() {
      public void apply(final A key) {
        map.put(key, value);
      }
    };
  }

  public static  SideEffect1 putToMap(final Map map,
      final F f) {
    return new SideEffect1() {
      public void apply(final A key) {
        map.put(key, f.apply(key));
      }
    };
  }

  public final static  F id() {
    return new F() {
      public A apply(final A a) {
        return a;
      }
    };
  }

  public final static  F id(final Class type) {
    return id();
  }

  public final static  SideEffect1 print() {
    return printTo(System.out);
  }

  public final static  SideEffect1 print(final Class type) {
    return print();
  }

  public final static  SideEffect1 printTo(final Class type,
      final PrintStream stream) {
    return printTo(stream);
  }

  public final static  SideEffect1 printTo(final PrintStream stream) {
    return new SideEffect1() {
      public void apply(final T a) {
        stream.println(a);
      }
    };
  }

  public static  SideEffect1 printWith(final T t) {
    return new SideEffect1() {
      public void apply(final T a) {
        System.out.println(t + " : " + a);
      }
    };
  }

  public static  Predicate isGreaterThan(final T value) {
    return new Predicate() {
      public Boolean apply(final T o) {
        return o.longValue() > value.longValue();
      }
    };
  }

  public static  Predicate isEqualTo(final T value) {
    return new Predicate() {
      public Boolean apply(final T o) {
        return o.equals(value);
      }
    };
  }

  public static  Predicate isNotNull() {
    return new Predicate() {
      public Boolean apply(final T o) {
        return (o != null);
      }
    };
  }

  public static  Predicate isNull() {
    return new Predicate() {
      public Boolean apply(final T o) {
        return (o == null);
      }
    };
  }

  public static  F asString() {
    return new F() {
      public String apply(final T t) {
        return t.toString();
      }

    };
  }

  public static F2 concatenateWith(
      final String seperator) {
    return new F2() {
      public String apply(final String a, final String b) {
        return a + seperator + b;
      }
    };
  }

  public static  F> asList(final Class type) {
    return new F>() {

      public Iterable apply(final T a) {
        return Collections.singletonList(a);
      }

    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy