br.shura.team.mpsbot.venusext.Helper.java Source code

Java tutorial

Introduction

Here is the source code for br.shura.team.mpsbot.venusext.Helper.java

Source

//////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016, Joo Vitor Verona Biazibetti - All Rights Reserved                /
//                                                                                       /
// Licensed under the GNU General Public License v3;                                     /
// you may not use this file except in compliance with the License.                      /
//                                                                                       /
// You may obtain a copy of the License at                                               /
//     http://www.gnu.org/licenses/gpl.html                                              /
//                                                                                       /
// Unless required by applicable law or agreed to in writing, software                   /
// distributed under the License is distributed on an "AS IS" BASIS,                     /
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.              /
// See the License for the specific language governing permissions and                   /
// limitations under the License.                                                        /
//                                                                                       /
// Written by Joo Vitor Verona Biazibetti <joaaoverona@gmail.com>, March 2016           /
// https://www.github.com/BloodShura                                                     /
//////////////////////////////////////////////////////////////////////////////////////////

package br.shura.team.mpsbot.venusext;

import br.shura.team.mpsbot.control.TaskExecutionListener;
import br.shura.venus.exception.runtime.ScriptRuntimeException;
import br.shura.venus.executor.Context;
import br.shura.x.math.number.BaseConverter;
import twitter4j.TwitterException;

/**
 * Helper.java
 *
 * @author <a href="https://www.github.com/BloodShura">BloodShura</a> (Joo Vitor Verona Biazibetti)
 * @contact joaaoverona@gmail.com
 * @date 23/05/16 - 20:20
 * @since GAMMA - 0x3
 */
public class Helper {
    public static final String PREFIX = "[TWITTER ERROR]";
    public static final String REGEX_PREFIX = "\\[TWITTER ERROR\\]";

    public static boolean execute(Context context, TwitterRunnable runnable) throws ScriptRuntimeException {
        try {
            runnable.run();

            return true;
        } catch (TwitterException exception) {
            TaskExecutionListener listener = context.getApplicationContext().getUserData("listener",
                    TaskExecutionListener.class);

            listener.scriptOutput(
                    PREFIX + " Code: 0x" + BaseConverter.encode(exception.getErrorCode(), BaseConverter.HEXADECIMAL)
                            + " | Message: \"" + exception.getErrorMessage() + "\"\n");

            return false;
        }
    }

    public static <E> E execute(Context context, TwitterSupplier<E> supplier) throws ScriptRuntimeException {
        try {
            return supplier.run();
        } catch (TwitterException exception) {
            TaskExecutionListener listener = context.getApplicationContext().getUserData("listener",
                    TaskExecutionListener.class);

            listener.scriptOutput(
                    PREFIX + " Code: 0x" + BaseConverter.encode(exception.getErrorCode(), BaseConverter.HEXADECIMAL)
                            + " | Message: \"" + exception.getErrorMessage() + "\"\n");

            return null;
        }
    }

    public interface TwitterRunnable {
        void run() throws TwitterException;
    }

    public interface TwitterSupplier<E> {
        E run() throws TwitterException;
    }
}