4

Suppose you assign a custom CharacterIterator.Attribute to the first five characters of a ten-character string.

Suppose further you assign a different CharacterIterator.Attribute to the remaining characters.

Why then, when I call AttributedString.getRunStart(firstAttribute) do I get 0 (I expect this) and when I call AttributedString.getRunStart(secondAttribute) do I also get 0?

Here's my setup code:

final AttributedString s = new AttributedString("SQ3R9FFFFF");
final Attribute baseID = new Attribute("Base ID") {};
final Attribute fs = new Attribute("FFF") {};
s.addAttribute(baseID, "Ignored", 0, 5);
s.addAttribute(fs, "Whatever", 5, 10);
final AttributedCharacterIterator iterator = s.getIterator();
assertNotNull(iterator);

And now here's some code that outputs some diagnostics:

for (char c = iterator.first(); c != DONE; c = iterator.next()) {
  System.out.println("Character: " + c);
  System.out.println("Character index: " + iterator.getIndex());
  System.out.println("Attributes: " + iterator.getAttributes());
  System.out.println("Start for baseID: " + iterator.getRunStart(baseID));
  System.out.println("Limit for baseID: " + iterator.getRunLimit(baseID));
  System.out.println("Start for fs: " + iterator.getRunStart(fs));
  System.out.println("Limit for fs: " + iterator.getRunLimit(fs));
}

The output is this:

 Character: S
 Character index: 0
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
 Start for baseID: 0
 Limit for baseID: 5
 Start for fs: 0
 Limit for fs: 5
 Character: Q
 Character index: 1
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
 Start for baseID: 0
 Limit for baseID: 5
 Start for fs: 0
 Limit for fs: 5
 Character: 3
 Character index: 2
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
 Start for baseID: 0
 Limit for baseID: 5
 Start for fs: 0
 Limit for fs: 5
 Character: R
 Character index: 3
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
 Start for baseID: 0
 Limit for baseID: 5
 Start for fs: 0
 Limit for fs: 5
 Character: 9
 Character index: 4
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
 Start for baseID: 0
 Limit for baseID: 5
 Start for fs: 0
 Limit for fs: 5
 Character: F
 Character index: 5
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
 Start for baseID: 5
 Limit for baseID: 10
 Start for fs: 5
 Limit for fs: 10
 Character: F
 Character index: 6
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
 Start for baseID: 5
 Limit for baseID: 10
 Start for fs: 5
 Limit for fs: 10
 Character: F
 Character index: 7
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
 Start for baseID: 5
 Limit for baseID: 10
 Start for fs: 5
 Limit for fs: 10
 Character: F
 Character index: 8
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
 Start for baseID: 5
 Limit for baseID: 10
 Start for fs: 5
 Limit for fs: 10
 Character: F
 Character index: 9
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
 Start for baseID: 5
 Limit for baseID: 10
 Start for fs: 5
 Limit for fs: 10

Note, in particular, the last entry, which reports that the "Start" for "baseID" is 5. Huh?

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79
Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
  • Why do you think it has anything to do with i18n? I can't see a connection... – Paweł Dyda May 13 '11 at 20:30
  • It's in the java.text package, which is most commonly used for I18N. I don't have enough reputation points to coin a new tag, and the "java" tag is so enormous that it is essentially useless. – Laird Nelson May 13 '11 at 20:35
  • java.text is related to text manipulation. It just happens that is quite common in i18n. However, many i18n-related classes could be found in java.util, and you wouldn't call this package i18n-related, right? As for tagging, it should be related to your problem and targeted to reach as broad audience as it is possible. – Paweł Dyda May 14 '11 at 02:17
  • If you really want to have your question answered, re-tagging won't do. I'd rather go with a bounty. – Paweł Dyda May 14 '11 at 02:18
  • My apologies; I will remove the tag. – Laird Nelson May 19 '11 at 15:37

1 Answers1

0

Per the Javadoc:

A run with respect to an attribute is a maximum text range for which:

  • the attribute is undefined or null for the entire range, or
  • the attribute value is defined and has the same non-null value for the entire range.

I think it may be the first bullet point. For characters 0-4, the fs attribute is undefined, so it's a valid range. baseID is defined, and also valid.

http://download.oracle.com/javase/1,5.0/docs/api/java/text/AttributedCharacterIterator.html

Community
  • 1
  • 1
Bill Brasky
  • 2,614
  • 2
  • 19
  • 20