DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / commons / src / main / java / org / onap / sdc / dcae / catalog / commons / ListBuilder.java
1 package org.onap.sdc.dcae.catalog.commons;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 import org.onap.sdc.dcae.catalog.commons.ListBuilder;
7
8 import java.util.LinkedList;
9
10 public class ListBuilder<T> {
11
12         private List<T> list;
13
14         public ListBuilder() {
15                 this.list = new LinkedList<T>();
16         }
17
18         public boolean isEmpty() {
19                 return this.list.isEmpty();
20         }
21
22         public ListBuilder add(T theValue) {
23                 this.list.add(theValue);
24                 return this;
25         }
26
27         public ListBuilder addAll(final Iterable<? extends T> theValues) {
28                 for (final T val : theValues) {
29                         this.list.add(val);
30                 }
31                 return this;
32         }
33
34         public ListBuilder addAll(final List<? extends T> theList) {
35                 this.list.addAll(theList);
36                 return this;
37         }  
38
39         public ListBuilder addAll(final T[] theArray) {
40                 for (T t: theArray) this.list.add(t);
41                 return this;
42         }
43   
44         public List build() {
45                 return this.list;
46         }
47
48         public List buildOpt() {
49                 return this.list.isEmpty() ? null : this.list;
50         }
51
52         public static <V> List<V> asList(V[] theArray) {
53                 return Arrays.asList(theArray);
54         }
55         
56         public static <V> List<V> asListOpt(V[] theArray) {
57                 return (theArray != null && theArray.length > 0) ? Arrays.asList(theArray) : null;
58         }
59 }