zoukankan      html  css  js  c++  java
  • [Java]如何制作一个WordCount-Plus的Plugin

    主类

    每个Plugin都有一个主类实现了com.jihuayu.wordcount.api.Plugin接口,这个主类就是插件的路口。

    获取命令介绍

    可以通过向方法getCommandUsage的参数info添加String的方法添加你的方法介绍。

    获取命令名字

    可以通过方法getCommandName的返回值来指定命令名字。

    • 若返回null则表示只进行初始化,不出现在指令区。
    • 若返回“”代表无参数时执行该代码的。

    设置命令执行代码

    可以通过设置doCommand方法来设置执行时的代码,其中doCommand的参数为针对该指令传入的参数。

    事件

    WordCount-Plus默认自带了4个事件,分别是:

    ReadyEvent

    准备完成时触发。

    ReadEvent

    每次读取完成时触发。

    ReadOverEvent

    全部文本读取完成时触发。

    WriteEvent

    每次需要输出时触发。

    自定义事件

    当然,你也可以自定义事件。

    示例

    ReadyPlug

    import com.jihuayu.wordcount.api.Plugin;
    import com.jihuayu.wordcount.api.event.ReadyEvent;
    import org.greenrobot.eventbus.EventBus;
    import org.greenrobot.eventbus.Subscribe;
    import org.greenrobot.eventbus.ThreadMode;
    
    import java.util.List;
    
    public class ReadyPlug implements Plugin {
        public ReadyPlug(){
            EventBus.getDefault().register(this);
        }
        @Override
        public void getCommandUsage(List<String> info) {
        }
    
        @Override
        public String getCommandName() {
            return null;
        }
    
        @Override
        public void doCommand(String[] args) {
    
        }
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onMessageEvent(ReadyEvent event){
            System.out.println("hello");
        }
    }
    
    

    ReadPlug

    import com.jihuayu.wordcount.api.Plugin;
    import com.jihuayu.wordcount.api.event.ReadEvent;
    import com.jihuayu.wordcount.api.event.ReadOverEvent;
    import org.greenrobot.eventbus.EventBus;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.List;
    
    public class ReadPlug implements Plugin {
        @Override
        public void getCommandUsage(List<String> info) {
            info.add("<file>:read from file");
        }
    
        @Override
        public String getCommandName() {
            return "r";
        }
    
        @Override
        public void doCommand(String[]args) {
            if(args.length>0){
                String str = args[0];
                FileReader fr= null;
                try {
                    fr = new FileReader(str);
    
                BufferedReader br=new BufferedReader(fr);
                String line="";
                while ((line=br.readLine())!=null) {
                    EventBus.getDefault().post(new ReadEvent(line));
                }
                EventBus.getDefault().post(new ReadOverEvent());
                br.close();
                fr.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
  • 相关阅读:
    linux/windows nginx安装
    linux/windows vsftpd安装
    linux 操作命令
    linux/windows java tomcat安装
    常见的Activity Action Intent常量
    Intent.ACTION_PICK
    Android实现抽奖转盘
    Android-短信验证
    Android-多平台分享(新浪微博)
    Android 手势滑动,多点触摸放大缩小图片
  • 原文地址:https://www.cnblogs.com/jhy16193335/p/10522431.html
Copyright © 2011-2022 走看看