Example usage for org.apache.commons.lang3 StringUtils center

List of usage examples for org.apache.commons.lang3 StringUtils center

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils center.

Prototype

public static String center(String str, final int size, String padStr) 

Source Link

Document

Centers a String in a larger String of size size .

Usage

From source file:com.newlandframework.avatarmq.test.AvatarMQProducer1.java

public static void main(String[] args) throws InterruptedException {
    AvatarMQProducer producer = new AvatarMQProducer("127.0.0.1:18888", "AvatarMQ-Topic-1");
    producer.setClusterId("AvatarMQCluster");
    producer.init();/* ww  w.  j a  v  a2 s.c  o m*/
    producer.start();

    System.out.println(StringUtils.center("AvatarMQProducer1 ???", 50, "*"));

    for (int i = 0; i < 1; i++) {
        Message message = new Message();
        String str = "Hello AvatarMQ From Producer1[" + i + "]";
        message.setBody(str.getBytes());
        ProducerAckMessage result = producer.delivery(message);
        if (result.getStatus() == (ProducerAckMessage.SUCCESS)) {
            System.out.printf("AvatarMQProducer1 ????:%s\n", result.getMsgId());
        }

        Thread.sleep(100);
    }

    producer.shutdown();
    System.out.println(StringUtils.center("AvatarMQProducer1 ???", 50, "*"));
}

From source file:com.newlandframework.avatarmq.test.AvatarMQProducer2.java

public static void main(String[] args) throws InterruptedException {
    AvatarMQProducer producer = new AvatarMQProducer("127.0.0.1:18888", "AvatarMQ-Topic-2");
    producer.setClusterId("AvatarMQCluster2");
    producer.init();/*from  ww  w.  j a va  2s.  c  om*/
    producer.start();

    System.out.println(StringUtils.center("AvatarMQProducer2 ???", 50, "*"));

    for (int i = 0; i < 100; i++) {
        Message message = new Message();
        String str = "Hello AvatarMQ From Producer2[" + i + "]";
        message.setBody(str.getBytes());
        ProducerAckMessage result = producer.delivery(message);
        if (result.getStatus() == (ProducerAckMessage.SUCCESS)) {
            System.out.printf("AvatarMQProducer2 ????:%s\n", result.getMsgId());
        }

        Thread.sleep(100);
    }

    producer.shutdown();
    System.out.println(StringUtils.center("AvatarMQProducer2 ???", 50, "*"));
}

From source file:kenh.expl.functions.Center.java

public String process(String str, int size, String padStr) {
    return StringUtils.center(str, size, padStr);
}

From source file:com.feilong.spring.manager.java.SalesOrderManagerImpl.java

@Override
@Transactional()//from  w  w w .  j a  v  a2 s . c o  m
public void addUser(String name) {
    memberManager.addUser(name);

    log.debug(StringUtils.center("before getUserReadOnly", 50, "*"));
    getUserReadOnly(name);
    log.debug(StringUtils.center("end getUserReadOnly", 50, "*"));

    log.debug(StringUtils.center("before deleteUser", 50, "*"));
    deleteUser(name);
    log.debug(StringUtils.center("end deleteUser", 50, "*"));
}

From source file:com.feilong.commons.core.lang.ObjectUtilTest.java

/**
 * Test method for {@link com.feilong.commons.core.lang.ObjectUtil#toIterator(java.lang.Object)}.
 *///from w  w  w  . ja va  2s  .c om
@Test
public final void testToIterator() {

    // *************************?********************************
    log.info(StringUtils.center("?", 60, "*"));
    Iterator<?> iterator = ObjectUtil.toIterator("1,2");
    printIterator(iterator);

    // ************************map*********************************
    log.info(StringUtils.center("map", 60, "*"));
    Map<String, String> map = new HashMap<String, String>();

    map.put("a", "1");
    map.put("b", "2");

    iterator = ObjectUtil.toIterator(map);
    printIterator(iterator);

    // ***************************array******************************
    log.info(StringUtils.center("array", 60, "*"));
    Object[] array = { "5", 8 };
    iterator = ObjectUtil.toIterator(array);
    printIterator(iterator);
    // ***************************collection******************************
    log.info(StringUtils.center("collection", 60, "*"));
    Collection<String> collection = new ArrayList<String>();
    collection.add("aaaa");
    collection.add("nnnnn");

    iterator = ObjectUtil.toIterator(collection);
    printIterator(iterator);

    // **********************enumeration***********************************
    log.info(StringUtils.center("enumeration", 60, "*"));
    Enumeration<Object> enumeration = new StringTokenizer("this is a test");
    log.debug(JsonUtil.format(ObjectUtil.toIterator(enumeration)));
}

