001 /*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.util;
021
022 import java.util.ArrayList;
023 import java.util.HashMap;
024 import java.util.HashSet;
025 import java.util.Iterator;
026 import java.util.LinkedList;
027 import java.util.List;
028
029 public class Utils {
030
031 public static <E> ArrayList<E> newArrayList() {
032 return new ArrayList<E>();
033 }
034
035 public static <E> LinkedList<E> newLinkedList() {
036 return new LinkedList<E>();
037 }
038
039 public static <E> HashSet<E> newHashSet() {
040 return new HashSet<E>();
041 }
042
043 public static <K, V> HashMap<K, V> newHashMap() {
044 return new HashMap<K, V>();
045 }
046
047 public static <E>List<E> list(Iterable<E> iterable) {
048 return list(iterable.iterator());
049 }
050
051 public static <E>List<E> list(Iterator<E> iterator) {
052 ArrayList<E> list = new ArrayList<E>();
053 while (iterator.hasNext()) {
054 list.add(iterator.next());
055 }
056 return list;
057 }
058
059 public static int indexOf(CharSequence s, int off, char c) {
060 for (int len = s.length();off < len;off++) {
061 if (s.charAt(off) == c) {
062 return off;
063 }
064 }
065 return -1;
066 }
067
068 public static String trimLeft(String s) {
069 if (s == null) {
070 throw new NullPointerException("No null string accepted");
071 }
072 int index = 0;
073 int len = s.length();
074 while (index < len) {
075 if (s.charAt(index) == ' ') {
076 index++;
077 } else {
078 break;
079 }
080 }
081 if (index > 0) {
082 return s.substring(index);
083 } else {
084 return s;
085 }
086 }
087
088 public static <E> E notNull(E e1, E e2) {
089 if (e1 != null) {
090 return e1;
091 } else {
092 return e2;
093 }
094 }
095 }