com.github.sommeri.less4j.utils.ArraysUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of less4j Show documentation
Show all versions of less4j Show documentation
Less language is an extension of css and less4j compiles it into regular css. It adds several dynamic features into css: variables, expressions, nested rules.
Less4j is a port. The original compiler was written in JavaScript and is called less.js. The less language is mostly defined in less.js documentation/issues and by what less.js actually do. Links to less.js:
* home page: http://lesscss.org/
* source code & issues: https://github.com/cloudhead/less.js
package com.github.sommeri.less4j.utils;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.github.sommeri.less4j.core.ast.SelectorPart;
public class ArraysUtils {
public interface Filter {
public boolean accept(T t);
}
public static T[] asArray(T... args) {
return args;
}
public static Set asSet(T... args) {
return new HashSet(Arrays.asList(args));
}
public static List asList(T... args) {
return new ArrayList(Arrays.asList(args));
}
public static int count(Collection collection, Filter filter) {
int result = 0;
for (T t : collection) {
if (filter.accept(t))
result++;
}
return result;
}
public static List sameLengthSuffix(List ofList, List asThis) {
if (asThis.size()>ofList.size())
return null;
int to = ofList.size();
int from = to - asThis.size();
return ofList.subList(from, to);
}
public static List sameLengthPrefix(List ofList, List asThis) {
if (asThis.size()>ofList.size())
return null;
return ofList.subList(0, asThis.size());
}
public static List sublistWithoutLast(List values) {
if (values.isEmpty())
return null;
return values.subList(0, values.size()-1);
}
public static List sublistWithoutFirst(List values) {
if (values.isEmpty())
return null;
return values.subList(1, values.size());
}
public static List safeSublist(List values, int from, int to) {
if (values.isEmpty())
return null;
if (from>to)
return new ArrayList();
if (from>values.size())
from=values.size();
if (to>values.size())
to=values.size();
return values.subList(from, to);
}
public static T last(List values) {
if (values.isEmpty())
return null;
return values.get(values.size() - 1);
}
public static T chopLast(List values) {
if (values.isEmpty())
return null;
return values.remove(values.size() - 1);
}
public static T first(List values) {
if (values.isEmpty())
return null;
return values.get(0);
}
public static T chopFirst(List values) {
if (values.isEmpty())
return null;
return values.remove(0);
}
public static List chopUpTo(List list, T exclusiveTo) {
int indx = list.indexOf(exclusiveTo);
if (indx == -1)
return new ArrayList();
List subList = list.subList(0, indx);
List result = new ArrayList(subList);
subList.clear();
return result;
}
public static List chopPrefix(List list, int exclusiveTo) {
if (exclusiveTo > list.size())
exclusiveTo = list.size();
List subList = list.subList(0, exclusiveTo);
List result = new ArrayList(subList);
subList.clear();
return result;
}
public static List asNonNullList(T... a) {
List result = new ArrayList();
for (T t : a) {
if (t != null)
result.add(t);
}
return result;
}
public static List addIfNonNull(List destination, T... a) {
for (T t : a) {
if (t != null)
destination.add(t);
}
return destination;
}
@SuppressWarnings("unchecked")
public static List deeplyClonedList(List list) {
List result = new ArrayList();
for (T t : list) {
result.add((T)t.clone());
}
return result;
}
@SuppressWarnings("unchecked")
public static LinkedList deeplyClonedLinkedList(LinkedList list) {
LinkedList result = new LinkedList();
for (T t : list) {
result.add((T)t.clone());
}
return result;
}
@SuppressWarnings("unchecked")
public static Map deeplyClonedMap(Map map) {
Map result = new HashMap();
for (Entry t : map.entrySet()) {
result.put(t.getKey(), (T)(t.getValue().clone()));
}
return result;
}
public static Map> deeplyClonedListInMap(Map> map) {
Map> result = new HashMap>();
for (Entry> t : map.entrySet()) {
result.put(t.getKey(), deeplyClonedList(t.getValue()));
}
return result;
}
public static List remaining(Iterator iterator) {
List result = new ArrayList();
while (iterator.hasNext())
result.add(iterator.next());
return result;
}
public static List asModifiableList(T... head) {
List result = new ArrayList();
for (T t : head) {
result.add(t);
}
return result;
}
public static List joinAll(List... lists) {
List result = new ArrayList();
for (List t : lists) {
result.addAll(t);
}
return result;
}
public static void replace(SelectorPart lookFor, List inside, List replaceBy) {
int indx = inside.indexOf(lookFor);
inside.remove(lookFor);
inside.addAll(indx, replaceBy);
}
public static boolean isUtf8(byte[] input) {
return isEncodedAs(input, "UTF-8");
}
public static boolean isUsAscii(byte[] input) {
return isEncodedAs(input, "US-ASCII");
}
public static boolean isEncodedAs(byte[] input, String encoding) {
CharsetDecoder decoder = Charset.forName(encoding).newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
try {
decoder.decode(ByteBuffer.wrap(input));
} catch (CharacterCodingException e) {
return false;
}
return true;
}
public static void replace(List inList, Q oldElement, LinkedList newElements) {
int level = inList.indexOf(oldElement);
inList.remove(level);
inList.addAll(level, newElements);
}
}