From source file:com.feilong.commons.core.bean.BeanUtilTest.java

/**
 * Demo normal java beans./*www  .jav a  2  s.  c  o m*/
 *
 * @throws Exception
 *             the exception
 */
@Test
public void testDemoNormalJavaBeans() throws Exception {

    log.debug(StringUtils.center(" demoNormalJavaBeans ", 40, "="));

    // data setup  
    Address addr1 = new Address("CA1234", "xxx", "Los Angeles", "USA");
    Address addr2 = new Address("100000", "xxx", "Beijing", "China");
    Address[] addrs = new Address[2];
    addrs[0] = addr1;
    addrs[1] = addr2;
    Customer cust = new Customer(123, "John Smith", addrs);

    // accessing the city of first address  
    String cityPattern = "addresses[0].city";
    String name = (String) PropertyUtils.getSimpleProperty(cust, "name");
    String city = (String) PropertyUtils.getProperty(cust, cityPattern);
    Object[] rawOutput1 = new Object[] { "The city of customer ", name, "'s first address is ", city, "." };
    log.debug(StringUtils.join(rawOutput1));

    // setting the zipcode of customer's second address  
    String zipPattern = "addresses[1].zipCode";
    if (PropertyUtils.isWriteable(cust, zipPattern)) {//PropertyUtils  
        log.debug("Setting zipcode ...");
        PropertyUtils.setProperty(cust, zipPattern, "200000");//PropertyUtils  
    }
    String zip = (String) PropertyUtils.getProperty(cust, zipPattern);//PropertyUtils  
    Object[] rawOutput2 = new Object[] { "The zipcode of customer ", name, "'s second address is now ", zip,
            "." };
    log.debug(StringUtils.join(rawOutput2));
}

From source file:com.feilong.core.bean.BeanUtilTest.java

/**
 * Demo normal java beans.//  w w  w. j  a  va  2s.  co m
 *
 * @throws Exception
 *             the exception
 */
@Test
public void testDemoNormalJavaBeans() throws Exception {
    LOGGER.debug(StringUtils.center(" demoNormalJavaBeans ", 40, "="));

    // data setup  
    Customer customer = new Customer(123, "John Smith",
            toArray(new Address("CA1234", "xxx", "Los Angeles", "USA"),
                    new Address("100000", "xxx", "Beijing", "China")));

    // accessing the city of first address  
    String name = (String) PropertyUtils.getSimpleProperty(customer, "name");
    String city = (String) PropertyUtils.getProperty(customer, "addresses[0].city");

    LOGGER.debug(StringUtils
            .join(new Object[] { "The city of customer ", name, "'s first address is ", city, "." }));

    // setting the zipcode of customer's second address  
    String zipPattern = "addresses[1].zipCode";
    if (PropertyUtils.isWriteable(customer, zipPattern)) {//PropertyUtils  
        LOGGER.debug("Setting zipcode ...");
        PropertyUtils.setProperty(customer, zipPattern, "200000");//PropertyUtils  
    }
    String zip = (String) PropertyUtils.getProperty(customer, zipPattern);//PropertyUtils  

    LOGGER.debug(StringUtils
            .join(new Object[] { "The zipcode of customer ", name, "'s second address is now ", zip, "." }));
}

From source file:com.feilong.core.bean.ConvertUtilTest.java

/**
 * Test to iterator./*from  w  w  w .  j  a v a 2s  .co m*/
 */
