Lucene--AttributeSource

一个AttributeSource中包含着一个由不同AttributeImpl组成的列表,以及添加和获取AttributeImpl的一些方法。

内部类:State,标示每个AttributeImpl的状态

AttributeSource():默认使用AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY

public AttributeSource() {
		this(AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY);
	}

AttributeSource(AttributeFactory factory):使用提供的factory来创建attribute实例

public AttributeSource(AttributeFactory factory) {
		this.attributes = new LinkedHashMap<>();
		this.attributeImpls = new LinkedHashMap<>();
		this.currentState = new State[1];
		this.factory = factory;
	}
AttributeSource(AttributeSource input):使用提供的AttributeSource

public AttributeSource(AttributeSource input) {
		if (input == null) {
			throw new IllegalArgumentException("input AttributeSource must not be null");
		}
		this.attributes = input.attributes;
		this.attributeImpls = input.attributeImpls;
		this.currentState = input.currentState;
		this.factory = input.factory;
	}

addAttribute(Class<T> attClass):attClass必须是Attribute的子类,attributes已经存在此类,则返回此类,没有就调用addAttributeImpl(final AttributeImpl att)添加

public final <T extends Attribute> T addAttribute(Class<T> attClass) {
		AttributeImpl attImpl = attributes.get(attClass);
		if (attImpl == null) {
			if (!(attClass.isInterface() && Attribute.class.isAssignableFrom(attClass))) {
				.......
			}
			addAttributeImpl(attImpl = this.factory.createAttributeInstance(attClass));
		}
		return attClass.cast(attImpl);
	}

addAttributeImpl(final AttributeImpl att):添加att到attributeImpls,以及att的接口到attributes。每个Attribute和AttributeImpl保证只有一个AttributeImpl实例

	public final void addAttributeImpl(final AttributeImpl att) {
		final Class<? extends AttributeImpl> clazz = att.getClass();
		if (attributeImpls.containsKey(clazz))
			return;
		// add all interfaces of this AttributeImpl to the maps
		for (final Class<? extends Attribute> curInterface : getAttributeInterfaces(clazz)) {
			// Attribute is a superclass of this interface
			if (!attributes.containsKey(curInterface)) {
				// invalidate state to force recomputation in captureState()
				this.currentState[0] = null;
				attributes.put(curInterface, att);
				attributeImpls.put(clazz, att);
			}
		}
	}

captureState():获取所有attributes

public final State captureState() {
		final State state = this.getCurrentState();
		return (state == null) ? null : state.clone();
	}

clearAttributes():调用AttributeImpl.clear()重置AttributeSource的所有Attributes

	public final void clearAttributes() {
		for (State state = getCurrentState(); state != null; state = state.next) {
			state.attribute.clear();
		}
	}
cloneAttributes():克隆所有的attributes和attributeImpls。如果是查看和修改可以用captureState()

public final AttributeSource cloneAttributes() {
		final AttributeSource clone = new AttributeSource(this.factory);

		if (hasAttributes()) {
			// first clone the impls
			for (State state = getCurrentState(); state != null; state = state.next) {
				clone.attributeImpls.put(state.attribute.getClass(), state.attribute.clone());
			}

			// now the interfaces
			for (Entry<Class<? extends Attribute>, AttributeImpl> entry : this.attributes.entrySet()) {
				clone.attributes.put(entry.getKey(), clone.attributeImpls.get(entry.getValue().getClass()));
			}
		}

		return clone;
	}
copyTo(AttributeSource target):两个AttributeSource必须有相同的AttributeImpl,理想情况下用相同的AttributeFactory来创建AttributeSource
	public final void copyTo(AttributeSource target) {
		for (State state = getCurrentState(); state != null; state = state.next) {
			final AttributeImpl targetImpl = target.attributeImpls.get(state.attribute.getClass());
			if (targetImpl == null) {
				throw new IllegalArgumentException("This AttributeSource contains AttributeImpl of type "
						+ state.attribute.getClass().getName() + " that is not in the target");
			}
			state.attribute.copyTo(targetImpl);
		}
	}
equals(Object obj):主要还是对比attribute是否相同

getAttribute(Class<T> attClass):获取AttributeImpl,传过来的attClass必须继承Attribute

getAttributeClassesIterator():获取AttributeImpl的序列,顺序同加进来的顺序

hasAttribute(Class<? extends Attribute> attClass):attClass必须继承Attribute
hasAttributes():判断AttributeSource是否有其他attributes

reflectAsString(final boolean prependAttClass):把attributes转为字符串,prependAttClass控制是否要返回类名

reflectWith(AttributeReflector reflector):自定义返回字符串的格式

restoreState(State state):重置attribute,只重置state的内容,不重置state外的内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值