2021SC@SDUSC
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.util;
/**
* 可以添加到属性的基类
* {@link org.apache.lucene.util.AttributeSource}。
* <p>
* 属性用于以动态但类型安全的方式将数据添加到源
* 通常是流式传输的对象
*/
public abstract class AttributeImpl implements Cloneable, Attribute {
/**
* 清除此 AttributeImpl 中的值并将其重置为其
* 默认值。 如果这个实现实现了多个 Attribute 接口
* 它清除所有。
*/
public abstract void clear();
/**
* 清除此 AttributeImpl 中的值并将其重置为其值
* 在字段的末尾。 如果这个实现实现了多个 Attribute 接口
* 它清除所有。
* <p>
* 默认实现只是调用 {@link #clear()}
*/
public void end() {
clear();
}
/**
* 此方法以以下格式的字符串返回当前属性值
* 通过调用 {@link #reflectWith(AttributeReflector)} 方法:
*
* <ul>
* <li><em>iff {@code prependAttClass=true}:</em> {@code "AttributeClass#key=value,AttributeClass#key=value"}
* <li><em>iff {@code prependAttClass=false}:</em> {@code "key=value,key=value"}
* </ul>
*
* @see #reflectWith(AttributeReflector)
*/
public final String reflectAsString(final boolean prependAttClass) {
final StringBuilder buffer = new StringBuilder();
reflectWith((attClass, key, value) -> {
if (buffer.length() > 0) {
buffer.append(',');
}
if (prependAttClass) {
buffer.append(attClass.getName()).append('#');
}
buffer.append(key).append('=').append((value == null) ? "null" : value);
});
return buffer.toString();
}
/**
* 该方法用于属性自省
* 将此属性持有的键/值添加到给定的 {@link AttributeReflector}。
*
* <p>Implementations look like this (e.g. for a combined attribute implementation):
* <pre class="prettyprint">
* public void reflectWith(AttributeReflector reflector) {
* reflector.reflect(CharTermAttribute.class, "term", term());
* reflector.reflect(PositionIncrementAttribute.class, "positionIncrement", getPositionIncrement());
* }
* </pre>
*
* <p>如果你实现了这个方法,请确保对于每次调用,同一套{@link Attribute}
* 接口和键以相同的顺序传递给 {@link AttributeReflector#reflect},但可能
* 不同的值。 所以不要自动排除例如 {@code null} 属性!
*
* @see #reflectAsString(boolean)
*/
public abstract void reflectWith(AttributeReflector reflector);
/**
* 将来自该属性的值复制到传入的
* 目标属性。 目标实现必须支持所有
* 此实现支持的属性。
*/
public abstract void copyTo(AttributeImpl target);
/**
* 在大多数情况下,克隆是并且应该是深的,以便能够
* 正确捕获所有属性的状态。
*/
@Override
public AttributeImpl clone() {
AttributeImpl clone = null;
try {
clone = (AttributeImpl)super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e); // shouldn't happen
}
return clone;
}
}