@Test
public void testToIterator() {
    // *************************?********************************
    LOGGER.debug(StringUtils.center("?", 60, "*"));
    LOGGER.debug(JsonUtil.format(ConvertUtil.toIterator("1,2")));

    // ************************map*********************************
    LOGGER.debug(StringUtils.center("map", 60, "*"));
    Map<String, String> map = new HashMap<String, String>();

    map.put("a", "1");
    map.put("b", "2");

    LOGGER.debug(JsonUtil.format(ConvertUtil.toIterator(map)));

    // ***************************array******************************
    LOGGER.debug(StringUtils.center("array", 60, "*"));
    Object[] array = { "5", 8 };
    LOGGER.debug(JsonUtil.format(ConvertUtil.toIterator(array)));
    // ***************************collection******************************
    LOGGER.debug(StringUtils.center("collection", 60, "*"));
    Collection<String> collection = new ArrayList<String>();
    collection.add("aaaa");
    collection.add("nnnnn");

    LOGGER.debug(JsonUtil.format(ConvertUtil.toIterator(collection)));

    // **********************enumeration***********************************
    LOGGER.debug(StringUtils.center("enumeration", 60, "*"));
    Enumeration<Object> enumeration = new StringTokenizer("this is a test");
    LOGGER.debug(JsonUtil.format(ConvertUtil.toIterator(enumeration)));
}

From source file:com.feilong.commons.core.bean.BeanUtilTest.java

/**
 * Demo dyna beans./*from   ww w.ja  v a 2  s  . co  m*/
 *
 * @throws Exception
 *             the exception
 */
@Test
public void demoDynaBeans() throws Exception {

    log.debug(StringUtils.center(" demoDynaBeans ", 40, "="));

    // creating a DynaBean  
    DynaProperty[] dynaBeanProperties = new DynaProperty[] { //DynaProperty  
            new DynaProperty("name", String.class), new DynaProperty("inPrice", Double.class),
            new DynaProperty("outPrice", Double.class), };
    BasicDynaClass cargoClass = new BasicDynaClass("Cargo", BasicDynaBean.class, dynaBeanProperties); //BasicDynaClass  BasicDynaBean  
    DynaBean cargo = cargoClass.newInstance();//DynaBean  

    // accessing a DynaBean  
    cargo.set("name", "Instant Noodles");
    cargo.set("inPrice", new Double(21.3));
    cargo.set("outPrice", new Double(23.8));
    log.debug("name: " + cargo.get("name"));
    log.debug("inPrice: " + cargo.get("inPrice"));
    log.debug("outPrice: " + cargo.get("outPrice"));
}

From source file:de.vandermeer.asciitable.v2.render.V2_AsciiTableRenderer.java

private void appendWithAlignment(char alignment, StrBuilder sb, String str, int width, char paddingChar,
        int padding, boolean isLastLine) {
    if (padding > 0) {
        width = width - padding * 2;//from w  ww. jav a2 s . c o  m
    }
    for (int i = 0; i < padding; i++) {
        sb.append(paddingChar);
    }

    if ('l' == alignment) {
        sb.appendFixedWidthPadRight(str, width, paddingChar);
    } else if ('r' == alignment) {
        sb.appendFixedWidthPadLeft(str, width, paddingChar);
    } else if ('c' == alignment) {
        sb.append(StringUtils.center(str, width, paddingChar));
    } else if ('j' == alignment || 't' == alignment) {
        if (isLastLine) {
            //nothing needed if this is the last line of a wrapped text
            if ('j' == alignment) {
                sb.appendFixedWidthPadRight(str, width, paddingChar);
            } else {
                //mist be 't' now
                sb.appendFixedWidthPadLeft(str, width, paddingChar);
            }
        } else {
            String[] ar = StringUtils.split(str);
            int length = 0;
            for (String s : ar) {
                length += s.length();
            }

            //first spaces to distributed (even)
            int first = ((width - length) / (ar.length - 1)) * (ar.length - 1);
            for (int i = 0; i < ar.length - 1; i++) {
                if (first != 0) {
                    ar[i] += " ";
                    first--;
                }
            }

            //second space to distributed (leftovers, as many as there are)
            int second = (width - length) % (ar.length - 1);
            for (int i = 0; i < ar.length - 1; i++) {
                if (second != 0) {
                    ar[i] += " ";
                    second--;
                }
            }
            sb.append(StringUtils.join(ar));
        }
    } else {
        System.err.println("ERROR RENDER ALIGNMENT");//TODO
    }

    for (int i = 0; i < padding; i++) {
        sb.append(paddingChar);
    }
}