Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
J2SE 5.0 Generic应用一:类型安全的functor

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

一、简介

    函数式编程是非常常用和非常重要的一种编程范式,有的语言直接提供支持,C++则通过()运算符重载和模板提供了还算灵活的支持,而Java中的函数式编程则由于语言本身的局限没有得到广泛应用,Apache Commons Functor 项目是一个正在开发中的函数式编程库,但目前看来并不是类型安全的;J2SE 5.0提供了有限的generic能力,除了用于Collection之外,类型安全的functor也是其用武之地,已有一个开源项目Generic Algorithms for Java开始了这方面的工作

二、示例

  • 一元函数、谓词、过程

public interface UnaryFunction<R, P> {
    R evaluate(P obj);
}

public interface UnaryPredicate<T> {
    boolean test(T obj);
}

public interface UnaryProcedure<T> {
    void run(T obj);
}

  • 二元函数、谓词、过程

public interface BinaryFunction<R, T, S> {
    R evaluate(T left, S right);
}

public interface BinaryPredicate<T, S> {
    boolean test(T left, S right);
}

public interface BinaryProcedure<T, S> {
    void run(T left, S right);
}

  • 特化一:过滤

public interface Filter<T> extends UnaryFunction<T, T>{
}

  • 几个示例算法:transform、select、foreach

public static <Destination, Source> List<Destination> transform(Collection<Source> source, UnaryFunction<Destination, Source> transformer){
    List<Destination> result = new ArrayList<Destination>();
    for(Source item : source){
        result.add(transformer.evaluate(item));
    }
    return result;
}

public static <T> List<T> select(Collection<T> source, UnaryPredicate<T> selector){
    List<T> result = new ArrayList<T>();
    for(T item : source){
        if(selector.test(item)){
            result.add(item);
        }
    }
    return result;
}

public static <T> void foreach(Collection<T> source, UnaryProcedure<T> procedure){
    for(T item : source){
        procedure.run(item);
    }
}

  • 几个composite:And、Or、Not

public class And<T> implements UnaryPredicate<T>, Serializable{
    private UnaryPredicate<T>[] predicates;
    public And(UnaryPredicate<T>... predicates){
        this.predicates = predicates;
    }
    public boolean test(T obj) {
        for(UnaryPredicate<T> predicate : predicates){
            if( !predicate.test(obj) ){
                return false;
            }
        }
        return true;
    }
    public static <T> And<T> and(UnaryPredicate<T>... predicates){
        return new And<T>(predicates);
    }
}

public class Or<T> implements UnaryPredicate<T>, Serializable{
    private UnaryPredicate<T>[] predicates;
    public Or(UnaryPredicate<T>... predicates){
        this.predicates = predicates;
    }
    public boolean test(T obj) {
        for(UnaryPredicate<T> predicate : predicates){
            if( predicate.test(obj) ){
                return true;
            }
        }
        return false;
    }
    public static <T> Or<T> or(UnaryPredicate<T>... predicates){
        return new Or<T>(predicates);
    }
}

public class Not<T> implements UnaryPredicate<T>, Serializable{
    private UnaryPredicate<T> predicate;
    public Not(UnaryPredicate<T> predicate){
        this.predicate = predicate;
    }
    public boolean test(T obj) {
        return !predicate.test(obj);
    }
    public static <T> Not<T> not(UnaryPredicate<T> predicate){
        return new Not<T>(predicate);
    }
}




相关文章

相关软件