wiki:GettingStarted

Getting Started with KommonLee

Get KommonLee

Maven Users


  <repositories>
    <repository>
      <id>kevin-public-snapshots</id>
      <name>Kevin's Public Snapshots</name>
      <url>http://nexus.lckymn.com/content/repositories/kevin-public-snapshots</url>
      <releases>
        <enabled>false</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>kevin-public-releases</id>
      <name>Kevin's Public Releases</name>
      <url>http://nexus.lckymn.com/content/repositories/kevin-public-releases</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>


  <dependencies>
    <dependency>
      <groupId>org.elixirian</groupId>
      <artifactId>kommonlee-test</artifactId>
      <version>0.0.17-SNAPSHOT</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.elixirian</groupId>
      <artifactId>kommonlee-core</artifactId>
      <version>0.0.44-SNAPSHOT</version>
    </dependency>

    <dependency>
      <groupId>org.elixirian</groupId>
      <artifactId>kommonlee-3rd-asm</artifactId>
      <version>5.0.3</version>
    </dependency>
    <dependency>
      <groupId>org.elixirian</groupId>
      <artifactId>kommonlee-asm</artifactId>
      <version>0.1.0-SNAPSHOT</version>
    </dependency>

    <dependency>
      <groupId>org.elixirian</groupId>
      <artifactId>kommonlee-web3</artifactId>
      <version>0.0.24-SNAPSHOT</version>
    </dependency>
<!-- Tomcat 6 (Servlet 2.5) users need to use this one instead of kommonlee-web3 (designed for Servlet 3.0) -->
<!--
    <dependency>
      <groupId>org.elixirian</groupId>
      <artifactId>kommonlee-web</artifactId>
      <version>0.0.24-SNAPSHOT</version>
    </dependency>
-->

  </dependencies>

Download

It will be added...

Common Usage

KommonLee Core?

Objects

hash(), castIfInstanceOf() and equal()

e.g.) hashCode() and equals() methods Here is a simple Java bean.


public class FileElement
{
  private Long id;
  private Folder folder;
  private String name;

  public Long getId()
  {
    return id;
  }

  public Folder getFolder()
  {
    return folder;
  }

  public String getName()
  {
    return name;
  }
}

