leap.lang.New Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2013 the original author or authors.
*
* 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 leap.lang;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import leap.lang.annotation.Nullable;
public class New {
/**
* Returns a new array of the given length with the specified component type.
*
* @param type the component type
* @param length the length of the new array
*/
@SuppressWarnings("unchecked")
public static T[] array(Class type, int length) {
return (T[]) Array.newInstance(type, length);
}
/**
* Create a new empty {@link ArrayList}.
*/
public static ArrayList arrayList() {
return new ArrayList();
}
/**
* Create a new {@link ArrayList} and wrapped by {@link Collections#unmodifiableList(java.util.List)}.
*/
public static List unmodifiableArrayList(Collection c){
return Collections.unmodifiableList(null == c ? new ArrayList() : new ArrayList(c));
}
/**
* Create a new {@link ArrayList}.
*/
@SuppressWarnings("unchecked")
public static ArrayList arrayList(E... elements) {
ArrayList list = new ArrayList(elements.length);
if(null != elements){
for(E e : elements){
list.add(e);
}
}
return list;
}
/**
* Create a new {@link ArrayList}.
*/
public static ArrayList arrayList(@Nullable Iterable extends E> elements) {
ArrayList list = new ArrayList();
if(null != elements){
for(E e : elements){
list.add(e);
}
}
return list;
}
/**
* Create a new {@link ArrayList}.
*/
public static ArrayList arrayList(@Nullable Iterator extends E> elements) {
ArrayList list = new ArrayList();
if(null != elements){
while(elements.hasNext()){
list.add(elements.next());
}
}
return list;
}
/**
* Create a new {@link HashSet}
*/
@SuppressWarnings("unchecked")
public static HashSet hashSet(@Nullable E... elements){
HashSet set = new HashSet();
if(null != elements){
for(E e : elements){
set.add(e);
}
}
return set;
}
/**
* Create a new {@link HashSet}.
*/
public static HashSet hashSet(@Nullable Iterable extends E> elements) {
HashSet set = new HashSet();
if(null != elements){
for(E e : elements){
set.add(e);
}
}
return set;
}
/**
* Create a new {@link HashSet}.
*/
public static HashSet hashSet(@Nullable Iterator extends E> elements) {
HashSet set = new HashSet();
if(null != elements){
while(elements.hasNext()){
set.add(elements.next());
}
}
return set;
}
/**
* Create a new {@link LinkedHashSet}.
*/
@SuppressWarnings("unchecked")
public static LinkedHashSet linkedHashSet(@Nullable E... elements) {
LinkedHashSet set = new LinkedHashSet();
if(null != elements){
for(E e : elements){
set.add(e);
}
}
return set;
}
/**
* Create a new {@link LinkedHashSet}.
*/
public static LinkedHashSet linkedHashSet(@Nullable Iterable extends E> elements) {
LinkedHashSet set = new LinkedHashSet();
if(null != elements){
for(E e : elements){
set.add(e);
}
}
return set;
}
/**
* Create a new {@link LinkedHashSet}.
*/
public static LinkedHashSet linkedHashSet(@Nullable Iterator extends E> elements) {
LinkedHashSet set = new LinkedHashSet();
if(null != elements){
while(elements.hasNext()){
set.add(elements.next());
}
}
return set;
}
/**
* Create a new {@link HashMap}
*/
public static HashMap hashMap(){
return new HashMap();
}
/**
* Create a new {@link HashMap}
*/
public static HashMap hashMap(K k1,V v1){
HashMap map = new HashMap();
map.put(k1, v1);
return map;
}
/**
* Create a new {@link HashMap}
*/
public static HashMap hashMap(K k1,V v1,K k2,V v2){
HashMap map = new HashMap();
map.put(k1, v1);
map.put(k2, v2);
return map;
}
/**
* Create a new {@link HashMap}
*/
public static HashMap hashMap(K k1,V v1,K k2,V v2, K k3, V v3){
HashMap map = new HashMap();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
return map;
}
/**
* Create a new {@link LinkedHashMap}
*/
public static LinkedHashMap linkedHashMap(){
return new LinkedHashMap();
}
/**
* Create a new {@link LinkedHashMap}
*/
public static LinkedHashMap linkedHashMap(K k1,V v1){
LinkedHashMap map = new LinkedHashMap();
map.put(k1, v1);
return map;
}
/**
* Create a new {@link LinkedHashMap}
*/
public static LinkedHashMap linkedHashMap(K k1,V v1,K k2,V v2){
LinkedHashMap map = new LinkedHashMap();
map.put(k1, v1);
map.put(k2, v2);
return map;
}
/**
* Create a new {@link LinkedHashMap}
*/
public static LinkedHashMap linkedHashMap(K k1,V v1,K k2,V v2, K k3, V v3){
LinkedHashMap map = new LinkedHashMap();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
return map;
}
protected New(){
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy