Android Open Source - o365api-android-get-events-app In Memory Cache Store






From Project

Back to project page o365api-android-get-events-app.

License

The source code is released under:

MIT License

If you think the Android project o365api-android-get-events-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.mattleib.myinboxapplication;
//from   ww w . j  a v a 2  s  . c  om
import com.microsoft.aad.adal.CacheKey;
import com.microsoft.aad.adal.ITokenCacheStore;
import com.microsoft.aad.adal.ITokenStoreQuery;
import com.microsoft.aad.adal.TokenCacheItem;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

/**
 * Created by mattleib on 1/27/2015.
 */
public class InMemoryCacheStore implements ITokenCacheStore, ITokenStoreQuery {
    private static final long serialVersionUID = 1L;
    private static final String TAG = "InMemoryCacheStore";
    private static Object sLock = new Object();
    HashMap<String, TokenCacheItem> cache = new HashMap<>();

    private static final InMemoryCacheStore INSTANCE = new InMemoryCacheStore();

    private InMemoryCacheStore() {}

    public static InMemoryCacheStore getInstance() {
        return INSTANCE;
    }

    @Override
    public TokenCacheItem getItem(String key) {
        if (key == null) {
            throw new IllegalArgumentException("key");
        }

        return cache.get(key);
    }

    @Override
    public void removeItem(String key) {
        if (key == null) {
            throw new IllegalArgumentException("key");
        }

        cache.remove(key);
    }

    @Override
    public void setItem(String key, TokenCacheItem item) {
        if (key == null) {
            throw new IllegalArgumentException("key");
        }

        if (item == null) {
            throw new IllegalArgumentException("item");
        }

        cache.put(key, item);
    }

    @Override
    public void removeAll() {
        cache = new HashMap<>();
    }

    // Extra helper methods can be implemented here for queries

    /**
     * User can query over iterator values.
     */
    @Override
    public Iterator<TokenCacheItem> getAll() {

        Iterator<TokenCacheItem> values = cache.values().iterator();
        return values;
    }

    /**
     * Unique users with tokens.
     *
     * @return unique users
     */
    @Override
    public HashSet<String> getUniqueUsersWithTokenCache() {
        Iterator<TokenCacheItem> results = this.getAll();
        HashSet<String> users = new HashSet<String>();

        while (results.hasNext()) {
            TokenCacheItem item = results.next();
            if (item.getUserInfo() != null && !users.contains(item.getUserInfo().getUserId())) {
                users.add(item.getUserInfo().getUserId());
            }
        }

        return users;
    }

    /**
     * Tokens for resource.
     *
     * @param resource Resource identifier
     * @return list of {@link TokenCacheItem}
     */
    @Override
    public ArrayList<TokenCacheItem> getTokensForResource(String resource) {
        Iterator<TokenCacheItem> results = this.getAll();
        ArrayList<TokenCacheItem> tokenItems = new ArrayList<TokenCacheItem>();

        while (results.hasNext()) {
            TokenCacheItem item = results.next();
            if (item.getResource().equals(resource)) {
                tokenItems.add(item);
            }
        }

        return tokenItems;
    }

    /**
     * Get tokens for user.
     *
     * @param userid Userid
     * @return list of {@link TokenCacheItem}
     */
    @Override
    public ArrayList<TokenCacheItem> getTokensForUser(String userid) {
        Iterator<TokenCacheItem> results = this.getAll();
        ArrayList<TokenCacheItem> tokenItems = new ArrayList<TokenCacheItem>();

        while (results.hasNext()) {
            TokenCacheItem item = results.next();
            if (item.getUserInfo() != null
                    && item.getUserInfo().getUserId().equalsIgnoreCase(userid)) {
                tokenItems.add(item);
            }
        }

        return tokenItems;
    }

    /**
     * Clear tokens for user without additional retry.
     *
     * @param userid UserId
     */
    @Override
    public void clearTokensForUser(String userid) {
        ArrayList<TokenCacheItem> results = this.getTokensForUser(userid);

        for (TokenCacheItem item : results) {
            if (item.getUserInfo() != null
                    && item.getUserInfo().getUserId().equalsIgnoreCase(userid)) {
                this.removeItem(CacheKey.createCacheKey(item));
            }
        }
    }

    /**
     * Get tokens about to expire.
     *
     * @return list of {@link TokenCacheItem}
     */
    @Override
    public ArrayList<TokenCacheItem> getTokensAboutToExpire() {
        Iterator<TokenCacheItem> results = this.getAll();
        ArrayList<TokenCacheItem> tokenItems = new ArrayList<TokenCacheItem>();

        while (results.hasNext()) {
            TokenCacheItem item = results.next();
            if (isAboutToExpire(item.getExpiresOn())) {
                tokenItems.add(item);
            }
        }

        return tokenItems;
    }

    private boolean isAboutToExpire(Date expires) {
        Date validity = getTokenValidityTime().getTime();

        if (expires != null && expires.before(validity)) {
            return true;
        }

        return false;
    }

    private static final int TOKEN_VALIDITY_WINDOW = 10;

    private static Calendar getTokenValidityTime() {
        Calendar timeAhead = Calendar.getInstance();
        timeAhead.add(Calendar.SECOND, TOKEN_VALIDITY_WINDOW);
        return timeAhead;
    }

    @Override
    public boolean contains(String key) {
        if (key == null) {
            throw new IllegalArgumentException("key");
        }

        return cache.containsKey(key);
    }
}




Java Source Code List

com.example.mattleib.myinboxapplication.AppConfig.java
com.example.mattleib.myinboxapplication.AppHelper.java
com.example.mattleib.myinboxapplication.ApplicationTest.java
com.example.mattleib.myinboxapplication.Constants.java
com.example.mattleib.myinboxapplication.DataTypes.java
com.example.mattleib.myinboxapplication.EmailAddress.java
com.example.mattleib.myinboxapplication.EmptyItem.java
com.example.mattleib.myinboxapplication.EventItem.java
com.example.mattleib.myinboxapplication.EventItemsFragment.java
com.example.mattleib.myinboxapplication.Helpers.java
com.example.mattleib.myinboxapplication.InMemoryCacheStore.java
com.example.mattleib.myinboxapplication.Item.java
com.example.mattleib.myinboxapplication.LocalDateTimeConverter.java
com.example.mattleib.myinboxapplication.Location.java
com.example.mattleib.myinboxapplication.MainActivity.java
com.example.mattleib.myinboxapplication.Organizer.java
com.example.mattleib.myinboxapplication.PreferenceSetting.java
com.example.mattleib.myinboxapplication.PreferenceSettings.java
com.example.mattleib.myinboxapplication.SectionItem.java
com.example.mattleib.myinboxapplication.SettingsActivity.java
com.example.mattleib.myinboxapplication.SettingsFragment.java
com.example.mattleib.myinboxapplication.SimpleAlertDialog.java