If you use Eclipse's "generate hashCode() and equals()" function, it gives the code like this


  @Override
  public int hashCode()
  {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((folder == null) ? 0 : folder.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
  }

  @Override
  public boolean equals(final Object obj)
  {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    final FileElement other = (FileElement) obj;
    if (folder == null)
    {
      if (other.folder != null)
        return false;
    }
    else if (!folder.equals(other.folder))
      return false;
    if (name == null)
    {
      if (other.name != null)
        return false;
    }
    else if (!name.equals(other.name))
      return false;
    return true;
  }

The equals() method can be improved like this.


  @Override
  public boolean equals(final Object fileElement)
  {
    if (this == fileElement)
    {
      return true;
    }
    if (!(fileElement instanceof FileElement))
    {
      return false;
    }
    final FileElement that = (FileElement) fileElement;
    return (this.folder == that.folder ||
               (null != this.folder &&
                this.folder
                    .equals(that
                    .getFolder()))) &&
           (this.name == that.name ||
               (null != this.name &&
                this.name
                    .equals(that
                    .getName())));
  }

If you use KommonLee, it becomes much simpler.


import static org.elixirian.kommonlee.util.Objects.*;

public class FileElement
{
  // ...

  @Override
  public int hashCode()
  {
    return hash(folder, name);
  }

  @Override
  public boolean equals(final Object fileElement)
  {
    if (this == fileElement)
    {
      return true;
    }
    final FileElement that = castIfInstanceOf(FileElement.class, fileElement);
    return null != that &&
        (equal(this.folder, that.getFolder()) &&
         equal(this.name, that.getName()));
  }
}

  • hashCode() and equals() methods can be easily created using Objects.hash(), Objects.castIfInstanceOf() and Objects.equal() methods.

import static org.elixirian.kommonlee.util.Objects.*;

public class FileElement
{
  private Long id;
  private Folder folder;
  private String name;

  public Long getId()
  {
    return id;
  }

  public Folder getFolder()
  {
    return folder;
  }

  public String getName()
  {
    return name;
  }

  @Override
  public int hashCode()
  {
    return hash(folder, name);
  }

  @Override
  public boolean equals(final Object fileElement)
  {
    if (this == fileElement)
    {
      return true;
    }
    final FileElement that = castIfInstanceOf(FileElement.class, fileElement);
    return null != that &&
        (equal(this.folder, that.getFolder()) &&
         equal(this.name, that.getName()));
  }
}

Traditional KommonLee
public class FileElement
{
  // ...

  @Override
  public int hashCode()
  {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((folder == null) ? 0 : folder.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
  }

  @Override
  public boolean equals(final Object fileElement)
  {
    if (this == fileElement)
    {
      return true;
    }
    if (!(fileElement instanceof FileElement))
    {
      return false;
    }
    final FileElement that = (FileElement) fileElement;
    return (this.folder == that.folder ||
               (null != this.folder &&
                this.folder
                    .equals(that
                    .getFolder()))) &&
           (this.name == that.name ||
               (null != this.name &&
                this.name
                    .equals(that
                    .getName())));
  }
}


import static org.elixirian.kommonlee.util.Objects.*;

public class FileElement
{
  // ...

  @Override
  public int hashCode()
  {
    return hash(folder, name);
  }

  @Override
  public boolean equals(final Object fileElement)
  {
    if (this == fileElement)
    {
      return true;
    }
    final FileElement that = castIfInstanceOf(FileElement.class, fileElement);
    return null != that &&
        (equal(this.folder, that.getFolder()) &&
         equal(this.name, that.getName()));
  }
}

toStringBuilder()

  • toString() method using toStringBuilder()
Traditional KommonLee

  @Override
  public String toString()
  {
    return new StringBuilder("FileElement[id=")
                 .append(id)
                 .append(", folder=")
                 .append(folder)
                 .append(", name=")
                 .append(name)
                 .append("]")
               .toString();
  }

OR


  @Override
  public String toString()
  {
    return "FileElement[id=" + id + ", folder=" + folder + ", name=" + name + "]";
  }


import static org.elixirian.kommonlee.util.Objects.*;

//...

  @Override
  public String toString()
  {
    return toStringBuilder(this)
             .add("id",     id)
             .add("folder", folder)
             .add("name",   name)
           .toString();
  }

fileElement.toString() will return the String value like:

FileElement{id=1, folder=Folder{id=1, name=root, parent=null}, name=some file}

toStringOf()


final String name = "Kevin";
final int number = 1234;
final int[] intArray = {1, 10, 100, 1000, 1111, 2222, 123456};

final int[][] _2dIntArray = {{-100, -25, 0}, {1000, 1111, 123456}};

JDK KommonLee

String.valueOf(name);
Result: Kevin

String.valueOf(number);
Result: 1234

String.valueOf(intArray);
Result: [I@7f5f5897

Arrays.toString(intArray);
Result: [1, 10, 100, 1000, 1111, 2222, 123456]


String.valueOf(_2dIntArray);
Result: [[I@4cb162d5

Arrays.toString(_2dIntArray);
Result: [[I@11cfb549, [I@5b86d4c1]


import static org.elixirian.kommonlee.util.Objects.*;

//...

toStringOf(name);
Result: Kevin

toStringOf(number);
Result: 1234

toStringOf(intArray);
Result: [1, 10, 100, 1000, 1111, 2222, 123456]

toStringOf(_2dIntArray);
Result: [[-100, -25, 0], [1000, 1111, 123456]]

More (will be added...)

Strings

ToStringGlues?

org.elixirian.kommonlee.util.collect.*

Lists

Create a new ArrayList
JDK KommonLee

import java.util.ArrayList;
import java.util.List;

//...
final List<Integer> integerList = new ArrayList<Integer>();


import static org.elixirian.kommonlee.util.collect.Lists.*;

import java.util.List;

// ...
final List<Integer> integerList = newArrayList();

Create a new ArrayList and add values
JDK KommonLee

import java.util.ArrayList;
import java.util.List;

  // JDK
  final List<Integer> integerList = new ArrayList<Integer>();
  integerList.add(-14);
  integerList.add(-10);
  integerList.add(-1);
  integerList.add(0);
  integerList.add(1);
  integerList.add(2);
  integerList.add(10);
  integerList.add(35);
  integerList.add(100);


import static org.elixirian.kommonlee.util.collect.Lists.*;

import java.util.List;

  // KommonLee
  final List<Integer> integerList = newArrayList(-14, -10, -1, 0, 1, 2, 10, 35, 100);

Create a new ArrayList with elements from another !List
JDK KommonLee

import java.util.ArrayList;
import java.util.List;

  // JDK
  final List<Integer> integerList = new ArrayList<Integer>();
  integerList.add(-14);
  integerList.add(-10);
  integerList.add(-1);
  integerList.add(0);
  integerList.add(1);
  integerList.add(2);
  integerList.add(10);
  integerList.add(35);
  integerList.add(100);

  final List<Integer> anotherIntegerList = new ArrayList<Integer>(integerList);


import static org.elixirian.kommonlee.util.collect.Lists.*;

import java.util.List;

  // KommonLee
  final List<Integer> integerList = newArrayList(-14, -10, -1, 0, 1, 2, 10, 35, 100);
  final List<Integer> antherIntegerList = newArrayList(integerList);

Sets

Create a new HashSet
JDK KommonLee

import java.util.HashSet;
import java.util.Set;

  // JDK
  final Set<Integer> integerSet = new HashSet<Integer>();


import static org.elixirian.kommonlee.util.collect.Sets.*;

import java.util.Set;

  // KommonLee
  final Set<Integer> integerSet = newHashSet();

Create a new ArrayList and add values
JDK KommonLee

import java.util.HashSet;
import java.util.Set;

  // JDK
  final Set<Integer> integerSet = new HashSet<Integer>();
  integerSet.add(-14);
  integerSet.add(-10);
  integerSet.add(-1);
  integerSet.add(0);
  integerSet.add(1);
  integerSet.add(2);
  integerSet.add(10);
  integerSet.add(35);
  integerSet.add(100);


import static org.elixirian.kommonlee.util.collect.Sets.*;

import java.util.Set;

  // KommonLee
  final Set<Integer> integerSet2_2 = newHashSet(-14, -10, -1, 0, 1, 2, 10, 35, 100);

Create a new ArrayList with elements from another !List
JDK KommonLee

import java.util.HashSet;
import java.util.Set;

  // JDK
  final Set<Integer> integerSet = new HashSet<Integer>();
  integerSet.add(-14);
  integerSet.add(-10);
  integerSet.add(-1);
  integerSet.add(0);
  integerSet.add(1);
  integerSet.add(2);
  integerSet.add(10);
  integerSet.add(35);
  integerSet.add(100);
  final Set<Integer> anotherintegerSet = new HashSet<Integer>(integerSet);


import static org.elixirian.kommonlee.util.collect.Sets.*;

import java.util.Set;

  // KommonLee
  final Set<Integer> integerSet = newHashSet(-14, -10, -1, 0, 1, 2, 10, 35, 100);
  final Set<Integer> antherintegerSet = newHashSet(integerSet);

KommonLee IO?

IoUtil?

IoUtil.readFile()

Traditional KommonLee

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.ArrayList;
import java.util.List;

// ...

  final File file = new File("/tmp/test.txt");
  InputStream inputStream = null;
  final int bufferSize = 131072; // 128Ki

  final List<Byte> byteList = new ArrayList<Byte>(); 

  try
  {
    inputStream = new FileInputStream(file);
    final byte[] buffer = new byte[bufferSize];
    int bytesRead = inputStream.read(buffer);

    while (-1 < bytesRead)
    {
      for (int i = 0; i < bytesRead; i++)
      {
        byteList.add(buffer[i]);
      }
      bytesRead = inputStream.read(buffer);
    }
  }
  catch (final IOException e)
  {
    e.printStackTrace();
  }
  finally
  {
    if (null != inputStream)
    {
      try
      {
        inputStream.close();
      }
      catch (final IOException e)
      {
        e.printStackTrace();
      }
    }
  }
  final int length = byteList.size();
  final byte[] bytes = new byte[length];
  for (int i = 0; i < length; i++)
  {
    bytes[i] = byteList.get(i);
  }
  System.out.println(new String(bytes));


import static org.elixirian.kommonlee.io.DataConsumers.*;
import static org.elixirian.kommonlee.io.IoCommonConstants.*;
import static org.elixirian.kommonlee.io.util.IoUtil.*;

import java.io.File;
import java.io.IOException;

import org.elixirian.kommonlee.io.ByteArrayConsumingContainer;

//...

  final File file = new File("/tmp/test.txt");
  final ByteArrayConsumingContainer byteArrayConsumingContainer = 
                                newByteArrayConsumingContainer();
  readFile(file, BUFFER_SIZE_128Ki, byteArrayConsumingContainer);
  System.out.println(byteArrayConsumingContainer.toString());

IoUtil.readInputStream()

Traditional KommonLee

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.ArrayList;
import java.util.List;

// ...

  final int bufferSize = 131072; // 128Ki
  InputStream bufferedInputStream = null;
  final List<Byte> byteList = new ArrayList<Byte>();

  try
  {
    bufferedInputStream = new BufferedInputStream(inputStream, bufferSize);
    final byte[] buffer = new byte[bufferSize];
    int bytesRead = bufferedInputStream.read(buffer);

    while (-1 < bytesRead)
    {
      for (int i = 0; i < bytesRead; i++)
      {
        byteList.add(buffer[i]);
      }
      bytesRead = bufferedInputStream.read(buffer);
    }
  }
  catch (final IOException e)
  {
    e.printStackTrace();
  }
  finally
  {
    if (null != bufferedInputStream)
    {
      try
      {
        bufferedInputStream.close();
      }
      catch (final IOException e)
      {
        e.printStackTrace();
      }
    }
    if (null != inputStream)
    {
      try
      {
        inputStream.close();
      }
      catch (final IOException e)
      {
        e.printStackTrace();
      }
    }
  }
  final int length = byteList.size();
  final byte[] bytes = new byte[length];
  for (int i = 0; i < length; i++)
  {
    bytes[i] = byteList.get(i);
  }
  System.out.println(new String(bytes));


import static org.elixirian.kommonlee.io.DataConsumers.*;
import static org.elixirian.kommonlee.io.IoCommonConstants.*;
import static org.elixirian.kommonlee.io.util.IoUtil.*;

import java.io.IOException;
import java.io.InputStream;

import org.elixirian.kommonlee.io.ByteArrayConsumingContainer;

//...

  final InputStream inputStream =
          IoUtilExample.class.getResourceAsStream("/test.txt");
  final ByteArrayConsumingContainer byteArrayConsumingContainer =
                                            newByteArrayConsumingContainer();
  readInputStream(inputStream, BUFFER_SIZE_128Ki, byteArrayConsumingContainer);
  System.out.println(byteArrayConsumingContainer.toString());

IoUtil example code

KommonLee Test?

KommonLee Exception?

KommonLee Reflection?

KommonLee ASM?

Java classes lose all the parameter names of the methods and constructors after it is compiled. So with only what JDK provides such as reflection, there is no way (or at least no easy or convenient way) to get the names of method / constructor parameters (I was shocked when I first discovered it). There are a few ways to solve this issue and one of these is using ASM. What KommonLee? provides is an easy way to get the parameter names without directly dealing with the APIs in ASM since not all programmers are familiar with Visitor Pattern and they might want to run only one or two methods to get the parameter names instead of creating a few classes to do it. I'm also working on another project named JSON Statham which is Java / JSON mapper and it needs the constructor parameter names in order to instantiate an object especially immutable one. So KommonLee ASM? was created.

AsmMethodAndConstructorAnalyser?

  • Target class
    public class Person
    {
      private String surname;
    
      private String givenName;
    
      private String email;
    
      public Person()
      {
      }
    
      public Person(final String surname, final String givenName)
      {
        this.surname = surname;
        this.givenName = givenName;
      }
    
      public Person(final String email)
      {
        this.email = email;
      }
    
      public Person(final String surname, final String givenName, final String email)
      {
        this.surname = surname;
        this.givenName = givenName;
        this.email = email;
      }
    
      public String getSurname()
      {
        return surname;
      }
    
      public String getGivenName()
      {
        return givenName;
      }
    
      public String getEmail()
      {
        return email;
      }
    
      public void rename(final String surname, final String givenName)
      {
        this.surname = surname;
        this.givenName = givenName;
      }
    
      public void changeNameAndEmail(final String surname, final String givenName, final String email)
      {
        this.surname = surname;
        this.givenName = givenName;
        this.email = email;
      }
    
    }
    

Get All Methods with Parameter Names


import static org.elixirian.kommonlee.util.Objects.*;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.Map.Entry;

import org.elixirian.kommonlee.asm.analysis.AsmMethodAndConstructorAnalyser;
import org.elixirian.kommonlee.asm.analysis.MethodAnalyser;

    // KommonLee
    final MethodAnalyser methodAnalyser = new AsmMethodAndConstructorAnalyser();
    final Map<Method, String[]> methodToParamNames = methodAnalyser.findMethodsWithParameterNames(Person.class);

    for (final Entry<Method, String[]> entry : methodToParamNames.entrySet())
    {
      System.out.println("method: " + entry.getKey());
      System.out.println("paramsNames: " + toStringOf(entry.getValue()));
      System.out.println();
    }

  • Result:
    method: public java.lang.String org.elixirian.kommonlee.examples.asm.analysis.Person.getSurname()
    paramsNames: []
    
    method: public java.lang.String org.elixirian.kommonlee.examples.asm.analysis.Person.getGivenName()
    paramsNames: []
    
    method: public java.lang.String org.elixirian.kommonlee.examples.asm.analysis.Person.getEmail()
    paramsNames: []
    
    method: public void org.elixirian.kommonlee.examples.asm.analysis.Person.rename(java.lang.String,java.lang.String)
    paramsNames: [surname, givenName]
    
    method: public void org.elixirian.kommonlee.examples.asm.analysis.Person.changeNameAndEmail(java.lang.String,java.lang.String,java.lang.String)
    paramsNames: [surname, givenName, email]
    

Get All Constructors with Parameter Names


import static org.elixirian.kommonlee.util.Objects.*;

import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.Map.Entry;

import org.elixirian.kommonlee.asm.analysis.AsmMethodAndConstructorAnalyser;
import org.elixirian.kommonlee.asm.analysis.ConstructorAnalyser;

  // KommonLee
  final ConstructorAnalyser constructorAnalyser = new AsmMethodAndConstructorAnalyser();
  final Map<Constructor<Person>, String[]> constructorToParamNames =
    constructorAnalyser.findConstructorsWithParameterNames(Person.class);

  for (final Entry<Constructor<Person>, String[]> entry : constructorToParamNames.entrySet())
  {
    System.out.println("constructor: " + entry.getKey());
    System.out.println("paramsNames: " + toStringOf(entry.getValue()));
    System.out.println();
  }

  • Result:
    constructor: public org.elixirian.kommonlee.examples.asm.analysis.Person()
    paramsNames: []
    
    constructor: public org.elixirian.kommonlee.examples.asm.analysis.Person(java.lang.String,java.lang.String)
    paramsNames: [surname, givenName]
    
    constructor: public org.elixirian.kommonlee.examples.asm.analysis.Person(java.lang.String)
    paramsNames: [email]
    
    constructor: public org.elixirian.kommonlee.examples.asm.analysis.Person(java.lang.String,java.lang.String,java.lang.String)
    paramsNames: [surname, givenName, email]
    

Get All Constructors and Methods with Parameter Names


import static org.elixirian.kommonlee.util.Objects.*;

import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.util.Map;
import java.util.Map.Entry;

import org.elixirian.kommonlee.asm.analysis.AsmMethodAndConstructorAnalyser;
import org.elixirian.kommonlee.asm.analysis.MethodAndConstructorAnalyser;

  // KommonLee
  final MethodAndConstructorAnalyser methodAndConstructorAnalyser = new AsmMethodAndConstructorAnalyser();
  final Map<Member, String[]> memberToParamNames =
    methodAndConstructorAnalyser.findMethodsAndConstructorsWithParameterNames(Person.class);

  for (final Entry<Member, String[]> entry : memberToParamNames.entrySet())
  {
    final Member member = entry.getKey();
    System.out.println("class: " + member.getClass());
    System.out.println("member: " + member);
    System.out.println("paramsNames: " + toStringOf(entry.getValue()));
    System.out.println();
  }

  • Result:
    class: class java.lang.reflect.Constructor
    member: public org.elixirian.kommonlee.examples.asm.analysis.Person()
    paramsNames: []
    
    class: class java.lang.reflect.Constructor
    member: public org.elixirian.kommonlee.examples.asm.analysis.Person(java.lang.String,java.lang.String)
    paramsNames: [surname, givenName]
    
    class: class java.lang.reflect.Constructor
    member: public org.elixirian.kommonlee.examples.asm.analysis.Person(java.lang.String)
    paramsNames: [email]
    
    class: class java.lang.reflect.Constructor
    member: public org.elixirian.kommonlee.examples.asm.analysis.Person(java.lang.String,java.lang.String,java.lang.String)
    paramsNames: [surname, givenName, email]
    
    class: class java.lang.reflect.Method
    member: public java.lang.String org.elixirian.kommonlee.examples.asm.analysis.Person.getSurname()
    paramsNames: []
    
    class: class java.lang.reflect.Method
    member: public java.lang.String org.elixirian.kommonlee.examples.asm.analysis.Person.getGivenName()
    paramsNames: []
    
    class: class java.lang.reflect.Method
    member: public java.lang.String org.elixirian.kommonlee.examples.asm.analysis.Person.getEmail()
    paramsNames: []
    
    class: class java.lang.reflect.Method
    member: public void org.elixirian.kommonlee.examples.asm.analysis.Person.rename(java.lang.String,java.lang.String)
    paramsNames: [surname, givenName]
    
    class: class java.lang.reflect.Method
    member: public void org.elixirian.kommonlee.examples.asm.analysis.Person.changeNameAndEmail(java.lang.String,java.lang.String,java.lang.String)
    paramsNames: [surname, givenName, email]
    

KommonLee Collection?

org.elixirian.kommonlee.collect.immutable

ImmutableArrayList?

  • New immutable array
JDK KommonLee

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

  // JDK
  final List<Integer> integerList = new ArrayList<Integer>();
  integerList.add(-14);
  integerList.add(-10);
  integerList.add(-1);
  integerList.add(0);
  integerList.add(1);
  integerList.add(2);
  integerList.add(10);
  integerList.add(35);
  integerList.add(100);

  // It is not really immutable as the elements in the unmodifiableIntegerList
  // can be added and removed through integerList.
  final List<Integer> unmodifiableIntegerList = Collections.unmodifiableList(integerList);


import org.elixirian.kommonlee.collect.immutable.ImmutableArrayList;
import org.elixirian.kommonlee.collect.immutable.ImmutableList;

import java.util.Set;

  // KommonLee
  final ImmutableList<Integer> immutableIntegerList =
                              ImmutableArrayList.listOf(-14, -10, -1, 0, 1, 2, 10, 35, 100);

  • Select specific elements from a List and make a new immutable list having these elements.

get all positive integer.

JDK KommonLee


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

  // JDK
  final List<Integer> integerList = new ArrayList<Integer>();
  integerList.add(-14);
  integerList.add(-10);
  integerList.add(-1);
  integerList.add(0);
  integerList.add(1);
  integerList.add(2);
  integerList.add(10);
  integerList.add(35);
  integerList.add(100);
  final List<Integer> unmodifiableIntegerList = Collections.unmodifiableList(integerList);

  final List<Integer> positiveIntegerList = new ArrayList<Integer>();
  for (final Integer i : unmodifiableIntegerList)
  {
    if (0 < i.intValue())
    {
      positiveIntegerList.add(i);
    }
  }
  final List<Integer> unmodifiablePositiveIntegerList =
                Collections.unmodifiableList(positiveIntegerList);


import org.elixirian.kommonlee.collect.immutable.ImmutableArrayList;
import org.elixirian.kommonlee.collect.immutable.ImmutableList;
import org.elixirian.kommonlee.type.functional.Condition1;

  // KommonLee
  final ImmutableList<Integer> immutableIntegerList =
                                  ImmutableArrayList.listOf(-14, -10, -1, 0, 1, 2, 10, 35, 100);
  final ImmutableList<Integer> immutablePositiveIntegerList = immutableIntegerList.select(
  new Condition1<Integer>() {
    @Override
    public boolean isMet(final Integer integer)
    {
      return 0 < integer.intValue();
    }
  });

  • Generate an immutable list containing String value created from the numbers stored in another list.

List of String created from the numbers in another list.

JDK KommonLee


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

  // JDK
  final List<Integer> integerList = new ArrayList<Integer>();
  integerList.add(-14);
  integerList.add(-10);
  integerList.add(-1);
  integerList.add(0);
  integerList.add(1);
  integerList.add(2);
  integerList.add(10);
  integerList.add(35);
  integerList.add(100);
  final List<Integer> unmodifiableIntegerList = Collections.unmodifiableList(integerList);

  final List<String> mappedList = new ArrayList<String>();
  for (final Integer integer : unmodifiableIntegerList)
  {
    mappedList.add("The number is " + integer + ".");
  }
  final List<String> unmodifiableMappedList = Collections.unmodifiableList(mappedList);


import org.elixirian.kommonlee.collect.immutable.ImmutableArrayList;
import org.elixirian.kommonlee.collect.immutable.ImmutableList;
import org.elixirian.kommonlee.type.functional.Function1;

  // KommonLee
  final ImmutableList<Integer> immutableIntegerList =
                                  ImmutableArrayList.listOf(-14, -10, -1, 0, 1, 2, 10, 35, 100);
  final ImmutableList<String> immutableMappedList = immutableIntegerList.map(
  new Function1<Integer, String>() {
    @Override
    public String apply(final Integer integer)
    {
      return "The number is " + integer + ".";
    }
  });

KommonLee Function?

org.elixirian.kommonlee.functional.collect

CollectionUtil?

  • Select all positive integer from List
JDK KommonLee

final List<Integer> integerList = new ArrayList<Integer>();
integerList.add(-100);
integerList.add(-55);
integerList.add(-32);
integerList.add(-21);
integerList.add(-17);
integerList.add(-10);
integerList.add(-3);
integerList.add(0);
integerList.add(1);
integerList.add(15);
integerList.add(33);
integerList.add(77);
integerList.add(999);
integerList.add(1234);

final List<Integer> anotherIntegerList = new ArrayList<Integer>();
anotherIntegerList.add(-555);
anotherIntegerList.add(-46);
anotherIntegerList.add(-23);
anotherIntegerList.add(-11);
anotherIntegerList.add(-8);
anotherIntegerList.add(-5);
anotherIntegerList.add(-1);
anotherIntegerList.add(2);
anotherIntegerList.add(11);
anotherIntegerList.add(14);
anotherIntegerList.add(24);
anotherIntegerList.add(56);
anotherIntegerList.add(100);
anotherIntegerList.add(9999);

// JDK
final List<Integer> positiveIntegerList = new ArrayList<Integer>();
for (final Integer integer : integerList)
{
  if (0 < integer.intValue())
  {
    positiveIntegerList.add(integer);
  }
}
System.out.println("positiveIntegerList: " + positiveIntegerList);

final List<Integer> anotherPositiveIntegerList = new ArrayList<Integer>();
for (final Integer integer : anotherIntegerList)
{
  if (0 < integer.intValue())
  {
    anotherPositiveIntegerList.add(integer);
  }
}
System.out.println("anotherPositiveIntegerList: " + anotherPositiveIntegerList);


import static org.elixirian.kommonlee.functional.collect.CollectionUtil.*;
import static org.elixirian.kommonlee.util.collect.Lists.*;

    final List<Integer> integerList =
        newArrayList(-100, -55, -32, -21, -17, -10, -3, 0, 1, 15, 33, 77, 999, 1234);

    final List<Integer> anotherIntegerList =
        newArrayList(-555, -46, -23, -11, -8, -5, -1, 2, 11, 14, 24, 56, 100, 9999);

    final Condition1<Integer> positiveInteger = new Condition1<Integer>() {
      @Override
      public boolean isMet(final Integer integer)
      {
        return 0 < integer.intValue();
      }
    };

    // KommonLee
    final List<Integer> positiveIntegerList = selector()
        .fromIterable()
        .toArrayList()
        .select(integerList, positiveInteger);
    System.out.println("positiveIntegerList: " + positiveIntegerList);

    final List<Integer> anotherPositiveIntegerList = selector()
        .fromIterable()
        .toArrayList()
        .select(anotherIntegerList, positiveInteger);
    System.out.println("anotherPositiveIntegerList: " + anotherPositiveIntegerList);

  • Get all the email addresses from Person objects stored in the List.

final List<Person> personList =
  newArrayList(new Person("Lee", "Kevin", "test@test.email.com"),
               new Person("Kent", "Clark", "clark.kent@krypton.com"),
               new Person("Wayne", "Bruce", "bruce.wayne@gotham.com"),
               new Person("Lee", "Tom", "test@another.test-email.com"),
               new Person("Odinson", "Thor", "thor.odinson@asgard.com"),
               new Person("Stark","Tony", "tony.stark@stark.com"));

final List<Person> personList2 =
  newArrayList(new Person("Rogers", "Steve", "steve.rogers@captain.america.com"),
               new Person("Jordan", "Hal", "hal.jordan@earth.com"),
               new Person("Banner", "Bruce", "bruce.banner@hulk.com"));

final Person[] personArray = {new Person("Lee", "Kevin", "test@test.email.com"),
               new Person("Kent", "Clark", "clark.kent@krypton.com"),
               new Person("Wayne", "Bruce", "bruce.wayne@gotham.com"),
               new Person("Lee", "Tom", "test@another.test-email.com"),
               new Person("Odinson", "Thor", "thor.odinson@asgard.com"),
               new Person("Stark","Tony", "tony.stark@stark.com")};

JDK KommonLee

// JDK
final List<String> emailAddressList = new ArrayList<String>();
for (final Person person : personList)
{
  emailAddressList.add(person.getEmail());
}
System.out.println("emailAddressList: " + emailAddressList);

final Set<String> emailAddressSet = new HashSet<String>();
for (final Person person : personList2)
{
  emailAddressSet.add(person.getEmail());
}
System.out.println("emailAddressSet: " + emailAddressSet);

final List<String> emailAddressList2 = new ArrayList<String>();
for (final Person person : personArray)
{
  emailAddressList.add(person.getEmail());
}
System.out.println("emailAddressList2: " + emailAddressList2);


// reusable mapper function
final Function1<Person, String> personToEmailMapper = new Function1<Person, String>() {
  @Override
  public String apply(final Person person)
  {
    return person.getEmail();
  }
};

// KommonLee
final List<String> emailAddressList = mapper()
    .fromIterable()
    .toArrayList()
    .map(personList, personToEmailMapper);
System.out.println("emailAddressList: " + emailAddressList);

final Set<String> emailAddressSet = mapper()
    .fromIterable()
    .toHashSet()
    .map(personList2, personToEmailMapper);
System.out.println("emailAddressSet: " + emailAddressSet);

final List<String> emailAddressList2 = mapper()
    .fromArray()
    .toArrayList()
    .map(personArray, personToEmailMapper);
System.out.println("emailAddressList2: " + emailAddressList2);

KommonLee Web?

... more will be added...

Examples

KommonLee Examples

Last modified 7 weeks ago Last modified on 07/07/14 20:57:55