Java Code Examples for java.text.AttributedString

Following code examples demonstrate how to use java.text.AttributedStringfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use java.text.AttributedStringand various code implementation of this class.

    @Test
    public default void verifyGenericType()
    throws Exception {
        final Class<SUT> sut = createNewSUT();
        assertTrue("This j8unit test interface is used with a generic type that is illegaly not assignable to AttributedString.class!",
                   AttributedString.class.isAssignableFrom(sut));
    } 


    public void test_ConstructorLjava_lang_String() {
        String test = "Test string";
        AttributedString attrString = new AttributedString(test);
        assertEqualString("String must match!", test, attrString);
    } 

    public void test_ConstructorLAttributedCharacterIterator_1() {
        String testString = "Test string";
        AttributedString attrString = new AttributedString(testString);
        AttributedCharacterIterator iter = attrString.getIterator();
        AttributedString attrString2 = new AttributedString(iter);
        assertEqualString("String must match!", testString, attrString2);
    } 

    public void test_getIterator() {
        String test = "Test string";
        try {
            AttributedString attrString = new AttributedString(test);
            AttributedCharacterIterator it = attrString.getIterator();
            assertEquals("Incorrect iteration on AttributedString", it.first(),
                    test.charAt(0));
        } catch (Exception e) {
            fail("Unexpected exceptiption " + e.toString());
        }
    } 

    public void test_ConstructorLjava_lang_StringLjava_util_Map() {
        String test = "Test string";

        try {
            AttributedString attrString = new AttributedString(
                    test,
                    new WeakHashMap<AttributedCharacterIterator.Attribute, String>());
            AttributedCharacterIterator it = attrString.getIterator();
            StringBuffer buf = new StringBuffer();
            buf.append(it.first());
            char ch;
            while ((ch = it.next()) != CharacterIterator.DONE)
                buf.append(ch);
            assertTrue("Wrong string: " + buf, buf.toString().equals(test));
        } catch (Exception e) {
            fail("Unexpected exception " + e.toString());
        }

        try {
            Map<AttributedCharacterIterator.Attribute, String> whm = new WeakHashMap<AttributedCharacterIterator.Attribute, String>();
            whm.put(new TestAttributedCharacterIteratorAttribute("test"),
                    "value");
            new AttributedString("", whm);
            fail("Expected IllegalArgumentException was not thrown");
        } catch (Exception e) {
        }
    } 

    public void test_ConstructorLAttributedCharacterIterator_2() {
        String testString = "Test string";
        AttributedString attrString = new AttributedString(testString);
        AttributedCharacterIterator iter = attrString.getIterator();
        AttributedString attrString2 = new AttributedString(iter, 2, 7);
        assertEqualString("String must match!", "st st", attrString2);
    } 

    static void assertEqualString (String msg, String expected, AttributedString attrString) {
        AttributedCharacterIterator it = attrString.getIterator();
        StringBuffer buf = new StringBuffer();
        buf.append(it.first());
        char ch;
        while ((ch = it.next()) != CharacterIterator.DONE)
            buf.append(ch);
        assertEquals(msg, expected, buf.toString());
    } 

    public void test_ConstructorLAttributedCharacterIterator_3() {
        String testString = "Test string";
        AttributedString attrString = new AttributedString(testString);
        AttributedCharacterIterator iter = attrString.getIterator();
        AttributedString attrString2;

        attrString2 = new AttributedString(iter, 2, 7, new AttributedCharacterIterator.Attribute[] {});
        assertEqualString("String must match!", "st st", attrString2);

        attrString2 = new AttributedString(iter, 2, 7, null);
        assertEqualString("String must match!", "st st", attrString2);
    } 

    public void test_ConstructorLAttributedCharacterIterator() {
        assertNotNull(new AttributedString(
                new testAttributedCharacterIterator()));
    } 

    public void test_addAttributesLjava_util_MapII() {
        AttributedString as = new AttributedString("test");
        Map<AttributedCharacterIterator.Attribute, String> whm = new WeakHashMap<AttributedCharacterIterator.Attribute, String>();

        try {
            whm.put(new TestAttributedCharacterIteratorAttribute("test1"),
                    "value1");
            whm.put(new TestAttributedCharacterIteratorAttribute("test2"),
                    "value2");
            whm.put(new TestAttributedCharacterIteratorAttribute("test3"),
                    "value3");
            as.addAttributes(whm, 0, 3);
        } catch (Exception e) {
            fail("Unexpected exception " + e.toString());
        }

        try {
            as.addAttributes(null, 0, 3);
            fail("Expected NullPointerException was not thrown");
        } catch (NullPointerException e) {
        }

        try {
            as.addAttributes(whm, 0, 0);
            fail("Expected IllegalArgumentException was not thrown");
        } catch (IllegalArgumentException e) {
        }
    } 

    public void test_ConstructorLAttributedCharacterIteratorII() {

        try {
            new AttributedString(new testAttributedCharacterIterator(), 0, 0);
        } catch (Exception e) {
            fail("Unexpected exception " + e.toString());
        }

        try {
            new AttributedString(new testAttributedCharacterIterator(), -1, 0);
            fail("Expected IllegalArgumentException was not thrown");
        } catch (IllegalArgumentException e) {
        }

        try {
            new AttributedString(new testAttributedCharacterIterator(), 0, -1);
            fail("Expected IllegalArgumentException was not thrown");
        } catch (IllegalArgumentException e) {
        }
    } 

    public void test_ConstructorLAttributedCharacterIterator_3() {
        String testString = "Test string";
        AttributedString attrString = new AttributedString(testString);
        AttributedCharacterIterator iter = attrString.getIterator();
        AttributedString attrString2;

        attrString2 = new AttributedString(iter, 2, 7, new AttributedCharacterIterator.Attribute[] {});
        assertEqualString("String must match!", "st st", attrString2);

        attrString2 = new AttributedString(iter, 2, 7, null);
        assertEqualString("String must match!", "st st", attrString2);
    } 

    static void assertEqualString (String msg, String expected, AttributedString attrString) {
        AttributedCharacterIterator it = attrString.getIterator();
        StringBuffer buf = new StringBuffer();
        buf.append(it.first());
        char ch;
        while ((ch = it.next()) != CharacterIterator.DONE)
            buf.append(ch);
        assertEquals(msg, expected, buf.toString());
    } 

    public void test_ConstructorLAttributedCharacterIterator_2() {
        String testString = "Test string";
        AttributedString attrString = new AttributedString(testString);
        AttributedCharacterIterator iter = attrString.getIterator();
        AttributedString attrString2 = new AttributedString(iter, 2, 7);
        assertEqualString("String must match!", "st st", attrString2);
    } 

    public void test_ConstructorLjava_lang_StringLjava_util_Map() {
        String test = "Test string";

        try {
            AttributedString attrString = new AttributedString(
                    test,
                    new WeakHashMap<AttributedCharacterIterator.Attribute, String>());
            AttributedCharacterIterator it = attrString.getIterator();
            StringBuffer buf = new StringBuffer();
            buf.append(it.first());
            char ch;
            while ((ch = it.next()) != CharacterIterator.DONE)
                buf.append(ch);
            assertTrue("Wrong string: " + buf, buf.toString().equals(test));
        } catch (Exception e) {
            fail("Unexpected exception " + e.toString());
        }

        try {
            Map<AttributedCharacterIterator.Attribute, String> whm = new WeakHashMap<AttributedCharacterIterator.Attribute, String>();
            whm.put(new TestAttributedCharacterIteratorAttribute("test"),
                    "value");
            new AttributedString("", whm);
            fail("Expected IllegalArgumentException was not thrown");
        } catch (Exception e) {
        }
    } 

    public void test_ConstructorLAttributedCharacterIteratorII() {

        try {
            new AttributedString(new testAttributedCharacterIterator(), 0, 0);
        } catch (Exception e) {
            fail("Unexpected exception " + e.toString());
        }

        try {
            new AttributedString(new testAttributedCharacterIterator(), -1, 0);
            fail("Expected IllegalArgumentException was not thrown");
        } catch (IllegalArgumentException e) {
        }

        try {
            new AttributedString(new testAttributedCharacterIterator(), 0, -1);
            fail("Expected IllegalArgumentException was not thrown");
        } catch (IllegalArgumentException e) {
        }
    } 

    public void test_addAttributesLjava_util_MapII() {
        AttributedString as = new AttributedString("test");
        Map<AttributedCharacterIterator.Attribute, String> whm = new WeakHashMap<AttributedCharacterIterator.Attribute, String>();

        try {
            whm.put(new TestAttributedCharacterIteratorAttribute("test1"),
                    "value1");
            whm.put(new TestAttributedCharacterIteratorAttribute("test2"),
                    "value2");
            whm.put(new TestAttributedCharacterIteratorAttribute("test3"),
                    "value3");
            as.addAttributes(whm, 0, 3);
        } catch (Exception e) {
            fail("Unexpected exception " + e.toString());
        }

        try {
            as.addAttributes(null, 0, 3);
            fail("Expected NullPointerException was not thrown");
        } catch (NullPointerException e) {
        }

        try {
            as.addAttributes(whm, 0, 0);
            fail("Expected IllegalArgumentException was not thrown");
        } catch (IllegalArgumentException e) {
        }
    } 

    public void test_ConstructorLAttributedCharacterIterator_1() {
        String testString = "Test string";
        AttributedString attrString = new AttributedString(testString);
        AttributedCharacterIterator iter = attrString.getIterator();
        AttributedString attrString2 = new AttributedString(iter);
        assertEqualString("String must match!", testString, attrString2);
    } 

    public void test_getIterator() {
        String test = "Test string";
        try {
            AttributedString attrString = new AttributedString(test);
            AttributedCharacterIterator it = attrString.getIterator();
            assertEquals("Incorrect iteration on AttributedString", it.first(),
                    test.charAt(0));
        } catch (Exception e) {
            fail("Unexpected exceptiption " + e.toString());
        }
    } 

        public java.lang.Object get(java.lang.Object r1) {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributeMap.get(java.lang.Object):java.lang.Object");
        } 

    public void addAttributes(java.util.Map<? extends java.text.AttributedCharacterIterator.Attribute, ?> r1, int r2, int r3) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.addAttributes(java.util.Map, int, int):void");
    } 

        public int getIndex() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getIndex():int");
        } 

    private final int ensureRunBreak(int r1, boolean r2) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.ensureRunBreak(int, boolean):int");
    } 

        public boolean equals(java.lang.Object r1) {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.equals(java.lang.Object):boolean");
        } 

        public int getRunLimit(java.text.AttributedCharacterIterator.Attribute r1) {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getRunLimit(java.text.AttributedCharacterIterator$Attribute):int");
        } 

        public int getRunLimit(java.util.Set<? extends java.text.AttributedCharacterIterator.Attribute> r1) {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getRunLimit(java.util.Set):int");
        } 

        public int getRunStart() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getRunStart():int");
        } 

        private java.text.AttributedString getString() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getString():java.text.AttributedString");
        } 

        public java.lang.Object getAttribute(java.text.AttributedCharacterIterator.Attribute r1) {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getAttribute(java.text.AttributedCharacterIterator$Attribute):java.lang.Object");
        } 

        public int getRunStart(java.util.Set<? extends java.text.AttributedCharacterIterator.Attribute> r1) {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getRunStart(java.util.Set):int");
        } 

        public char next() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.next():char");
        } 

        public char previous() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.previous():char");
        } 

        public char current() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.current():char");
        } 

    public void addAttribute(java.text.AttributedCharacterIterator.Attribute r1, java.lang.Object r2, int r3, int r4) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.addAttribute(java.text.AttributedCharacterIterator$Attribute, java.lang.Object, int, int):void");
    } 

        public java.util.Set<java.text.AttributedCharacterIterator.Attribute> getAllAttributeKeys() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getAllAttributeKeys():java.util.Set<java.text.AttributedCharacterIterator$Attribute>");
        } 

        public int getRunLimit() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getRunLimit():int");
        } 

    private final void createRunAttributeDataVectors() {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.createRunAttributeDataVectors():void");
    } 

    private synchronized java.lang.Object getAttribute(java.text.AttributedCharacterIterator.Attribute r1, int r2) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.getAttribute(java.text.AttributedCharacterIterator$Attribute, int):java.lang.Object");
    } 

    public void addAttribute(java.text.AttributedCharacterIterator.Attribute r1, java.lang.Object r2) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.addAttribute(java.text.AttributedCharacterIterator$Attribute, java.lang.Object):void");
    } 

        public int getEndIndex() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getEndIndex():int");
        } 

        public char setIndex(int r1) {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.setIndex(int):char");
        } 

    private synchronized void addAttributeImpl(java.text.AttributedCharacterIterator.Attribute r1, java.lang.Object r2, int r3, int r4) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.addAttributeImpl(java.text.AttributedCharacterIterator$Attribute, java.lang.Object, int, int):void");
    } 

        public int hashCode() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.hashCode():int");
        } 

    private void addAttributeRunData(java.text.AttributedCharacterIterator.Attribute r1, java.lang.Object r2, int r3, int r4) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.addAttributeRunData(java.text.AttributedCharacterIterator$Attribute, java.lang.Object, int, int):void");
    } 

    private final void appendContents(java.lang.StringBuffer r1, java.text.CharacterIterator r2) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.appendContents(java.lang.StringBuffer, java.text.CharacterIterator):void");
    } 

    private java.lang.Object getAttributeCheckRange(java.text.AttributedCharacterIterator.Attribute r1, int r2, int r3, int r4) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.getAttributeCheckRange(java.text.AttributedCharacterIterator$Attribute, int, int, int):java.lang.Object");
    } 

    public java.text.AttributedCharacterIterator getIterator(java.text.AttributedCharacterIterator.Attribute[] r1) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.getIterator(java.text.AttributedCharacterIterator$Attribute[]):java.text.AttributedCharacterIterator");
    } 

        public java.util.Map<java.text.AttributedCharacterIterator.Attribute, java.lang.Object> getAttributes() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getAttributes():java.util.Map<java.text.AttributedCharacterIterator$Attribute, java.lang.Object>");
        } 

        AttributeMap(java.text.AttributedString r1, int r2, int r3, int r4) {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributeMap.<init>(java.text.AttributedString, int, int, int):void");
        } 

        AttributedStringIterator(java.text.AttributedString r1, java.text.AttributedCharacterIterator.Attribute[] r2, int r3, int r4) {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.<init>(java.text.AttributedString, java.text.AttributedCharacterIterator$Attribute[], int, int):void");
        } 

        public char first() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.first():char");
        } 

    int length() {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.length():int");
    } 

    AttributedString(java.text.AttributedCharacterIterator[] r1) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.<init>(java.text.AttributedCharacterIterator[]):void");
    } 

    private char charAt(int r1) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.charAt(int):char");
    } 

    private static final boolean valuesMatch(java.lang.Object r1, java.lang.Object r2) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.valuesMatch(java.lang.Object, java.lang.Object):boolean");
    } 

        public int getRunStart(java.text.AttributedCharacterIterator.Attribute r1) {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getRunStart(java.text.AttributedCharacterIterator$Attribute):int");
        } 

        public java.util.Set entrySet() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributeMap.entrySet():java.util.Set");
        } 

    private void setAttributes(java.util.Map r1, int r2) {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.setAttributes(java.util.Map, int):void");
    } 

        private void updateRunInfo() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.updateRunInfo():void");
        } 

        public char last() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.last():char");
        } 

        private char internalSetIndex(int r1) {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.internalSetIndex(int):char");
        } 

        public int getBeginIndex() {
            
            throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.AttributedStringIterator.getBeginIndex():int");
        } 

    public java.text.AttributedCharacterIterator getIterator() {
        
        throw new UnsupportedOperationException("Method not decompiled: java.text.AttributedString.getIterator():java.text.AttributedCharacterIterator");
    } 

Advertisement
Javadoc
An AttributedString holds text and related attribute information. It may be used as the actual data

storage in some cases where a text reader wants to access attributed text through the AttributedCharacterIterator interface.

An attribute is a key/value pair, identified by the key. No two attributes on a given character can have the same key.

The values for an attribute are immutable, or must not be mutated by clients or storage. They are always passed by reference, and not cloned. @see AttributedCharacterIterator @see Annotation @since 1.2

Read More
Advertisement