@Override public void setNextEnum(TermsEnum termsEnum) { this.termsEnum = termsEnum; assert compareToLastTerm(null); // lazy init the initial ScoreTerm because comparator is not known on ctor: if (st == null) st = new ScoreTerm(new TermContext(topReaderContext)); boostAtt = termsEnum.attributes().addAttribute(BoostAttribute.class); }
/** Returns the related attributes. */ public AttributeSource attributes() { if (atts == null) atts = new AttributeSource(); return atts; }
/** * Sets attributeSource to a new instance. */ void setAttributeSource(AttributeSource attributeSource) { if (this.attributeSource != attributeSource) { this.attributeSource = attributeSource; termAttribute = attributeSource.getAttribute(TermToBytesRefAttribute.class); termFreqAttribute = attributeSource.addAttribute(TermFrequencyAttribute.class); posIncrAttribute = attributeSource.addAttribute(PositionIncrementAttribute.class); offsetAttribute = attributeSource.addAttribute(OffsetAttribute.class); payloadAttribute = attributeSource.getAttribute(PayloadAttribute.class); } }
AttributeSource other = (AttributeSource) obj; if (hasAttributes()) { if (!other.hasAttributes()) { return false; State thisState = this.getCurrentState(); State otherState = other.getCurrentState(); while (thisState != null && otherState != null) { if (otherState.attribute.getClass() != thisState.attribute.getClass() || !otherState.attribute.equals(thisState.attribute)) { return !other.hasAttributes();
/** * Performs a clone of all {@link AttributeImpl} instances returned in a new * {@code AttributeSource} instance. This method can be used to e.g. create another TokenStream * with exactly the same attributes (using {@link #AttributeSource(AttributeSource)}). * You can also use it as a (non-performant) replacement for {@link #captureState}, if you need to look * into / modify the captured state. */ 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; }
if (numFillerTokensToInsert > 0) { if (null == target) { newTarget = new InputWindowToken(nextInputStreamToken.cloneAttributes()); } else { nextInputStreamToken.copyTo(target.attSource); } else if (isNextInputStreamToken) { if (null == target) { newTarget = new InputWindowToken(nextInputStreamToken.cloneAttributes()); } else { nextInputStreamToken.copyTo(target.attSource); numFillerTokensToInsert = Math.min(posIncrAtt.getPositionIncrement(), maxShingleSize - 1); if (numFillerTokensToInsert > 0) { nextInputStreamToken = new AttributeSource(getAttributeFactory()); nextInputStreamToken.addAttribute(CharTermAttribute.class); OffsetAttribute newOffsetAtt = nextInputStreamToken.addAttribute(OffsetAttribute.class); newOffsetAtt.setOffset(offsetAtt.endOffset(), offsetAtt.endOffset());
@Override public int hashCode() { int code = 0; for (State state = getCurrentState(); state != null; state = state.next) { code = code * 31 + state.attribute.hashCode(); } return code; }
int end = breaker.next(); if (end != BreakIterator.DONE) { clonedToken.copyTo(this); termAtt.copyBuffer(clonedTermAtt.buffer(), start, end - start); if (hasIllegalOffsets) { clonedTermAtt = clonedToken.getAttribute(CharTermAttribute.class); clonedOffsetAtt = clonedToken.getAttribute(OffsetAttribute.class); } else { this.copyTo(clonedToken);
public InputWindowToken(AttributeSource attSource) { this.attSource = attSource; this.termAtt = attSource.getAttribute(CharTermAttribute.class); this.offsetAtt = attSource.getAttribute(OffsetAttribute.class); } }
/** <b>Expert:</b> Adds a custom AttributeImpl instance with one or more Attribute interfaces. * <p><b>NOTE:</b> It is not guaranteed, that <code>att</code> is added to * the <code>AttributeSource</code>, because the provided attributes may already exist. * You should always retrieve the wanted attributes using {@link #getAttribute} after adding * with this method and cast to your class. * The recommended way to use custom implementations is using an {@link AttributeFactory}. * </p> */ 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); } } }
private State getCurrentState() { State s = currentState[0]; if (s != null || !hasAttributes()) { return s; } State c = s = currentState[0] = new State(); final Iterator<AttributeImpl> it = attributeImpls.values().iterator(); c.attribute = it.next(); while (it.hasNext()) { c.next = new State(); c = c.next; c.attribute = it.next(); } return s; }
/** * The caller must pass in a Class<? extends Attribute> value. * This method first checks if an instance of that class is * already in this AttributeSource and returns it. Otherwise a * new instance is created, added to this AttributeSource and returned. */ 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))) { throw new IllegalArgumentException( "addAttribute() only accepts an interface that extends Attribute, but " + attClass.getName() + " does not fulfil this contract." ); } addAttributeImpl(attImpl = this.factory.createAttributeInstance(attClass)); } return attClass.cast(attImpl); }
/** * This method returns the current attribute values as a string in the following format * by calling the {@link #reflectWith(AttributeReflector)} method: * * <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(new AttributeReflector() { @Override public void reflect(Class<? extends Attribute> attClass, String key, Object 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(); }
/** * Performs a clone of all {@link AttributeImpl} instances returned in a new * {@code AttributeSource} instance. This method can be used to e.g. create another TokenStream * with exactly the same attributes (using {@link #AttributeSource(AttributeSource)}). * You can also use it as a (non-performant) replacement for {@link #captureState}, if you need to look * into / modify the captured state. */ 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; }
AttributeSource other = (AttributeSource) obj; if (hasAttributes()) { if (!other.hasAttributes()) { return false; State thisState = this.getCurrentState(); State otherState = other.getCurrentState(); while (thisState != null && otherState != null) { if (otherState.attribute.getClass() != thisState.attribute.getClass() || !otherState.attribute.equals(thisState.attribute)) { return !other.hasAttributes();
/** * Resets all Attributes in this AttributeSource by calling * {@link AttributeImpl#clear()} on each Attribute implementation. */ public final void clearAttributes() { for (State state = getCurrentState(); state != null; state = state.next) { state.attribute.clear(); } }
int end = breaker.next(); if (end != BreakIterator.DONE) { clonedToken.copyTo(this); termAtt.copyBuffer(clonedTermAtt.buffer(), start, end - start); if (hasIllegalOffsets) { clonedTermAtt = clonedToken.getAttribute(CharTermAttribute.class); clonedOffsetAtt = clonedToken.getAttribute(OffsetAttribute.class); } else { this.copyTo(clonedToken);
public InputWindowToken(AttributeSource attSource) { this.attSource = attSource; this.termAtt = attSource.getAttribute(CharTermAttribute.class); this.offsetAtt = attSource.getAttribute(OffsetAttribute.class); } }
/** <b>Expert:</b> Adds a custom AttributeImpl instance with one or more Attribute interfaces. * <p><b>NOTE:</b> It is not guaranteed, that <code>att</code> is added to * the <code>AttributeSource</code>, because the provided attributes may already exist. * You should always retrieve the wanted attributes using {@link #getAttribute} after adding * with this method and cast to your class. * The recommended way to use custom implementations is using an {@link AttributeFactory}. * </p> */ 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); } } }
private State getCurrentState() { State s = currentState[0]; if (s != null || !hasAttributes()) { return s; } State c = s = currentState[0] = new State(); final Iterator<AttributeImpl> it = attributeImpls.values().iterator(); c.attribute = it.next(); while (it.hasNext()) { c.next = new State(); c = c.next; c.attribute = it.next(); } return s; }