简体   繁体   中英

Java 1.7. implementation of Function

I am using 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.

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:

 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.

Compile Errors:

return new CombiningFunction<T, V, R>(before, after);

Expected 0 arguments but found 2

return new CombiningFunction<T, V, R>(before, after);

Expected 0 arguments but found 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

I would like to use the Function as follows:

/**
 * 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. 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

https://github.com/openjdk/jdk/blob/6bab0f539fba8fb441697846347597b4a0ade428/src/java.base/share/classes/java/util/function/Function.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM