Android Open Source - BusWear Exception To Resource Mapping






From Project

Back to project page BusWear.

License

The source code is released under:

Apache License

If you think the Android project BusWear 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 pl.tajchert.buswear.util;
//w  ww. j a v a 2 s  . co  m
import android.util.Log;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import pl.tajchert.buswear.EventBus;


/**
 * Maps throwables to texts for error dialogs. Use Config to configure the mapping.
 * 
 * @author Markus
 */
public class ExceptionToResourceMapping {

    public final Map<Class<? extends Throwable>, Integer> throwableToMsgIdMap;

    public ExceptionToResourceMapping() {
        throwableToMsgIdMap = new HashMap<Class<? extends Throwable>, Integer>();
    }

    /** Looks at the exception and its causes trying to find an ID. */
    public Integer mapThrowable(final Throwable throwable) {
        Throwable throwableToCheck = throwable;
        int depthToGo = 20;

        while (true) {
            Integer resId = mapThrowableFlat(throwableToCheck);
            if (resId != null) {
                return resId;
            } else {
                throwableToCheck = throwableToCheck.getCause();
                depthToGo--;
                if (depthToGo <= 0 || throwableToCheck == throwable || throwableToCheck == null) {
                    Log.d(EventBus.TAG, "No specific message ressource ID found for " + throwable);
                    // return config.defaultErrorMsgId;
                    return null;
                }
            }
        }

    }

    /** Mapping without checking the cause (done in mapThrowable). */
    protected Integer mapThrowableFlat(Throwable throwable) {
        Class<? extends Throwable> throwableClass = throwable.getClass();
        Integer resId = throwableToMsgIdMap.get(throwableClass);
        if (resId == null) {
            Class<? extends Throwable> closestClass = null;
            Set<Entry<Class<? extends Throwable>, Integer>> mappings = throwableToMsgIdMap.entrySet();
            for (Entry<Class<? extends Throwable>, Integer> mapping : mappings) {
                Class<? extends Throwable> candidate = mapping.getKey();
                if (candidate.isAssignableFrom(throwableClass)) {
                    if (closestClass == null || closestClass.isAssignableFrom(candidate)) {
                        closestClass = candidate;
                        resId = mapping.getValue();
                    }
                }
            }

        }
        return resId;
    }

    public ExceptionToResourceMapping addMapping(Class<? extends Throwable> clazz, int msgId) {
        throwableToMsgIdMap.put(clazz, msgId);
        return this;
    }

}




Java Source Code List

pl.tajchert.buswear.AsyncPoster.java
pl.tajchert.buswear.BackgroundPoster.java
pl.tajchert.buswear.EventBusBuilder.java
pl.tajchert.buswear.EventBusException.java
pl.tajchert.buswear.EventBus.java
pl.tajchert.buswear.HandlerPoster.java
pl.tajchert.buswear.NoSubscriberEvent.java
pl.tajchert.buswear.PendingPostQueue.java
pl.tajchert.buswear.PendingPost.java
pl.tajchert.buswear.SubscriberExceptionEvent.java
pl.tajchert.buswear.SubscriberMethodFinder.java
pl.tajchert.buswear.SubscriberMethod.java
pl.tajchert.buswear.Subscription.java
pl.tajchert.buswear.ThreadMode.java
pl.tajchert.buswear.sample.CustomObject.java
pl.tajchert.buswear.sample.CustomObject.java
pl.tajchert.buswear.sample.MainMobileActivity.java
pl.tajchert.buswear.sample.MainWearActivity.java
pl.tajchert.buswear.util.AsyncExecutor.java
pl.tajchert.buswear.util.ErrorDialogConfig.java
pl.tajchert.buswear.util.ErrorDialogFragmentFactory.java
pl.tajchert.buswear.util.ErrorDialogFragments.java
pl.tajchert.buswear.util.ErrorDialogManager.java
pl.tajchert.buswear.util.ExceptionToResourceMapping.java
pl.tajchert.buswear.util.HasExecutionScope.java
pl.tajchert.buswear.util.ThrowableFailureEvent.java
pl.tajchert.buswear.wear.EventCatcher.java
pl.tajchert.buswear.wear.SendByteArrayToNode.java
pl.tajchert.buswear.wear.SendCommandToNode.java
pl.tajchert.buswear.wear.SendWearManager.java
pl.tajchert.buswear.wear.WearBusTools.java