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外的内容

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Lucene-Core 是 Apache Lucene 项目的核心依赖库。Lucene 是一个开源的全文检索引擎工具包,提供了强大的全文检索功能,可用于构建各种基于文本的应用程序。 在使用 Lucene 时,需要添加 Lucene-Core 依赖到项目中,以便能够使用 Lucene 提供的各种功能。Lucene-Core 是 Lucene 项目最基本的依赖库,包含了一些必备的类和方法,用于索引和搜索文档。 通过 Lucene-Core,可以使用 Lucene 提供的各种 API 来创建索引、搜索和加权查询。Lucene 使用倒排索引的方式来快速定位包含搜索词的文档,而不需要遍历整个文档集合。这种索引结构使得 Lucene 具有出色的搜索效率和性能。 Lucene-Core 还提供了各种分析器(Analyzer)和查询解析器(Query Parser),用于处理文本的分词、词干处理和查询解析等操作。分析器可用于将文本分割成词语,并根据需要进行一些文本处理操作。查询解析器则用于将用户的查询语句解析成 Lucene 可以理解的查询对象。 除了 Lucene-Core,还存在其他的 Lucene 依赖库,如 Lucene-Analyzers、Lucene-Queries 等,它们提供了更高级的功能和扩展,用于处理多语言分词、模糊查询、范围查询等等。 总之,Java Lucene-Core 依赖是使用 Lucene 的必备库,它提供了构建全文检索应用程序所需的基本功能和工具。通过使用 Lucene-Core,开发人员可以更方便地利用 Lucene 的强大功能来实现高效的全文检索。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值