/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2017-2020 Yegor Bugayenko
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.cactoos.text;

import org.cactoos.Func;
import org.cactoos.Text;
import org.cactoos.scalar.Ternary;

/**
 * Text in capitalized case,
 * changed the first character to title case as per {@link Character#toTitleCase(int)},
 * no other characters are changed.
 *
 * @since 0.46
 */
public final class Capitalized extends TextEnvelope {

    /**
     * Ctor.
     *
     * @param text The text
     */
    public Capitalized(final String text) {
        this(new TextOf(text));
    }

    /**
     * Ctor.
     *
     * @param text The text
     */
    public Capitalized(final Text text) {
        super(
            new TextOf(
                () -> {
                    final Func<Text, Text> capitalize = cap -> {
                        final Text titled = new TextOf(
                            Character.toChars(
                                Character.toTitleCase(
                                    cap.asString().codePointAt(0)
                                    )
                                )
                            );
                        return
                            new Ternary<Text>(
                                () -> new StartsWith(cap, titled).value(),
                                () -> cap,
                                () -> new TextOf(new StringBuilder()
                                    .append(titled.asString())
                                    .append(new Sub(text, 1))
                                    .toString()
                                    )
                                )
                            .value();
                    };
                    return new Ternary<Text>(
                        text,
                        t -> new IsBlank(t).value(),
                        t -> t,
                        t -> capitalize.apply(t)
                            ).value().asString();
                }));
    }
}

@@ -2127,34 +2127,26 @@
             
-    )%0A    
+)%0A
             
@@ -2155,36 +2155,32 @@
               )%0A
-    
                 
@@ -2295,30 +2295,24 @@
             
- () -%3E
  new StartsW
@@ -2327,24 +2327,16 @@
  titled)
-.value()
 ,%0A      
@@ -2418,34 +2418,15 @@
 new 
-TextOf(new StringBuilder()
+Joined(
 %0A   
@@ -2462,34 +2462,11 @@
     
-.append(titled.asString())
+%22%22,
 %0A   
@@ -2502,33 +2502,15 @@
     
-.append(new Sub(text, 1))
+titled,
 %0A   
@@ -2546,56 +2546,23 @@
     
-.toString()%0A                                    
+new Sub(text, 1
 )%0A  
@@ -2585,32 +2585,62 @@
               )%0A
+                            )%0A
                 
@@ -2757,43 +2757,8 @@
     
- text,%0A                        t -%3E
  new
@@ -2767,24 +2767,19 @@
 sBlank(t
-).value(
+ext
 ),%0A     
@@ -2797,21 +2797,25 @@
         
-t
+()
  -%3E 
+tex
 t,%0A     
@@ -2833,17 +2833,18 @@
         
-t
+()
  -%3E capi
@@ -2857,26 +2857,21 @@
 .apply(t
-)%0A        
+ext)%0A
         

add using Joined and other simplifications