All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.logicbus.backend.ServicePath Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.logicbus.backend;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * 服务路径
 *
 * @since 1.6.14.12 [20210617 duanyy]
 */
public interface ServicePath {
    public final static String Delimiter = "/";
    public String getId();
    public String getPackage();
    public void setPath(String path);
    public ServicePath append(String id,boolean copy);
    public ServicePath append(ServicePath path,boolean copy);
    public boolean isRoot();
    public ServicePath subPath(int start,int end);
    public List list();

    public static class Default implements ServicePath{
        protected List content = new ArrayList();

        public Default(String path){
            parsePath(path);
        }

        @Override
        public void setPath(String path){
            content.clear();
            parsePath(path);
        }

        protected void parsePath(String path) {
            StringTokenizer t = new StringTokenizer(path,Delimiter);
            while (t.hasMoreTokens()){
                String value = t.nextToken();
                if (StringUtils.isNotEmpty(value)){
                    content.add(value);
                }
            }
        }

        public Default(List list){
            content = list;
        }

        public String format(int start,int end){
            StringBuffer buf = new StringBuffer();
            boolean isEmpty = true;
            for (int i = start ; i < end ; i ++){
                if (i < 0 || i >= content.size()){
                    break;
                }
                isEmpty = false;
                buf.append(Delimiter).append(content.get(i));
            }
            return isEmpty?Delimiter:buf.toString();
        }

        public Default(List list,int start,int end){
           for (int i = start ; i < end ; i ++){
                if (i < 0 || i >= list.size()){
                    break;
                }
                content.add(list.get(i));
           }
        }

        @Override
        public String getId() {
            if (content.size() < 1){
                return null;
            }
            return content.get(content.size() - 1);
        }

        @Override
        public String getPackage() {
            return format(0,content.size()-1);
        }

        @Override
        public ServicePath append(String id,boolean copy) {
            if (copy){
                ServicePath newPath = new Default(content,0,content.size());
                return newPath.append(id,false);
            }else{
                if (StringUtils.isNotEmpty(id)){
                    content.add(id);
                }
                return this;
            }
        }

        @Override
        public ServicePath append(ServicePath path,boolean copy) {
            if (copy){
                ServicePath newPath = new Default(content,0,content.size());
                return newPath.append(path,false);
            }else{
                List list = path.list();
                for (String item:list){
                    content.add(item);
                }
                return this;
            }
        }

        @Override
        public boolean isRoot() {
            return content.size() <= 0;
        }

        @Override
        public ServicePath subPath(int start, int end) {
            ServicePath newPath = new Default(content,start,end);
            return newPath;
        }

        @Override
        public List list() {
            return content;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy