at.molindo.utils.collections.CollectionBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of molindo-utils Show documentation
Show all versions of molindo-utils Show documentation
Simply utility methods used across other Molindo projects
/**
* Copyright 2010 Molindo GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package at.molindo.utils.collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.TreeSet;
public class CollectionBuilder> {
public static CollectionBuilder> list(V... v) {
return builder(new ArrayList()).addAll(v);
}
public static CollectionBuilder> set(V... v) {
return builder(new HashSet()).addAll(v);
}
public static CollectionBuilder> sortedSet(V... v) {
return builder(new TreeSet()).addAll(v);
}
public static CollectionBuilder> list(Class cls) {
return builder(new ArrayList());
}
public static CollectionBuilder> set(Class cls) {
return builder(new HashSet());
}
public static CollectionBuilder> sortedSet(Class cls) {
return builder(new TreeSet());
}
public static > CollectionBuilder builder(C collection) {
return new CollectionBuilder(collection);
}
private final C _collection;
protected CollectionBuilder(C collection) {
if (collection == null) {
throw new NullPointerException("collection");
}
_collection = collection;
}
public C get() {
return _collection;
}
public CollectionBuilder add(V e) {
_collection.add(e);
return this;
}
public CollectionBuilder addAll(Collection extends V> c) {
_collection.addAll(c);
return this;
}
public CollectionBuilder addAll(V... e) {
_collection.addAll(Arrays.asList(e));
return this;
}
}