[英]Java 1.7. implementation of Function
I am using Java 1.7.我正在使用 Java 1.7。 (old I know, but I am working on a legacy application with a lot of dependencies). (老我知道,但我正在开发一个有很多依赖项的遗留应用程序)。
I would like to implement the java.util.function.Function
interface in 1.7.我想在 1.7 中实现java.util.function.Function
接口。
I have the following so far, but am still getting compile errors:到目前为止,我有以下内容,但仍然出现编译错误:
public interface Function<T, R> {
R apply(T t);
public static <T, V, R> Function<V, R> compose(Function<? super V, ? extends T> before, Function<? super T, ? super R> after) {
return new CombiningFunction<T, V, R>(before, after);
}
public static <T, R, V> Function<T, V> andThen(Function<? super T, ? super R> before, Function<? super R, ? extends V> after) {
return new CombiningFunction<T, V, R>(before, after);
}
static <T> Function<T, T> identity() {
return new Function<T, T> {
T apply(T t) { return t; }
}
}
}
class CombiningFunction<T, V, R> implements Function<T, R> {
Function<T, V> first;
Function<V, R> second;
public R apply(T t) {
V intermediate = first.apply(t);
return second.apply(intermediate);
}
}
I need to add a constructor to CombiningFunction
, but I am not sure how to add it, as I have tried the following:我需要向CombiningFunction
添加一个构造函数,但我不确定如何添加它,因为我尝试了以下操作:
public <V, T, R> CombiningFunction(Function<? super V,? extends T> before, Function<? super T,? super R> after) { }
but I get more compile errors on the types when I access the constructor.但是当我访问构造函数时,我会在类型上遇到更多编译错误。
Also, the following has errors:此外,以下有错误:
return new Function<T, T> {
T apply(T t) { return t; }
}
Any help with this Function
interface would be appreciated.对此Function
接口的任何帮助将不胜感激。
Compile Errors:编译错误:
return new CombiningFunction<T, V, R>(before, after);
Expected 0 arguments but found 2预期为 0 arguments 但发现 2
return new CombiningFunction<T, V, R>(before, after);
Expected 0 arguments but found 2预期为 0 arguments 但发现 2
static <T> Function<T, T> identity() {
return new Function<T, T> {
T apply(T t) { return t; }
}
}
'(' or '[' expected ';' expected Cannot resolve symbol 't' Unexpected token '(' 或 '[' 预期 ';' 预期 无法解析符号 't' 意外标记
I would like to use the Function
as follows:我想使用Function
如下:
/**
* Creates Jwt token and other util methods.
*/
public class JwtTokenUtil {
private String SECRET_KEY = "secret-876123";
public String extractUserName(String token) {
return extractClaim(token, new Function<Claims, String>() {
@Override
public String apply(Claims claims) {
return claims.getSubject();
}
});
}
public Date extractExpiration(String token) {
return extractClaim(token, new Function<Claims, Date>() {
@Override
public Date apply(Claims claims) {
return claims.getExpiration();
}
});
}
public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
final Claims claims = extractAllClaims(token);
return claimsResolver.apply(claims);
}
private Claims extractAllClaims(String token) {
return Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody();
}
private Boolean isTokenExpired(String token) {
return extractExpiration(token).before(new Date());
}
/**
* Pass in user details, and get a Jwt
* @param userDetails
* @return
*/
public String generateToken(UserDetails userDetails) {
Map<String,Object> claims = new HashMap<>();
return createToken(claims, userDetails.getUsername());
}
private String createToken(Map<String,Object> claims, String subject) {
return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10)) // 10 hrs
.signWith(SignatureAlgorithm.HS256, SECRET_KEY).compact();
}
public Boolean validteToken(String token, UserDetails userDetails) {
final String username = extractUserName(token);
return (username.equals(userDetails.getUsername()) && !isTokenExpired(token));
}
}
If you go to the OpenJDK repo you can find the source of the Function class.如果您将 go 转到 OpenJDK 存储库,您可以找到 Function class 的来源。 I am not up to date with available features and classes in Java 7 but I think this should fix some of your issues with Generics我不了解 Java 7 中的可用功能和类的最新信息,但我认为这应该可以解决 Generics 的一些问题
https://github.com/openjdk/jdk/blob/6bab0f539fba8fb441697846347597b4a0ade428/src/java.base/share/classes/java/util/function/Function.java https://github.com/openjdk/jdk/blob/6bab0f539fba8fb441697846347597b4a0ade428/src/java.base/share/classes/java/util/function/Function.Z93F725A07423DFE1C88B339
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.