Java Code Examples for ch.njol.util.coll.CollectionUtils#contains()

The following examples show how to use ch.njol.util.coll.CollectionUtils#contains() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Timespan.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Nullable
public static Timespan parse(final String s) {
	if (s.isEmpty())
		return null;
	long t = 0;
	boolean minecraftTime = false;
	boolean isMinecraftTimeSet = false;
	if (s.matches("^\\d+:\\d\\d(:\\d\\d)?(\\.\\d{1,4})?$")) { // MM:SS[.ms] or HH:MM:SS[.ms]
		final String[] ss = s.split("[:.]");
		final long[] times = {1000L * 60L * 60L, 1000L * 60L, 1000L, 1L}; // h, m, s, ms
		
		final int offset = ss.length == 3 && !s.contains(".") || ss.length == 4 ? 0 : 1;
		for (int i = 0; i < ss.length; i++) {
			t += times[offset + i] * Utils.parseLong("" + ss[i]);	
		}
	} else {
		final String[] subs = s.toLowerCase().split("\\s+");
		for (int i = 0; i < subs.length; i++) {
			String sub = subs[i];
			
			if (sub.equals(GeneralWords.and.toString())) {
				if (i == 0 || i == subs.length - 1)
					return null;
				continue;
			}
			
			double amount = 1;
			if (Noun.isIndefiniteArticle(sub)) {
				if (i == subs.length - 1)
					return null;
				amount = 1;
				sub = subs[++i];
			} else if (sub.matches("^\\d+(.\\d+)?$")) {
				if (i == subs.length - 1)
					return null;
				try {
					amount = Double.parseDouble(sub);
				} catch (NumberFormatException e) {
					throw new IllegalArgumentException("invalid timespan: " + s);
				}
				sub = subs[++i];
			}
			
			if (CollectionUtils.contains(Language.getList("time.real"), sub)) {
				if (i == subs.length - 1 || isMinecraftTimeSet && minecraftTime)
					return null;
				sub = subs[++i];
			} else if (CollectionUtils.contains(Language.getList("time.minecraft"), sub)) {
				if (i == subs.length - 1 || isMinecraftTimeSet && !minecraftTime)
					return null;
				minecraftTime = true;
				sub = subs[++i];
			}
			
			if (sub.endsWith(","))
				sub = sub.substring(0, sub.length() - 1);
			
			final Long d = parseValues.get(sub.toLowerCase());
			if (d == null)
				return null;
			
			if (minecraftTime && d != times[0]) // times[0] == tick
				amount /= 72f;
			
			t += Math.round(amount * d);
			
			isMinecraftTimeSet = true;
			
		}
	}
	return new Timespan(t);
}
 
Example 2
Source File: Utils.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
public static <T> boolean isEither(@Nullable T compared, @Nullable T... types) {
	return CollectionUtils.contains(types, compared);
}