001/*
002 * flattr4j - A Java library for Flattr
003 *
004 * Copyright (C) 2011 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;
020
021import java.util.concurrent.atomic.AtomicReference;
022
023import org.shredzone.flattr4j.connector.impl.FlattrConnector;
024import org.shredzone.flattr4j.impl.FlattrServiceImpl;
025import org.shredzone.flattr4j.oauth.AccessToken;
026
027/**
028 * A factory class that makes creation of the Flattr services as easy as possible.
029 *
030 * @author Richard "Shred" Körber
031 */
032public final class FlattrFactory {
033    private static final AtomicReference<FlattrFactory> instance = new AtomicReference<FlattrFactory>();
034
035    private FlattrFactory() {
036        // Private constructor
037    }
038
039    /**
040     * Retrieves an instance of the {@link FlattrFactory}. The factory is a singleton.
041     *
042     * @return {@link FlattrFactory}
043     */
044    public static FlattrFactory getInstance() {
045        FlattrFactory result = instance.get();
046        if (result != null) {
047            return result;
048        }
049
050        result = new FlattrFactory();
051
052        if (instance.compareAndSet(null, result)) {
053            return result;
054        } else {
055            return instance.get();
056        }
057    }
058
059    /**
060     * Creates a {@link FlattrService} for the given access token. The returned service is
061     * associated to the user of the access token.
062     *
063     * @param accessToken
064     *            Access token
065     * @return Created {@link FlattrService}
066     * @since 2.0
067     */
068    public FlattrService createFlattrService(String accessToken) {
069        return createFlattrService(new AccessToken(accessToken));
070    }
071
072    /**
073     * Creates a {@link FlattrService} for the given access token. The returned service is
074     * associated to the user of the access token.
075     *
076     * @param accessToken
077     *            {@link AccessToken} instance
078     * @return Created {@link FlattrService}
079     * @since 2.0
080     */
081    public FlattrService createFlattrService(AccessToken accessToken) {
082        FlattrConnector connector = new FlattrConnector();
083        connector.setAccessToken(accessToken);
084        return new FlattrServiceImpl(connector);
085    }
086
087    /**
088     * Creates a {@link FlattrService} that is only able to execute commands without
089     * scope.
090     *
091     * @return Created {@link FlattrService}
092     * @since 2.9
093     */
094    public FlattrService createFlattrService() {
095        return new FlattrServiceImpl(new FlattrConnector());
096    }
097
098}