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

com.zusmart.basic.handler.support.AbstractHandlerChain Maven / Gradle / Ivy

Go to download

基础模块,提供配置,日志,SPI,图排序,路径匹配,资源扫描,包扫描,常用工具类

There is a newer version: 0.0.3
Show newest version
package com.zusmart.basic.handler.support;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.zusmart.basic.handler.Handler;
import com.zusmart.basic.handler.HandlerChain;
import com.zusmart.basic.handler.HandlerContext;

public abstract class AbstractHandlerChain implements HandlerChain {

	protected HandlerContext head;
	protected HandlerContext foot;

	public AbstractHandlerChain() {
		this.head = this.createHeadHandlerContext();
		this.foot = this.createFootHandlerContext();
		this.head.setNext(this.foot);
		this.foot.setPrev(this.head);
	}

	@Override
	public Iterator> iterator() {
		return this.getAllHandlerAsMap().entrySet().iterator();
	}

	@Override
	public void addFirst(String name, T handler) {
		final HandlerContext context;
		synchronized (this) {
			context = this.createHandlerContext(name, handler);
			this.doAddFirst(context);
		}
	}

	@Override
	public void addLast(String name, T handler) {
		final HandlerContext context;
		synchronized (this) {
			context = this.createHandlerContext(name, handler);
			this.doAddLast(context);
		}
	}

	@Override
	public void addBefore(String target, String name, T handler) {
		final HandlerContext nContext;
		final HandlerContext oContext;
		synchronized (this) {
			oContext = this.getContext(target);
			nContext = this.createHandlerContext(name, handler);
			if (null == oContext) {
				throw new IllegalArgumentException("old context not found : [ " + target + "]");
			}
			this.doAddBefore(nContext, oContext);
		}
	}

	@Override
	public void addAfter(String target, String name, T handler) {
		final HandlerContext nContext;
		final HandlerContext oContext;
		synchronized (this) {
			oContext = this.getContext(target);
			nContext = this.createHandlerContext(name, handler);
			if (null == oContext) {
				throw new IllegalArgumentException("old context not found : [ " + target + "]");
			}
			this.doAddAfter(nContext, oContext);
		}
	}

	@Override
	public void delHandler(String target) {
		HandlerContext context = this.getContext(target);
		context.getPrev().setNext(context.getNext());
		context.getNext().setPrev(context.getPrev());
	}

	@Override
	public boolean containsHandler(String target) {
		HandlerContext context = this.head.getNext();
		while (context != this.foot) {
			if (context.getName().equals(target)) {
				return true;
			}
			context = context.getNext();
		}
		return false;
	}

	@Override
	public void replaceHandler(String target, String name, T handler) {
		HandlerContext context = this.getContext(target);
		HandlerContext newContext = this.createHandlerContext(name, handler);
		newContext.setPrev(context.getPrev());
		newContext.setNext(context.getNext());
	}

	@Override
	public T getFirstHandler() {
		HandlerContext context = this.getFirstHandlerContext();
		if (null != context) {
			return context.getHandler();
		}
		return null;
	}

	@Override
	public T getLastHandler() {
		HandlerContext context = this.getLastHandlerContext();
		if (null != context) {
			return context.getHandler();
		}
		return null;
	}

	@Override
	public HandlerContext getFirstHandlerContext() {
		HandlerContext context = this.head.getNext();
		if (context == this.foot) {
			return null;
		}
		return this.head.getNext();
	}

	@Override
	public HandlerContext getLastHandlerContext() {
		HandlerContext context = this.foot.getPrev();
		if (context == this.head) {
			return null;
		}
		return this.foot.getPrev();
	}

	@Override
	public Map getAllHandlerAsMap() {
		Map result = new LinkedHashMap();
		HandlerContext context = this.head.getNext();
		for (;;) {
			if (this.foot == context) {
				return result;
			}
			result.put(context.getName(), context.getHandler());
			context = context.getNext();
		}
	}

	@Override
	public List getAllHandlerAsList() {
		List result = new ArrayList();
		HandlerContext context = this.head.getNext();
		for (;;) {
			if (this.foot == context) {
				return result;
			}
			result.add(context.getHandler());
			context = context.getNext();
		}
	}

	@Override
	public List getAllHandlerNameAsList() {
		List result = new ArrayList();
		HandlerContext context = this.head.getNext();
		for (;;) {
			if (this.foot == context) {
				return result;
			}
			result.add(context.getName());
			context = context.getNext();
		}
	}

	private void doAddFirst(HandlerContext newContext) {
		HandlerContext nextContext = this.head.getNext();
		newContext.setPrev(this.head);
		newContext.setNext(nextContext);
		this.head.setNext(newContext);
		nextContext.setPrev(newContext);
	}

	private void doAddLast(HandlerContext newContext) {
		HandlerContext prevContext = this.foot.getPrev();
		newContext.setPrev(prevContext);
		newContext.setNext(this.foot);
		prevContext.setNext(newContext);
		this.foot.setPrev(newContext);
	}

	private void doAddBefore(HandlerContext newContext, HandlerContext oldContext) {
		newContext.setPrev(oldContext.getPrev());
		newContext.setNext(oldContext);
		oldContext.getPrev().setNext(newContext);
		oldContext.setPrev(newContext);
	}

	private void doAddAfter(HandlerContext newContext, HandlerContext oldContext) {
		newContext.setPrev(oldContext);
		newContext.setNext(oldContext.getNext());
		oldContext.getNext().setPrev(newContext);
		oldContext.setNext(newContext);
	}

	private HandlerContext getContext(String name) {
		HandlerContext context = this.head.getNext();
		while (context != this.foot) {
			if (context.getName().equals(name)) {
				return context;
			}
			context = context.getNext();
		}
		return null;
	}

	protected abstract HandlerContext createHeadHandlerContext();

	protected abstract HandlerContext createFootHandlerContext();

	protected abstract HandlerContext createHandlerContext(String name, T handler);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy