我正在将 Nat Pryce 的Maybe
课程翻译成 Delphi。
这里有一些背景: http: //www.natpryce.com/articles/000776.html
大部分都很简单,但是......
我如何将这个声明从 Java 翻译成 Delphi?
public abstract <U> Maybe<U> to(Function<? super T, ? extends U> mapping);
这是一个抽象的虚函数&to
,它接受一个function
命名mapping
为参数,但我不知道如何建模。
对于上下文,这是完整的代码:
package com.natpryce.maybe;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import java.util.Collections;
import java.util.Iterator;
public abstract class Maybe<T> implements Iterable<T> {
public abstract boolean isKnown();
public abstract T otherwise(T defaultValue);
public abstract Maybe<T> otherwise(Maybe<T> maybeDefaultValue);
public abstract <U> Maybe<U> to(Function<? super T, ? extends U> mapping);
public abstract Maybe<Boolean> query(Predicate<? super T> mapping);
public static <T> Maybe<T> unknown() {
return new Maybe<T>() {
@Override
public boolean isKnown() {
return false;
}
public Iterator<T> iterator() {
return Collections.<T>emptyList().iterator();
}
@Override
public T otherwise(T defaultValue) {
return defaultValue;
}
@Override
public Maybe<T> otherwise(Maybe<T> maybeDefaultValue) {
return maybeDefaultValue;
}
@Override
public <U> Maybe<U> to(Function<? super T, ? extends U> mapping) {
return unknown();
}
@Override
public Maybe<Boolean> query(Predicate<? super T> mapping) {
return unknown();
}
@Override
public String toString() {
return "unknown";
}
@Override
@SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"})
public boolean equals(Object obj) {
return false;
}
@Override
public int hashCode() {
return 0;
}
};
}
public static <T> Maybe<T> definitely(final T theValue) {
return new DefiniteValue<T>(theValue);
}
private static class DefiniteValue<T> extends Maybe<T> {
private final T theValue;
public DefiniteValue(T theValue) {
this.theValue = theValue;
}
@Override
public boolean isKnown() {
return true;
}
public Iterator<T> iterator() {
return Collections.singleton(theValue).iterator();
}
@Override
public T otherwise(T defaultValue) {
return theValue;
}
@Override
public Maybe<T> otherwise(Maybe<T> maybeDefaultValue) {
return this;
}
@Override
public <U> Maybe<U> to(Function<? super T, ? extends U> mapping) {
return definitely(mapping.apply(theValue));
}
@Override
public Maybe<Boolean> query(Predicate<? super T> mapping) {
return definitely(mapping.apply(theValue));
}
@Override
public String toString() {
return "definitely " + theValue.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DefiniteValue<?> that = (DefiniteValue<?>) o;
return theValue.equals(that.theValue);
}
@Override
public int hashCode() {
return theValue.hashCode();
}
}
}