- 浏览: 138686 次
- 性别:
- 来自: 福建省莆田市
-
文章分类
最新评论
-
houruiming:
tks for your info which helps m ...
setcontent和setcontentobject用的是同一片内存 -
turingfellow:
in.tftpd -l -s /home/tmp -u ro ...
commands -
turingfellow:
LINUX下的网络设置 ifconfig ,routeLINU ...
commands -
turingfellow:
安装 linux loopbackyum install um ...
commands
package org.apache.lucene.util;
/**
* 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.
*/
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.WeakHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.lucene.analysis.TokenStream; // for javadocs
/**
* An AttributeSource contains a list of different {@link AttributeImpl}s,
* and methods to add and get them. There can only be a single instance
* of an attribute in the same AttributeSource instance. This is ensured
* by passing in the actual type of the Attribute (Class<Attribute>) to
* the {@link #addAttribute(Class)}, which then checks if an instance of
* that type is already present. If yes, it returns the instance, otherwise
* it creates a new instance and returns it.
*/
public class AttributeSource {
/**
* An AttributeFactory creates instances of {@link AttributeImpl}s.
*/
public static abstract class AttributeFactory {
/**
* returns an {@link AttributeImpl} for the supplied {@link Attribute} interface class.
* <p>Signature for Java 1.5: <code>public AttributeImpl createAttributeInstance(Class%lt;? extends Attribute> attClass)</code>
*/
public abstract AttributeImpl createAttributeInstance(Class attClass);
/**
* This is the default factory that creates {@link AttributeImpl}s using the
* class name of the supplied {@link Attribute} interface class by appending <code>Impl</code> to it.
*/
public static final AttributeFactory DEFAULT_ATTRIBUTE_FACTORY = new DefaultAttributeFactory();
private static final class DefaultAttributeFactory extends AttributeFactory {
private static final WeakHashMap/*<Class<? extends Attribute>, WeakReference<Class<? extends AttributeImpl>>>*/ attClassImplMap = new WeakHashMap();
private DefaultAttributeFactory() {}
public AttributeImpl createAttributeInstance(Class attClass) {
try {
return (AttributeImpl) getClassForInterface(attClass).newInstance();
} catch (InstantiationException e) {
throw new IllegalArgumentException("Could not instantiate implementing class for " + attClass.getName());
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Could not instantiate implementing class for " + attClass.getName());
}
}
private static Class getClassForInterface(Class attClass) {
synchronized(attClassImplMap) {
final WeakReference ref = (WeakReference) attClassImplMap.get(attClass);
Class clazz = (ref == null) ? null : ((Class) ref.get());
if (clazz == null) {
try {
attClassImplMap.put(attClass, new WeakReference(
clazz = Class.forName(attClass.getName() + "Impl", true, attClass.getClassLoader())
));
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not find implementing class for " + attClass.getName());
}
}
return clazz;
}
}
}
}
// These two maps must always be in sync!!!
// So they are private, final and read-only from the outside (read-only iterators)
private final Map/*<Class<Attribute>,AttributeImpl>*/ attributes;
private final Map/*<Class<AttributeImpl>,AttributeImpl>*/ attributeImpls;
private AttributeFactory factory;
/**
* An AttributeSource using the default attribute factory {@link AttributeSource.AttributeFactory#DEFAULT_ATTRIBUTE_FACTORY}.
*/
public AttributeSource() {
this(AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY);
}
/**
* An AttributeSource that uses the same attributes as the supplied one.
*/
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.factory = input.factory;
}
/**
* An AttributeSource using the supplied {@link AttributeFactory} for creating new {@link Attribute} instances.
*/
public AttributeSource(AttributeFactory factory) {
this.attributes = new LinkedHashMap();
this.attributeImpls = new LinkedHashMap();
this.factory = factory;
}
/**
* returns the used AttributeFactory.
*/
public AttributeFactory getAttributeFactory() {
return this.factory;
}
/** Returns a new iterator that iterates the attribute classes
* in the same order they were added in.
* <p>Signature for Java 1.5: <code>public Iterator<Class<? extends Attribute>> getAttributeClassesIterator()</code>
*/
public Iterator getAttributeClassesIterator() {
return Collections.unmodifiableSet(attributes.keySet()).iterator();
}
/** Returns a new iterator that iterates all unique Attribute implementations.
* This iterator may contain less entries that {@link #getAttributeClassesIterator},
* if one instance implements more than one Attribute interface.
* <p>Signature for Java 1.5: <code>public Iterator<AttributeImpl> getAttributeImplsIterator()</code>
*/
public Iterator getAttributeImplsIterator() {
if (hasAttributes()) {
if (currentState == null) {
computeCurrentState();
}
final State initState = currentState;
return new Iterator() {
private State state = initState;
public void remove() {
throw new UnsupportedOperationException();
}
public Object next() {
if (state == null)
throw new NoSuchElementException();
final AttributeImpl att = state.attribute;
state = state.next;
return att;
}
public boolean hasNext() {
return state != null;
}
};
} else {
return Collections.EMPTY_SET.iterator();
}
}
/** a cache that stores all interfaces for known implementation classes for performance (slow reflection) */
private static final WeakHashMap/*<Class<? extends AttributeImpl>,LinkedList<WeakReference<Class<? extends Attribute>>>>*/
knownImplClasses = new WeakHashMap();
/** Adds a custom AttributeImpl instance with one or more Attribute interfaces. */
public void addAttributeImpl(final AttributeImpl att) {
final Class clazz = att.getClass();
if (attributeImpls.containsKey(clazz)) return;
LinkedList foundInterfaces;
synchronized(knownImplClasses) {
foundInterfaces = (LinkedList) knownImplClasses.get(clazz);
if (foundInterfaces == null) {
// we have a strong reference to the class instance holding all interfaces in the list (parameter "att"),
// so all WeakReferences are never evicted by GC
knownImplClasses.put(clazz, foundInterfaces=new LinkedList());
// find all interfaces that this attribute instance implements
// and that extend the Attribute interface
Class actClazz = clazz;
do {
Class[] interfaces = actClazz.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
final Class curInterface = interfaces[i];
if (curInterface != Attribute.class && Attribute.class.isAssignableFrom(curInterface)) {
foundInterfaces.add(new WeakReference(curInterface));
}
}
actClazz = actClazz.getSuperclass();
} while (actClazz != null);
}
}
// add all interfaces of this AttributeImpl to the maps
for (Iterator it = foundInterfaces.iterator(); it.hasNext(); ) {
final WeakReference curInterfaceRef = (WeakReference) it.next();
final Class curInterface = (Class) curInterfaceRef.get();
assert (curInterface != null) :
"We have a strong reference on the class holding the interfaces, so they should never get evicted";
// Attribute is a superclass of this interface
if (!attributes.containsKey(curInterface)) {
// invalidate state to force recomputation in captureState()
this.currentState = null;
attributes.put(curInterface, att);
attributeImpls.put(clazz, att);
}
}
}
/**
* 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.
* <p>Signature for Java 1.5: <code>public <T extends Attribute> T addAttribute(Class<T>)</code>
*/
public Attribute addAttribute(Class attClass) {
final Attribute att = (Attribute) attributes.get(attClass);
if (att == 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."
);
}
final AttributeImpl attImpl = this.factory.createAttributeInstance(attClass);
addAttributeImpl(attImpl);
return attImpl;
} else {
return att;
}
}
/** Returns true, iff this AttributeSource has any attributes */
public boolean hasAttributes() {
return !this.attributes.isEmpty();
}
/**
* The caller must pass in a Class<? extends Attribute> value.
* Returns true, iff this AttributeSource contains the passed-in Attribute.
* <p>Signature for Java 1.5: <code>public boolean hasAttribute(Class<? extends Attribute>)</code>
*/
public boolean hasAttribute(Class attClass) {
return this.attributes.containsKey(attClass);
}
/**
* The caller must pass in a Class<? extends Attribute> value.
* Returns the instance of the passed in Attribute contained in this AttributeSource
* <p>Signature for Java 1.5: <code>public <T extends Attribute> T getAttribute(Class<T>)</code>
*
* @throws IllegalArgumentException if this AttributeSource does not contain the
* Attribute. It is recommended to always use {@link #addAttribute} even in consumers
* of TokenStreams, because you cannot know if a specific TokenStream really uses
* a specific Attribute. {@link #addAttribute} will automatically make the attribute
* available. If you want to only use the attribute, if it is available (to optimize
* consuming), use {@link #hasAttribute}.
*/
public Attribute getAttribute(Class attClass) {
final Attribute att = (Attribute) this.attributes.get(attClass);
if (att == null) {
throw new IllegalArgumentException("This AttributeSource does not have the attribute '" + attClass.getName() + "'.");
}
return att;
}
/**
* This class holds the state of an AttributeSource.
* @see #captureState
* @see #restoreState
*/
public static final class State implements Cloneable {
private AttributeImpl attribute;
private State next;
public Object clone() {
State clone = new State();
clone.attribute = (AttributeImpl) attribute.clone();
if (next != null) {
clone.next = (State) next.clone();
}
return clone;
}
}
private State currentState = null;
private void computeCurrentState() {
currentState = new State();
State c = currentState;
Iterator it = attributeImpls.values().iterator();
c.attribute = (AttributeImpl) it.next();
while (it.hasNext()) {
c.next = new State();
c = c.next;
c.attribute = (AttributeImpl) it.next();
}
}
/**
* Resets all Attributes in this AttributeSource by calling
* {@link AttributeImpl#clear()} on each Attribute implementation.
*/
public void clearAttributes() {
if (hasAttributes()) {
if (currentState == null) {
computeCurrentState();
}
for (State state = currentState; state != null; state = state.next) {
state.attribute.clear();
}
}
}
/**
* Captures the state of all Attributes. The return value can be passed to
* {@link #restoreState} to restore the state of this or another AttributeSource.
*/
public State captureState() {
if (!hasAttributes()) {
return null;
}
if (currentState == null) {
computeCurrentState();
}
return (State) this.currentState.clone();
}
/**
* Restores this state by copying the values of all attribute implementations
* that this state contains into the attributes implementations of the targetStream.
* The targetStream must contain a corresponding instance for each argument
* contained in this state (e.g. it is not possible to restore the state of
* an AttributeSource containing a TermAttribute into a AttributeSource using
* a Token instance as implementation).
* <p>
* Note that this method does not affect attributes of the targetStream
* that are not contained in this state. In other words, if for example
* the targetStream contains an OffsetAttribute, but this state doesn't, then
* the value of the OffsetAttribute remains unchanged. It might be desirable to
* reset its value to the default, in which case the caller should first
* call {@link TokenStream#clearAttributes()} on the targetStream.
*/
public void restoreState(State state) {
if (state == null) return;
do {
AttributeImpl targetImpl = (AttributeImpl) attributeImpls.get(state.attribute.getClass());
if (targetImpl == null)
throw new IllegalArgumentException("State contains an AttributeImpl that is not in this AttributeSource");
state.attribute.copyTo(targetImpl);
state = state.next;
} while (state != null);
}
public int hashCode() {
int code = 0;
if (hasAttributes()) {
if (currentState == null) {
computeCurrentState();
}
for (State state = currentState; state != null; state = state.next) {
code = code * 31 + state.attribute.hashCode();
}
}
return code;
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof AttributeSource) {
AttributeSource other = (AttributeSource) obj;
if (hasAttributes()) {
if (!other.hasAttributes()) {
return false;
}
if (this.attributeImpls.size() != other.attributeImpls.size()) {
return false;
}
// it is only equal if all attribute impls are the same in the same order
if (this.currentState == null) {
this.computeCurrentState();
}
State thisState = this.currentState;
if (other.currentState == null) {
other.computeCurrentState();
}
State otherState = other.currentState;
while (thisState != null && otherState != null) {
if (otherState.attribute.getClass() != thisState.attribute.getClass() || !otherState.attribute.equals(thisState.attribute)) {
return false;
}
thisState = thisState.next;
otherState = otherState.next;
}
return true;
} else {
return !other.hasAttributes();
}
} else
return false;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append('(');
if (hasAttributes()) {
if (currentState == null) {
computeCurrentState();
}
for (State state = currentState; state != null; state = state.next) {
if (state != currentState) sb.append(',');
sb.append(state.attribute.toString());
}
}
sb.append(')');
return sb.toString();
}
/**
* Performs a clone of all {@link AttributeImpl} instances returned in a new
* AttributeSource instance. This method can be used to e.g. create another TokenStream
* with exactly the same attributes (using {@link #AttributeSource(AttributeSource)})
*/
public AttributeSource cloneAttributes() {
AttributeSource clone = new AttributeSource(this.factory);
// first clone the impls
if (hasAttributes()) {
if (currentState == null) {
computeCurrentState();
}
for (State state = currentState; state != null; state = state.next) {
clone.attributeImpls.put(state.attribute.getClass(), state.attribute.clone());
}
}
// now the interfaces
Iterator/*<Entry<Class<Attribute>, AttributeImpl>>*/ attIt = this.attributes.entrySet().iterator();
while (attIt.hasNext()) {
Entry/*<Class<Attribute>, AttributeImpl>*/ entry = (Entry/*<Class<Attribute>, AttributeImpl>*/) attIt.next();
clone.attributes.put(entry.getKey(), clone.attributeImpls.get(entry.getValue().getClass()));
}
return clone;
}
}
/**
* 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.
*/
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.WeakHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.lucene.analysis.TokenStream; // for javadocs
/**
* An AttributeSource contains a list of different {@link AttributeImpl}s,
* and methods to add and get them. There can only be a single instance
* of an attribute in the same AttributeSource instance. This is ensured
* by passing in the actual type of the Attribute (Class<Attribute>) to
* the {@link #addAttribute(Class)}, which then checks if an instance of
* that type is already present. If yes, it returns the instance, otherwise
* it creates a new instance and returns it.
*/
public class AttributeSource {
/**
* An AttributeFactory creates instances of {@link AttributeImpl}s.
*/
public static abstract class AttributeFactory {
/**
* returns an {@link AttributeImpl} for the supplied {@link Attribute} interface class.
* <p>Signature for Java 1.5: <code>public AttributeImpl createAttributeInstance(Class%lt;? extends Attribute> attClass)</code>
*/
public abstract AttributeImpl createAttributeInstance(Class attClass);
/**
* This is the default factory that creates {@link AttributeImpl}s using the
* class name of the supplied {@link Attribute} interface class by appending <code>Impl</code> to it.
*/
public static final AttributeFactory DEFAULT_ATTRIBUTE_FACTORY = new DefaultAttributeFactory();
private static final class DefaultAttributeFactory extends AttributeFactory {
private static final WeakHashMap/*<Class<? extends Attribute>, WeakReference<Class<? extends AttributeImpl>>>*/ attClassImplMap = new WeakHashMap();
private DefaultAttributeFactory() {}
public AttributeImpl createAttributeInstance(Class attClass) {
try {
return (AttributeImpl) getClassForInterface(attClass).newInstance();
} catch (InstantiationException e) {
throw new IllegalArgumentException("Could not instantiate implementing class for " + attClass.getName());
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Could not instantiate implementing class for " + attClass.getName());
}
}
private static Class getClassForInterface(Class attClass) {
synchronized(attClassImplMap) {
final WeakReference ref = (WeakReference) attClassImplMap.get(attClass);
Class clazz = (ref == null) ? null : ((Class) ref.get());
if (clazz == null) {
try {
attClassImplMap.put(attClass, new WeakReference(
clazz = Class.forName(attClass.getName() + "Impl", true, attClass.getClassLoader())
));
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not find implementing class for " + attClass.getName());
}
}
return clazz;
}
}
}
}
// These two maps must always be in sync!!!
// So they are private, final and read-only from the outside (read-only iterators)
private final Map/*<Class<Attribute>,AttributeImpl>*/ attributes;
private final Map/*<Class<AttributeImpl>,AttributeImpl>*/ attributeImpls;
private AttributeFactory factory;
/**
* An AttributeSource using the default attribute factory {@link AttributeSource.AttributeFactory#DEFAULT_ATTRIBUTE_FACTORY}.
*/
public AttributeSource() {
this(AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY);
}
/**
* An AttributeSource that uses the same attributes as the supplied one.
*/
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.factory = input.factory;
}
/**
* An AttributeSource using the supplied {@link AttributeFactory} for creating new {@link Attribute} instances.
*/
public AttributeSource(AttributeFactory factory) {
this.attributes = new LinkedHashMap();
this.attributeImpls = new LinkedHashMap();
this.factory = factory;
}
/**
* returns the used AttributeFactory.
*/
public AttributeFactory getAttributeFactory() {
return this.factory;
}
/** Returns a new iterator that iterates the attribute classes
* in the same order they were added in.
* <p>Signature for Java 1.5: <code>public Iterator<Class<? extends Attribute>> getAttributeClassesIterator()</code>
*/
public Iterator getAttributeClassesIterator() {
return Collections.unmodifiableSet(attributes.keySet()).iterator();
}
/** Returns a new iterator that iterates all unique Attribute implementations.
* This iterator may contain less entries that {@link #getAttributeClassesIterator},
* if one instance implements more than one Attribute interface.
* <p>Signature for Java 1.5: <code>public Iterator<AttributeImpl> getAttributeImplsIterator()</code>
*/
public Iterator getAttributeImplsIterator() {
if (hasAttributes()) {
if (currentState == null) {
computeCurrentState();
}
final State initState = currentState;
return new Iterator() {
private State state = initState;
public void remove() {
throw new UnsupportedOperationException();
}
public Object next() {
if (state == null)
throw new NoSuchElementException();
final AttributeImpl att = state.attribute;
state = state.next;
return att;
}
public boolean hasNext() {
return state != null;
}
};
} else {
return Collections.EMPTY_SET.iterator();
}
}
/** a cache that stores all interfaces for known implementation classes for performance (slow reflection) */
private static final WeakHashMap/*<Class<? extends AttributeImpl>,LinkedList<WeakReference<Class<? extends Attribute>>>>*/
knownImplClasses = new WeakHashMap();
/** Adds a custom AttributeImpl instance with one or more Attribute interfaces. */
public void addAttributeImpl(final AttributeImpl att) {
final Class clazz = att.getClass();
if (attributeImpls.containsKey(clazz)) return;
LinkedList foundInterfaces;
synchronized(knownImplClasses) {
foundInterfaces = (LinkedList) knownImplClasses.get(clazz);
if (foundInterfaces == null) {
// we have a strong reference to the class instance holding all interfaces in the list (parameter "att"),
// so all WeakReferences are never evicted by GC
knownImplClasses.put(clazz, foundInterfaces=new LinkedList());
// find all interfaces that this attribute instance implements
// and that extend the Attribute interface
Class actClazz = clazz;
do {
Class[] interfaces = actClazz.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
final Class curInterface = interfaces[i];
if (curInterface != Attribute.class && Attribute.class.isAssignableFrom(curInterface)) {
foundInterfaces.add(new WeakReference(curInterface));
}
}
actClazz = actClazz.getSuperclass();
} while (actClazz != null);
}
}
// add all interfaces of this AttributeImpl to the maps
for (Iterator it = foundInterfaces.iterator(); it.hasNext(); ) {
final WeakReference curInterfaceRef = (WeakReference) it.next();
final Class curInterface = (Class) curInterfaceRef.get();
assert (curInterface != null) :
"We have a strong reference on the class holding the interfaces, so they should never get evicted";
// Attribute is a superclass of this interface
if (!attributes.containsKey(curInterface)) {
// invalidate state to force recomputation in captureState()
this.currentState = null;
attributes.put(curInterface, att);
attributeImpls.put(clazz, att);
}
}
}
/**
* 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.
* <p>Signature for Java 1.5: <code>public <T extends Attribute> T addAttribute(Class<T>)</code>
*/
public Attribute addAttribute(Class attClass) {
final Attribute att = (Attribute) attributes.get(attClass);
if (att == 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."
);
}
final AttributeImpl attImpl = this.factory.createAttributeInstance(attClass);
addAttributeImpl(attImpl);
return attImpl;
} else {
return att;
}
}
/** Returns true, iff this AttributeSource has any attributes */
public boolean hasAttributes() {
return !this.attributes.isEmpty();
}
/**
* The caller must pass in a Class<? extends Attribute> value.
* Returns true, iff this AttributeSource contains the passed-in Attribute.
* <p>Signature for Java 1.5: <code>public boolean hasAttribute(Class<? extends Attribute>)</code>
*/
public boolean hasAttribute(Class attClass) {
return this.attributes.containsKey(attClass);
}
/**
* The caller must pass in a Class<? extends Attribute> value.
* Returns the instance of the passed in Attribute contained in this AttributeSource
* <p>Signature for Java 1.5: <code>public <T extends Attribute> T getAttribute(Class<T>)</code>
*
* @throws IllegalArgumentException if this AttributeSource does not contain the
* Attribute. It is recommended to always use {@link #addAttribute} even in consumers
* of TokenStreams, because you cannot know if a specific TokenStream really uses
* a specific Attribute. {@link #addAttribute} will automatically make the attribute
* available. If you want to only use the attribute, if it is available (to optimize
* consuming), use {@link #hasAttribute}.
*/
public Attribute getAttribute(Class attClass) {
final Attribute att = (Attribute) this.attributes.get(attClass);
if (att == null) {
throw new IllegalArgumentException("This AttributeSource does not have the attribute '" + attClass.getName() + "'.");
}
return att;
}
/**
* This class holds the state of an AttributeSource.
* @see #captureState
* @see #restoreState
*/
public static final class State implements Cloneable {
private AttributeImpl attribute;
private State next;
public Object clone() {
State clone = new State();
clone.attribute = (AttributeImpl) attribute.clone();
if (next != null) {
clone.next = (State) next.clone();
}
return clone;
}
}
private State currentState = null;
private void computeCurrentState() {
currentState = new State();
State c = currentState;
Iterator it = attributeImpls.values().iterator();
c.attribute = (AttributeImpl) it.next();
while (it.hasNext()) {
c.next = new State();
c = c.next;
c.attribute = (AttributeImpl) it.next();
}
}
/**
* Resets all Attributes in this AttributeSource by calling
* {@link AttributeImpl#clear()} on each Attribute implementation.
*/
public void clearAttributes() {
if (hasAttributes()) {
if (currentState == null) {
computeCurrentState();
}
for (State state = currentState; state != null; state = state.next) {
state.attribute.clear();
}
}
}
/**
* Captures the state of all Attributes. The return value can be passed to
* {@link #restoreState} to restore the state of this or another AttributeSource.
*/
public State captureState() {
if (!hasAttributes()) {
return null;
}
if (currentState == null) {
computeCurrentState();
}
return (State) this.currentState.clone();
}
/**
* Restores this state by copying the values of all attribute implementations
* that this state contains into the attributes implementations of the targetStream.
* The targetStream must contain a corresponding instance for each argument
* contained in this state (e.g. it is not possible to restore the state of
* an AttributeSource containing a TermAttribute into a AttributeSource using
* a Token instance as implementation).
* <p>
* Note that this method does not affect attributes of the targetStream
* that are not contained in this state. In other words, if for example
* the targetStream contains an OffsetAttribute, but this state doesn't, then
* the value of the OffsetAttribute remains unchanged. It might be desirable to
* reset its value to the default, in which case the caller should first
* call {@link TokenStream#clearAttributes()} on the targetStream.
*/
public void restoreState(State state) {
if (state == null) return;
do {
AttributeImpl targetImpl = (AttributeImpl) attributeImpls.get(state.attribute.getClass());
if (targetImpl == null)
throw new IllegalArgumentException("State contains an AttributeImpl that is not in this AttributeSource");
state.attribute.copyTo(targetImpl);
state = state.next;
} while (state != null);
}
public int hashCode() {
int code = 0;
if (hasAttributes()) {
if (currentState == null) {
computeCurrentState();
}
for (State state = currentState; state != null; state = state.next) {
code = code * 31 + state.attribute.hashCode();
}
}
return code;
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof AttributeSource) {
AttributeSource other = (AttributeSource) obj;
if (hasAttributes()) {
if (!other.hasAttributes()) {
return false;
}
if (this.attributeImpls.size() != other.attributeImpls.size()) {
return false;
}
// it is only equal if all attribute impls are the same in the same order
if (this.currentState == null) {
this.computeCurrentState();
}
State thisState = this.currentState;
if (other.currentState == null) {
other.computeCurrentState();
}
State otherState = other.currentState;
while (thisState != null && otherState != null) {
if (otherState.attribute.getClass() != thisState.attribute.getClass() || !otherState.attribute.equals(thisState.attribute)) {
return false;
}
thisState = thisState.next;
otherState = otherState.next;
}
return true;
} else {
return !other.hasAttributes();
}
} else
return false;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append('(');
if (hasAttributes()) {
if (currentState == null) {
computeCurrentState();
}
for (State state = currentState; state != null; state = state.next) {
if (state != currentState) sb.append(',');
sb.append(state.attribute.toString());
}
}
sb.append(')');
return sb.toString();
}
/**
* Performs a clone of all {@link AttributeImpl} instances returned in a new
* AttributeSource instance. This method can be used to e.g. create another TokenStream
* with exactly the same attributes (using {@link #AttributeSource(AttributeSource)})
*/
public AttributeSource cloneAttributes() {
AttributeSource clone = new AttributeSource(this.factory);
// first clone the impls
if (hasAttributes()) {
if (currentState == null) {
computeCurrentState();
}
for (State state = currentState; state != null; state = state.next) {
clone.attributeImpls.put(state.attribute.getClass(), state.attribute.clone());
}
}
// now the interfaces
Iterator/*<Entry<Class<Attribute>, AttributeImpl>>*/ attIt = this.attributes.entrySet().iterator();
while (attIt.hasNext()) {
Entry/*<Class<Attribute>, AttributeImpl>*/ entry = (Entry/*<Class<Attribute>, AttributeImpl>*/) attIt.next();
clone.attributes.put(entry.getKey(), clone.attributeImpls.get(entry.getValue().getClass()));
}
return clone;
}
}
发表评论
-
基于P2P的Web搜索强于集中式搜索引擎?
2011-02-14 22:51 1088搜索引擎已经成为一种重要的网络信息导航工具,它帮助人们在 ... -
Java陷阱之assert关键字
2010-09-04 14:48 802Java陷阱之assert关键字 ... -
standford vs opennlp
2010-09-04 06:59 4986重新训练的模型主要针对短角色,即词串数不大于3的角色,这是 ... -
Lucene Payload 的研究与应用
2010-09-02 21:51 897http://www.ibm.com/developerwor ... -
standardtokenizer
2010-09-02 14:50 1057/** * Licensed to the Apache S ... -
token
2010-09-02 14:37 1103package org.apache.lucene.analy ... -
改写lucene的Analyzer,添加自己的中文分词系统的方法
2010-09-02 12:44 1258/** *作者:夺天策 ... -
Apache Lucene - Index File Formats
2010-09-01 10:34 985http://lucene.apache.org/java/3 ... -
[zz]学习lucene应该多看源代码
2010-08-31 14:45 1103最近在为星网将要上线的商城系统开发搜索功能,要求使用lucen ... -
How to make indexing faster
2010-08-23 09:02 773Here are some things to try to ...
相关推荐
内容概要:本文详细介绍了基于MATLAB/Simulink的电动助力转向系统(EPS)模型的构建及其控制方法。首先,文中阐述了EPS在提升驾驶体验和安全性方面的重要意义。接着,重点讲解了四个关键模型的搭建:整车二自由度模型用于研究车辆转向特性;助力特性曲线模型确定不同驾驶条件下助力电机提供的助力力矩;助力电机模型模拟助力电机的工作过程;齿条模型描述助力电机转矩转化为车轮转向的动作。每个模型都有具体的参数设定和代码示例。此外,文章还解释了模型的输入(如前轮转角、方向盘力矩)和输出(转向助力力矩),并指出控制方法基于各模型间的输入输出关系,利用基本数学公式和逻辑判断实现。 适用人群:汽车工程领域的研究人员、工程师和技术爱好者。 使用场景及目标:适用于希望深入了解EPS工作原理的研究人员,以及需要进行EPS系统设计和优化的工程师。目标是掌握EPS系统的建模方法和控制策略,为实际项目提供理论支持和技术指导。 其他说明:文中提供了丰富的代码片段和详细的模型介绍,有助于读者更好地理解和实践。同时强调了EPS对于提高驾驶安全性和舒适性的重要性。
实训商业源码-帝国cms7.5 7.2 UTF-8移动端同步插件-酷网站-论文模板.zip
内容概要:本文详细介绍了基于Lasso分位数回归的数据回归预测方法。首先阐述了Lasso分位数回归作为一种结合Lasso回归与分位数回归的统计方法,能够在处理变量选择和模型复杂度方面发挥重要作用。接着解释了其基本原理,即在分位数回归基础上加入Lasso正则化项,从而确保模型既能良好拟合数据,又能有效避免过拟合现象。随后讨论了具体实施流程,从数据预处理到最终预测,涵盖了特征选择、模型构建以及参数优化等多个环节。最后强调了该方法在多个行业(如金融、医疗)的实际应用场景及其潜在价值。 适合人群:对统计学、机器学习有一定了解的研究人员和技术爱好者。 使用场景及目标:适用于需要精确预测并同时考虑多维度因素影响的场合,特别是在面对高维数据时,希望通过减少冗余变量来提高预测准确性的情况。 其他说明:文中提到的方法不仅限于特定领域,而是可以在多种不同类型的预测任务中发挥作用,为决策提供科学依据。
这段代码实现了一个 三维状态的扩展卡尔曼滤波 (Extended Kalman Filter, EKF) 算法。通过生成过程噪声和观测噪声,对真实状态进行滤波估计,同时对比了滤波前后状态量的误差和误差累积分布曲线。 只有一个m文件,下载后使用MATLAB打开运行即可,带误差输出。
毕业设计-百川多公众号集字福袋 2.0.5开源-整站商业源码.zip
实训商业源码-多商家营销活动平台V1.3.9小程序前后端完整全开源解密源码-论文模板.zip
ISC大作业论文
毕业论文-在线进销存-整站商业源码.zip
毕业设计-步数宝步数换购小程序 7.8.1-整站商业源码.zip
实训商业源码-叮咚-门店会员卡小程序4.8.2开源-论文模板.zip
毕业论文-芸众圈子社区V1.7.6 开源版-整站商业源码.zip
内容概要:本文探讨了多智能体强化学习(MARL)在配电网有功电压控制中的应用。文中介绍了将电压约束转化为势垒函数的方法,并在Dec-POMDP框架下对七种最先进的MARL算法进行了大规模实验。实验表明,设计合理的电压势垒函数对于提高电压控制效果至关重要。此外,作者还建立了开源环境,旨在促进电力社区和MARL社区的合作,推动MARL算法的实际应用。 适合人群:从事电力系统自动化、智能电网研究的专业人士,以及对多智能体系统和强化学习感兴趣的科研人员。 使用场景及目标:适用于需要优化配电网电压控制的场景,特别是希望通过软件手段而非硬件升级来提升电力质量和缓解电力拥塞的情况。目标是展示MARL在电力系统中的潜力,并为后续研究提供工具和支持。 其他说明:文章不仅讨论了理论和技术细节,还包括大量代码片段,帮助读者理解和实践MARL在电压控制中的具体应用。
内容概要:本文基于PFC3D(Particle Flow Code 3D)软件,详细探讨了岩石注浆过程中的破坏现象及其背后的机理。首先介绍了注浆破坏的复杂性,指出这是由材料特性、地质构造和计算机模拟技术共同决定的。接着重点讲解了注浆速度和流量的调整方法,强调适当的速度和流量对于确保注浆效率和避免过度破坏的重要性。最后讨论了在不考虑渗流场的情况下,如何根据岩石结构特征选择最佳的注浆孔位置,以提高注浆效果并保护周围岩石结构。 适合人群:从事地质工程领域的研究人员和技术人员,尤其是那些希望深入了解岩石注浆过程的人。 使用场景及目标:适用于需要利用PFC3D进行岩石注浆模拟的研究项目,旨在帮助用户掌握注浆速度、流量调节技巧以及合理的注浆孔位选择方法。 其他说明:文中提供了简单的PFC3D模拟代码框架,便于读者快速上手实践。同时提醒读者注意实际操作时应结合实验室理论模型和现场具体情况来进行参数优化。
内容概要:本文详细介绍了IEEE标准节点仿真模型系列,涵盖了从简单到复杂的多个节点配置,如2机5节点、6节点、3机9节点、13节点、5机14节点、15节点、30节点、33节点、34节点、10机39节点以及69节点。所有模型均已成功调试并实现了潮流计算,适用于短路仿真、稳定性研究和电能质量研究等领域。文中还特别强调了三相等效电源的应用,这是模拟真实电力系统的关键要素之一。 适合人群:从事电力系统研究、仿真和优化的专业人士和技术人员。 使用场景及目标:①用于电力系统短路仿真的建模与分析;②评估电力系统的稳定性和可靠性;③研究电能质量问题,提升电力设备的运行效率和寿命。 阅读建议:本文提供了丰富的背景知识和具体应用场景,建议读者结合实际项目需求选择合适的模型进行深入研究和应用。
实训商业源码-【超人】积分商城 5.2.26-论文模板.zip
实训商业源码-思创兼职小程序V6.7.6 开源版-论文模板.zip
2025年手绘风格毕业设计答辩模板范文
内容概要:本文档详细介绍了使用C语言实现常用的数据结构和算法。首先阐述了算法与数据结构的重要性,并具体讲解了链表、栈、队列、二叉树、图等数据结构的实现方法及其操作函数。接着深入探讨了快速排序和二分查找这两种高效的排序与查找算法,提供了完整的代码示例并解释了每个部分的作用。最后还讨论了图结构的深度优先搜索(DFS)和广度优先搜索(BFS)遍历算法,强调了内存管理和防御性编程的重要性。所有代码示例均可直接编译运行,建议在Linux环境下使用gcc编译测试。 适合人群:具备一定编程基础,尤其是熟悉C语言的初学者或有一定经验的研发人员。 使用场景及目标:①帮助读者理解并掌握常见的数据结构(如链表、栈、队列、二叉树、图)及其基本操作;②通过实际编码练习提高读者对经典算法(如快速排序、二分查找)的理解;③培养良好的编程习惯,如内存管理和防御性编程。 阅读建议:由于文档包含大量代码片段和详细的实现步骤,读者应边阅读边动手实践,尝试编译和运行提供的代码示例,同时注意理解每段代码背后的逻辑和设计思想。此外,建议读者关注文档中提到的编程规范和最佳实践,以提升自身的编程技能。
毕业论文-源导航V1.0-整站商业源码.zip
毕业论文-咻一咻抽奖V4.3.1 开源版-整站商业源码.zip