Joined July 2015
·

Emmanuel Essien-nta

Nigeria
·
·

Kikikiki... wicked laugh

We can do away with the excessive switch statements and make it easier to add more log states by taking advantage of some OOP thingys. Something like this:

package com.coderwall.logstate;
interface LogStateIF{
void log(String tag, String msg);
void log(Class<?> cls, String tag, String msg);
void log(Class<?> cls, String msg);
}

package com.coderwall.logstate;
class LogStateD implements LogStateIF{

    @Override
    public void log(String tag, String msg){
        Log.d(tag, msg);
    }

    @Override
    public void log(Class<?> cls, String tag, String msg){
        Log.d(tag, cls.getName() + " "+ msg);
    }

    @Override
    public void log(Class<?> cls, String msg){
        Log.d(TAG, cls.getName() + " "+ msg);
    }
}

package com.coderwall.logstate;
class LogStateW implements LogStateIF{

    @Override
    public void log(String tag, String msg){
        Log.w(tag, msg);
    }

    @Override
    public void log(Class<?> cls, String tag, String msg){
        Log.w(tag, cls.getName() + " "+ msg);
    }

    @Override
    public void log(Class<?> cls, String msg){
        Log.w(TAG, cls.getName() + " "+ msg);
    }
}

package com.coderwall.logstate;
class LogStateE implements LogStateIF{

    @Override
    public void log(String tag, String msg){
        Log.e(tag, msg);
    }

    @Override
    public void log(Class<?> cls, String tag, String msg){
        Log.e(tag, cls.getName() + " "+ msg);
    }

    @Override
    public void log(Class<?> cls, String msg){
        Log.e(TAG, cls.getName() + " "+ msg);
    }
}

package com.coderwall.logstate;
class LogStateI implements LogStateIF{

    @Override
    public void log(String tag, String msg){
        Log.i(tag, msg);
    }

    @Override
    public void log(Class<?> cls, String tag, String msg){
        Log.i(tag, cls.getName() + " "+ msg);
    }

    @Override
    public void log(Class<?> cls, String msg){
        Log.i(TAG, cls.getName() + " "+ msg);
    }
}

package com.coderwall.logstate;
public enum LogState{
    D(new LogStateD()),
    I(new LogStateI()),
    W(new LogStateW()),
    E(new LogStateE());

    private final LogStateIF implementation;
    private LogState(LogStateIF ls){
        this.implementation = ls;
    }

    public LogStateIF getImplementation(){
        return implementation;
    }
}


Sigh! Long code.. all in the name of OOP :)
Then an Adapter class that makes use of our work:

package com.coderwall.logstate;
public class LogStateAdapter{
public static void log(LogState ls, String tag, String msg){
    ls.getImplementation().log(tag, msg);
}

public static void log(Class<?> cls, LogState ls, String tag, String msg){
    ls.getImplementation().log(cls, tag, msg);
}

public static void log(Class<?> cls, LogState ls, String msg){
    ls.getImplementation().log(cls, msg);
}
}
Achievements
1 Karma
0 Total ProTip Views