< prev index next >

src/java.base/share/classes/java/text/AttributedString.java

Print this page




 315 
 316         if (attribute == null) {
 317             throw new NullPointerException();
 318         }
 319 
 320         int len = length();
 321         if (len == 0) {
 322             throw new IllegalArgumentException("Can't add attribute to 0-length text");
 323         }
 324 
 325         addAttributeImpl(attribute, value, 0, len);
 326     }
 327 
 328     /**
 329      * Adds an attribute to a subrange of the string.
 330      * @param attribute the attribute key
 331      * @param value The value of the attribute. May be null.
 332      * @param beginIndex Index of the first character of the range.
 333      * @param endIndex Index of the character following the last character of the range.
 334      * @exception NullPointerException if <code>attribute</code> is null.
 335      * @exception IllegalArgumentException if beginIndex is less then 0, endIndex is
 336      * greater than the length of the string, or beginIndex and endIndex together don't
 337      * define a non-empty subrange of the string.
 338      */
 339     public void addAttribute(Attribute attribute, Object value,
 340             int beginIndex, int endIndex) {
 341 
 342         if (attribute == null) {
 343             throw new NullPointerException();
 344         }
 345 
 346         if (beginIndex < 0 || endIndex > length() || beginIndex >= endIndex) {
 347             throw new IllegalArgumentException("Invalid substring range");
 348         }
 349 
 350         addAttributeImpl(attribute, value, beginIndex, endIndex);
 351     }
 352 
 353     /**
 354      * Adds a set of attributes to a subrange of the string.
 355      * @param attributes The attributes to be added to the string.
 356      * @param beginIndex Index of the first character of the range.
 357      * @param endIndex Index of the character following the last
 358      * character of the range.
 359      * @exception NullPointerException if <code>attributes</code> is null.
 360      * @exception IllegalArgumentException if beginIndex is less then
 361      * 0, endIndex is greater than the length of the string, or
 362      * beginIndex and endIndex together don't define a non-empty
 363      * subrange of the string and the attributes parameter is not an
 364      * empty Map.
 365      */
 366     public void addAttributes(Map<? extends Attribute, ?> attributes,
 367                               int beginIndex, int endIndex)
 368     {
 369         if (attributes == null) {
 370             throw new NullPointerException();
 371         }
 372 
 373         if (beginIndex < 0 || endIndex > length() || beginIndex > endIndex) {
 374             throw new IllegalArgumentException("Invalid substring range");
 375         }
 376         if (beginIndex == endIndex) {
 377             if (attributes.isEmpty())
 378                 return;
 379             throw new IllegalArgumentException("Can't add attribute to 0-length text");
 380         }


 563      *
 564      * @param attributes a list of attributes that the client is interested in
 565      * @return an iterator providing access to the entire text and its selected attributes
 566      */
 567     public AttributedCharacterIterator getIterator(Attribute[] attributes) {
 568         return getIterator(attributes, 0, length());
 569     }
 570 
 571     /**
 572      * Creates an AttributedCharacterIterator instance that provides access to
 573      * selected contents of this string.
 574      * Information about attributes not listed in attributes that the
 575      * implementor may have need not be made accessible through the iterator.
 576      * If the list is null, all available attribute information should be made
 577      * accessible.
 578      *
 579      * @param attributes a list of attributes that the client is interested in
 580      * @param beginIndex the index of the first character
 581      * @param endIndex the index of the character following the last character
 582      * @return an iterator providing access to the text and its attributes
 583      * @exception IllegalArgumentException if beginIndex is less then 0,
 584      * endIndex is greater than the length of the string, or beginIndex is
 585      * greater than endIndex.
 586      */
 587     public AttributedCharacterIterator getIterator(Attribute[] attributes, int beginIndex, int endIndex) {
 588         return new AttributedStringIterator(attributes, beginIndex, endIndex);
 589     }
 590 
 591     // all (with the exception of length) reading operations are private,
 592     // since AttributedString instances are accessed through iterators.
 593 
 594     // length is package private so that CharacterIteratorFieldDelegate can
 595     // access it without creating an AttributedCharacterIterator.
 596     int length() {
 597         return text.length();
 598     }
 599 
 600     private char charAt(int index) {
 601         return text.charAt(index);
 602     }
 603 




 315 
 316         if (attribute == null) {
 317             throw new NullPointerException();
 318         }
 319 
 320         int len = length();
 321         if (len == 0) {
 322             throw new IllegalArgumentException("Can't add attribute to 0-length text");
 323         }
 324 
 325         addAttributeImpl(attribute, value, 0, len);
 326     }
 327 
 328     /**
 329      * Adds an attribute to a subrange of the string.
 330      * @param attribute the attribute key
 331      * @param value The value of the attribute. May be null.
 332      * @param beginIndex Index of the first character of the range.
 333      * @param endIndex Index of the character following the last character of the range.
 334      * @exception NullPointerException if <code>attribute</code> is null.
 335      * @exception IllegalArgumentException if beginIndex is less than 0, endIndex is
 336      * greater than the length of the string, or beginIndex and endIndex together don't
 337      * define a non-empty subrange of the string.
 338      */
 339     public void addAttribute(Attribute attribute, Object value,
 340             int beginIndex, int endIndex) {
 341 
 342         if (attribute == null) {
 343             throw new NullPointerException();
 344         }
 345 
 346         if (beginIndex < 0 || endIndex > length() || beginIndex >= endIndex) {
 347             throw new IllegalArgumentException("Invalid substring range");
 348         }
 349 
 350         addAttributeImpl(attribute, value, beginIndex, endIndex);
 351     }
 352 
 353     /**
 354      * Adds a set of attributes to a subrange of the string.
 355      * @param attributes The attributes to be added to the string.
 356      * @param beginIndex Index of the first character of the range.
 357      * @param endIndex Index of the character following the last
 358      * character of the range.
 359      * @exception NullPointerException if <code>attributes</code> is null.
 360      * @exception IllegalArgumentException if beginIndex is less than
 361      * 0, endIndex is greater than the length of the string, or
 362      * beginIndex and endIndex together don't define a non-empty
 363      * subrange of the string and the attributes parameter is not an
 364      * empty Map.
 365      */
 366     public void addAttributes(Map<? extends Attribute, ?> attributes,
 367                               int beginIndex, int endIndex)
 368     {
 369         if (attributes == null) {
 370             throw new NullPointerException();
 371         }
 372 
 373         if (beginIndex < 0 || endIndex > length() || beginIndex > endIndex) {
 374             throw new IllegalArgumentException("Invalid substring range");
 375         }
 376         if (beginIndex == endIndex) {
 377             if (attributes.isEmpty())
 378                 return;
 379             throw new IllegalArgumentException("Can't add attribute to 0-length text");
 380         }


 563      *
 564      * @param attributes a list of attributes that the client is interested in
 565      * @return an iterator providing access to the entire text and its selected attributes
 566      */
 567     public AttributedCharacterIterator getIterator(Attribute[] attributes) {
 568         return getIterator(attributes, 0, length());
 569     }
 570 
 571     /**
 572      * Creates an AttributedCharacterIterator instance that provides access to
 573      * selected contents of this string.
 574      * Information about attributes not listed in attributes that the
 575      * implementor may have need not be made accessible through the iterator.
 576      * If the list is null, all available attribute information should be made
 577      * accessible.
 578      *
 579      * @param attributes a list of attributes that the client is interested in
 580      * @param beginIndex the index of the first character
 581      * @param endIndex the index of the character following the last character
 582      * @return an iterator providing access to the text and its attributes
 583      * @exception IllegalArgumentException if beginIndex is less than 0,
 584      * endIndex is greater than the length of the string, or beginIndex is
 585      * greater than endIndex.
 586      */
 587     public AttributedCharacterIterator getIterator(Attribute[] attributes, int beginIndex, int endIndex) {
 588         return new AttributedStringIterator(attributes, beginIndex, endIndex);
 589     }
 590 
 591     // all (with the exception of length) reading operations are private,
 592     // since AttributedString instances are accessed through iterators.
 593 
 594     // length is package private so that CharacterIteratorFieldDelegate can
 595     // access it without creating an AttributedCharacterIterator.
 596     int length() {
 597         return text.length();
 598     }
 599 
 600     private char charAt(int index) {
 601         return text.charAt(index);
 602     }
 603 


< prev index next >