Solr.AttributeImpl源码分析

Apache Lucene 的 util 包中的 AttributeImpl 类是一个抽象基类,用于实现添加到 AttributeSource 的属性。此类提供了清除、结束、反射和复制到其他属性实例的方法,以动态且类型安全的方式管理数据。属性主要用于流式处理对象的数据存储。
摘要由CSDN通过智能技术生成

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;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值