attClass) {
+ AttributeImpl attImpl = attributes.get(attClass);
+ if (attImpl == null) {
+ addAttributeImpl(attImpl = this.factory.createAttributeInstance(attClass));
}
+ // the following cast is unchecked, because compiler does not know if attImpl really implements A
+ @SuppressWarnings("unchecked") final A att = (A) attImpl;
+ return att;
}
/** Returns true, iff this AttributeSource has any attributes */
@@ -237,16 +231,14 @@
/**
* The caller must pass in a Class<? extends Attribute> value.
* Returns true, iff this AttributeSource contains the passed-in Attribute.
- * Signature for Java 1.5: public boolean hasAttribute(Class<? extends Attribute>)
*/
- public boolean hasAttribute(Class attClass) {
+ public boolean hasAttribute(Class extends Attribute> 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
- *
Signature for Java 1.5: public <T extends Attribute> T getAttribute(Class<T>)
*
* @throws IllegalArgumentException if this AttributeSource does not contain the
* Attribute. It is recommended to always use {@link #addAttribute} even in consumers
@@ -255,8 +247,9 @@
* 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);
+ public A getAttribute(Class attClass) {
+ // the following cast is unchecked, because compiler does not know if implementation really implements A
+ @SuppressWarnings("unchecked") final A att = (A) this.attributes.get(attClass);
if (att == null) {
throw new IllegalArgumentException("This AttributeSource does not have the attribute '" + attClass.getName() + "'.");
}
@@ -290,12 +283,12 @@
private void computeCurrentState() {
currentState = new State();
State c = currentState;
- Iterator it = attributeImpls.values().iterator();
- c.attribute = (AttributeImpl) it.next();
+ final Iterator it = attributeImpls.values().iterator();
+ c.attribute = it.next();
while (it.hasNext()) {
c.next = new State();
c = c.next;
- c.attribute = (AttributeImpl) it.next();
+ c.attribute = it.next();
}
}
@@ -348,7 +341,7 @@
if (state == null) return;
do {
- AttributeImpl targetImpl = (AttributeImpl) attributeImpls.get(state.attribute.getClass());
+ AttributeImpl targetImpl = 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);
@@ -412,9 +405,7 @@
}
public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append('(');
-
+ StringBuilder sb = new StringBuilder().append('(');
if (hasAttributes()) {
if (currentState == null) {
computeCurrentState();
@@ -424,8 +415,7 @@
sb.append(state.attribute.toString());
}
}
- sb.append(')');
- return sb.toString();
+ return sb.append(')').toString();
}
/**
@@ -442,14 +432,12 @@
computeCurrentState();
}
for (State state = currentState; state != null; state = state.next) {
- clone.attributeImpls.put(state.attribute.getClass(), state.attribute.clone());
+ clone.attributeImpls.put(state.attribute.getClass(), (AttributeImpl) state.attribute.clone());
}
}
// now the interfaces
- Iterator/*, AttributeImpl>>*/ attIt = this.attributes.entrySet().iterator();
- while (attIt.hasNext()) {
- Entry/*, AttributeImpl>*/ entry = (Entry/*, AttributeImpl>*/) attIt.next();
+ for (Entry, AttributeImpl> entry : this.attributes.entrySet()) {
clone.attributes.put(entry.getKey(), clone.attributeImpls.get(entry.getValue().getClass()));
}
Index: src/test/org/apache/lucene/util/TestAttributeSource.java
===================================================================
--- src/test/org/apache/lucene/util/TestAttributeSource.java (revision 809286)
+++ src/test/org/apache/lucene/util/TestAttributeSource.java (working copy)
@@ -27,8 +27,8 @@
public void testCaptureState() {
// init a first instance
AttributeSource src = new AttributeSource();
- TermAttribute termAtt = (TermAttribute) src.addAttribute(TermAttribute.class);
- TypeAttribute typeAtt = (TypeAttribute) src.addAttribute(TypeAttribute.class);
+ TermAttribute termAtt = src.addAttribute(TermAttribute.class);
+ TypeAttribute typeAtt = src.addAttribute(TypeAttribute.class);
termAtt.setTermBuffer("TestTerm");
typeAtt.setType("TestType");
final int hashCode = src.hashCode();
@@ -55,9 +55,9 @@
// init a second instance (with attributes in different order and one additional attribute)
AttributeSource src2 = new AttributeSource();
- typeAtt = (TypeAttribute) src2.addAttribute(TypeAttribute.class);
- FlagsAttribute flagsAtt = (FlagsAttribute) src2.addAttribute(FlagsAttribute.class);
- termAtt = (TermAttribute) src2.addAttribute(TermAttribute.class);
+ typeAtt = src2.addAttribute(TypeAttribute.class);
+ FlagsAttribute flagsAtt = src2.addAttribute(FlagsAttribute.class);
+ termAtt = src2.addAttribute(TermAttribute.class);
flagsAtt.setFlags(12345);
src2.restoreState(state);
@@ -67,7 +67,7 @@
// init a third instance missing one Attribute
AttributeSource src3 = new AttributeSource();
- termAtt = (TermAttribute) src3.addAttribute(TermAttribute.class);
+ termAtt = src3.addAttribute(TermAttribute.class);
try {
src3.restoreState(state);
fail("The third instance is missing the TypeAttribute, so restoreState() should throw IllegalArgumentException");
@@ -78,19 +78,19 @@
public void testCloneAttributes() {
final AttributeSource src = new AttributeSource();
- final TermAttribute termAtt = (TermAttribute) src.addAttribute(TermAttribute.class);
- final TypeAttribute typeAtt = (TypeAttribute) src.addAttribute(TypeAttribute.class);
+ final TermAttribute termAtt = src.addAttribute(TermAttribute.class);
+ final TypeAttribute typeAtt = src.addAttribute(TypeAttribute.class);
termAtt.setTermBuffer("TestTerm");
typeAtt.setType("TestType");
final AttributeSource clone = src.cloneAttributes();
- final Iterator it = clone.getAttributeClassesIterator();
+ final Iterator> it = clone.getAttributeClassesIterator();
assertEquals("TermAttribute must be the first attribute", TermAttribute.class, it.next());
assertEquals("TypeAttribute must be the second attribute", TypeAttribute.class, it.next());
assertFalse("No more attributes", it.hasNext());
- final TermAttribute termAtt2 = (TermAttribute) clone.getAttribute(TermAttribute.class);
- final TypeAttribute typeAtt2 = (TypeAttribute) clone.getAttribute(TypeAttribute.class);
+ final TermAttribute termAtt2 = clone.getAttribute(TermAttribute.class);
+ final TypeAttribute typeAtt2 = clone.getAttribute(TypeAttribute.class);
assertNotSame("TermAttribute of original and clone must be different instances", termAtt2, termAtt);
assertNotSame("TypeAttribute of original and clone must be different instances", typeAtt2, typeAtt);
assertEquals("TermAttribute of original and clone must be equal", termAtt2, termAtt);
@@ -99,12 +99,12 @@
public void testToStringAndMultiAttributeImplementations() {
AttributeSource src = new AttributeSource();
- TermAttribute termAtt = (TermAttribute) src.addAttribute(TermAttribute.class);
- TypeAttribute typeAtt = (TypeAttribute) src.addAttribute(TypeAttribute.class);
+ TermAttribute termAtt = src.addAttribute(TermAttribute.class);
+ TypeAttribute typeAtt = src.addAttribute(TypeAttribute.class);
termAtt.setTermBuffer("TestTerm");
typeAtt.setType("TestType");
assertEquals("Attributes should appear in original order", "("+termAtt.toString()+","+typeAtt.toString()+")", src.toString());
- Iterator it = src.getAttributeImplsIterator();
+ Iterator it = src.getAttributeImplsIterator();
assertTrue("Iterator should have 2 attributes left", it.hasNext());
assertSame("First AttributeImpl from iterator should be termAtt", termAtt, it.next());
assertTrue("Iterator should have 1 attributes left", it.hasNext());
@@ -114,7 +114,7 @@
src = new AttributeSource();
src.addAttributeImpl(new Token());
// this should not add a new attribute as Token implements TermAttribute, too
- termAtt = (TermAttribute) src.addAttribute(TermAttribute.class);
+ termAtt = src.addAttribute(TermAttribute.class);
assertTrue("TermAttribute should be implemented by Token", termAtt instanceof Token);
// get the Token attribute and check, that it is the only one
it = src.getAttributeImplsIterator();