All articles

Norconex Commons Lang 1.5.0 Released

This feature release brings the following additions…

Simple Pipeline

Useful if you want to quickly assemble multiple tasks to be run into a single “pipeline” while keeping it ultra simple. The following example does it all in a single class only to keep it short.

JAVA
public class MyPipeline extends Pipeline<String> {

    public MyPipeline() {
        addStage(new MyTask1());
        addStage(new MyTask2());
    }

    // Class: Task1
    private class MyTask1 implements IPipelineStage<String> {
        @Override
        public boolean execute(String context) {
            System.out.println("Task 1 executed: " + context);
            return true;
        }
    }  

    // Class: Task2
    private class MyTask2 implements IPipelineStage<String> {
        @Override
        public boolean execute(String context) {
            System.out.println("Task 2 executed: " + context);
            return true;
        }
    }  

    public static void main(String[] args) {
        new MyPipeline().execute("hello");

        // Will print out:
        //     Task 1 executed: hello
        //     Task 2 executed: hello
    }
}

Cacheable Streams

There are several excellent object caching mechanism available to Java already if you need something sophisticated. This release offers a very lightweight cache implementation that can make InputStream and OutputStream reusable. It stores the stream in memory until a configurable threshold is reached, after which it switches to fast file lookup. A CachedStreamFactory is used to obtain cached streams sharing the same pool of memory.

JAVA
        int size10mb = 10 * 1024 * 1024;
        int size1mb  = 1024 * 1024;
        InputStream is = null; // <-- your original input stream
        OutputStream os = null; // <-- your original output stream

        CachedStreamFactory streamFactory = new CachedStreamFactory(size10mb, size1mb);

        //--- Reuse the input stream ---
        CachedInputStream cachedInput = streamFactory.newInputStream(is);

        // Read the input stream the first time
        System.out.println(IOUtils.toString(cachedInput));
        // Read the input stream a second time
        System.out.println(IOUtils.toString(cachedInput));

        // Released the cached data, preventing further re-use
        cachedInput.dispose();

        //--- Reuse the output stream ---
        CachedOutputStream cachedOutput = streamFactory.newOuputStream(os);

        IOUtils.write("lots of data", cachedOutput);

        // Obtain a new input stream from the output
        CachedInputStream newInputStream = cachedOutput.getInputStream();

        // Do what you want with this input stream

Enhanced XML Writing

The Java XMLStreamWriter is a useful class, but is a bit annoying to use when you are not always writing strings. The EnhancedXMLStreamWriter add convenience method for primary types and others.

JAVA
        Writer out = null; // <-- your target writer

        EnhancedXMLStreamWriter xml = new EnhancedXMLStreamWriter(out);
        xml.writeStartDocument();
        xml.writeStartElement("item");

        xml.writeElementInteger("quantity", 23);

        xml.writeElementString("name", "something");

        xml.writeStartElement("size");
        xml.writeAttributeInteger("height", 24);
        xml.writeAttributeInteger("width", 26);
        xml.writeEndElement();

        xml.writeElementBoolean("sealwrapped", true);

        xml.writeEndElement();
        xml.writeEndDocument();

        /* Will write:

          <?xml version="1.0" encoding="UTF-8"?>
          <item>
              <quantity>23</quantity>
              <name>something</name>
              <size height="24" width="26" />
              <sealwrapped>true</sealwrapped>
          </item>
         */

More Equality checks

More methods were added to EqualUtils :

JAVA
        EqualsUtil.equalsAnyIgnoreCase("toMatch", "candidate1", "candiate1");
        EqualsUtil.equalsAllIgnoreCase("toMatch", "candidate1", "candiate1");
        EqualsUtil.equalsNoneIgnoreCase("toMatch", "candidate1", "candiate1");

Discover More Features

A few more features and updates were made to the Norconex Commons Lang library. For more information, check out the full release notes.

Download your copy now.