001/*
002 * flattr4j - A Java library for Flattr
003 *
004 * Copyright (C) 2014 Richard "Shred" Körber
005 *   http://flattr4j.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License / GNU Lesser
009 * General Public License as published by the Free Software Foundation,
010 * either version 3 of the License, or (at your option) any later version.
011 *
012 * Licensed under the Apache License, Version 2.0 (the "License");
013 * you may not use this file except in compliance with the License.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
018 */
019package org.shredzone.flattr4j.async;
020
021import org.shredzone.flattr4j.FlattrFactory;
022import org.shredzone.flattr4j.FlattrService;
023import org.shredzone.flattr4j.connector.RateLimit;
024import org.shredzone.flattr4j.oauth.AccessToken;
025
026/**
027 * Abstract implementation of {@link FlattrCallable}.
028 *
029 * @author Iulius Gutberlet
030 * @author Richard "Shred" Körber
031 */
032public abstract class AbstractFlattrCallable<R> implements FlattrCallable<R> {
033
034    private AccessToken token;
035    private boolean full;
036    private RateLimit rateLimit;
037    private R result;
038
039    @Override
040    public void setAccessToken(AccessToken token) {
041        this.token = token;
042    }
043
044    @Override
045    public void setAccessToken(String token) {
046        setAccessToken(new AccessToken(token));
047    }
048
049    /**
050     * Returns the {@link RateLimit} state after this callable was successfully executed,
051     * or {@code null} if the call failed or was not invoked yet.
052     */
053    public RateLimit getRateLimit() {
054        return rateLimit;
055    }
056
057    /**
058     * Sets the full mode to be used when the call is executed.
059     *
060     * @param full
061     *            full mode
062     */
063    public void setFullMode(boolean full)       { this.full = full; }
064    public boolean isFullMode()                 { return full; }
065
066    /**
067     * Creates a new {@link FlattrService} for the given {@link AccessToken}. Can be
068     * overridden by subclasses if the {@link FlattrService} needs a special
069     * configuration.
070     *
071     * @param token
072     *            {@link AccessToken}
073     * @return {@link FlattrService} that was created
074     */
075    protected FlattrService createFlattrService(AccessToken token) {
076        return FlattrFactory.getInstance().createFlattrService(token);
077    }
078
079    /**
080     * Calls the appropriate method at the service and returns the result.
081     *
082     * @param service
083     *            Preconfigured {@link FlattrService} to be invoked
084     * @return Result returned from the Flattr method.
085     */
086    public abstract R call(FlattrService service) throws Exception; //NOSONAR: accept all exceptions
087
088    @Override
089    public R call() throws Exception {
090        rateLimit = null;
091        FlattrService service = createFlattrService(token);
092        service.setFullMode(full);
093        result = call(service);
094        rateLimit = new RateLimit(service.getLastRateLimit());
095        return result;
096    }
097
098    @Override
099    public R getResult() {
100        return result;
101    }
102
103}