Move UglySQL into MUtil as transform

This commit is contained in:
Olof Larsson 2015-09-03 13:27:20 +02:00
parent 7b5f6acd1b
commit ab88ed8176
3 changed files with 92 additions and 87 deletions

View File

@ -5,6 +5,7 @@ import java.util.List;
import java.util.Map.Entry;
import com.massivecraft.massivecore.Predictate;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
@ -56,17 +57,17 @@ public abstract class CollAbstract<E> implements CollInterface<E>
return this.containsIdFixed(this.fixId(oid));
}
@Override public List<E> getAll(Predictate<? super E> where) { return MStoreUtil.uglySQL(this.getAll(), where, null, null, null); }
@Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, null, null); }
@Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, null); }
@Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, offset); }
@Override public List<E> getAll(Predictate<? super E> where, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), where, null, limit, null); }
@Override public List<E> getAll(Predictate<? super E> where, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), where, null, limit, offset); }
@Override public List<E> getAll(Comparator<? super E> orderby) { return MStoreUtil.uglySQL(this.getAll(), null, orderby, null, null); }
@Override public List<E> getAll(Comparator<? super E> orderby, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), null, orderby, limit, null); }
@Override public List<E> getAll(Comparator<? super E> orderby, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), null, orderby, limit, offset); }
@Override public List<E> getAll(Integer limit) { return MStoreUtil.uglySQL(this.getAll(), null, null, limit, null); }
@Override public List<E> getAll(Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), null, null, limit, offset); }
@Override public List<E> getAll(Predictate<? super E> where) { return MUtil.transform(this.getAll(), where); }
@Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby) { return MUtil.transform(this.getAll(), where, orderby); }
@Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit) { return MUtil.transform(this.getAll(), where, orderby, limit); }
@Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit, Integer offset) { return MUtil.transform(this.getAll(), where, orderby, limit, offset); }
@Override public List<E> getAll(Predictate<? super E> where, Integer limit) { return MUtil.transform(this.getAll(), where, limit); }
@Override public List<E> getAll(Predictate<? super E> where, Integer limit, Integer offset) { return MUtil.transform(this.getAll(), where, limit, offset); }
@Override public List<E> getAll(Comparator<? super E> orderby) { return MUtil.transform(this.getAll(), orderby); }
@Override public List<E> getAll(Comparator<? super E> orderby, Integer limit) { return MUtil.transform(this.getAll(), orderby, limit); }
@Override public List<E> getAll(Comparator<? super E> orderby, Integer limit, Integer offset) { return MUtil.transform(this.getAll(), orderby, limit, offset); }
@Override public List<E> getAll(Integer limit) { return MUtil.transform(this.getAll(), limit); }
@Override public List<E> getAll(Integer limit, Integer offset) { return MUtil.transform(this.getAll(), limit, offset); }
// -------------------------------------------- //
// BEHAVIOR

View File

@ -1,76 +0,0 @@
package com.massivecraft.massivecore.store;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import com.massivecraft.massivecore.Predictate;
public class MStoreUtil
{
public static <T> ArrayList<T> uglySQL(Collection<T> items, Predictate<? super T> where, Comparator<? super T> orderby, Integer limit, Integer offset)
{
ArrayList<T> ret = new ArrayList<T>(items.size());
// WHERE
if (where == null)
{
ret.addAll(items);
}
else
{
for (T item : items)
{
if (where.apply(item))
{
ret.add(item);
}
}
}
// ORDERBY
if (orderby != null)
{
Collections.sort(ret, orderby);
}
// LIMIT AND OFFSET
// Parse args
int fromIndex = 0;
if (offset != null)
{
fromIndex = offset;
}
int toIndex = ret.size()-1;
if (limit != null)
{
toIndex = offset+limit;
}
// Clean args
if (fromIndex <= 0)
{
fromIndex = 0;
}
else if (fromIndex > ret.size()-1)
{
fromIndex = ret.size()-1;
}
if (toIndex < fromIndex)
{
toIndex = fromIndex;
}
else if (toIndex > ret.size()-1)
{
toIndex = ret.size()-1;
}
// No limit?
if (fromIndex == 0 && toIndex == ret.size()-1) return ret;
return new ArrayList<T>(ret.subList(fromIndex, toIndex));
}
}

View File

@ -59,6 +59,7 @@ import com.massivecraft.massivecore.CaseInsensitiveComparator;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveCoreEngineMain;
import com.massivecraft.massivecore.MassiveCoreEngineWorldNameSet;
import com.massivecraft.massivecore.Predictate;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.collections.MassiveTreeSet;
@ -1247,6 +1248,85 @@ public class MUtil
return MassiveCoreEngineWorldNameSet.get().getWorldNames();
}
// -------------------------------------------- //
// TRANSFORM
// -------------------------------------------- //
public static <T> List<T> transform(Collection<T> items, Predictate<? super T> where, Comparator<? super T> orderby, Integer limit, Integer offset)
{
List<T> ret = new ArrayList<T>(items.size());
// WHERE
if (where == null)
{
ret.addAll(items);
}
else
{
for (T item : items)
{
if (where.apply(item))
{
ret.add(item);
}
}
}
// ORDERBY
if (orderby != null)
{
Collections.sort(ret, orderby);
}
// LIMIT AND OFFSET
// Parse args
int fromIndex = 0;
if (offset != null)
{
fromIndex = offset;
}
int toIndex = ret.size()-1;
if (limit != null)
{
toIndex = offset+limit;
}
// Clean args
if (fromIndex <= 0)
{
fromIndex = 0;
}
else if (fromIndex > ret.size()-1)
{
fromIndex = ret.size()-1;
}
if (toIndex < fromIndex)
{
toIndex = fromIndex;
}
else if (toIndex > ret.size()-1)
{
toIndex = ret.size()-1;
}
// No limit?
if (fromIndex == 0 && toIndex == ret.size()-1) return ret;
return new ArrayList<T>(ret.subList(fromIndex, toIndex));
}
public static <T> List<T> transform(Collection<T> items, Predictate<? super T> where) { return transform(items, where, null, null, null); }
public static <T> List<T> transform(Collection<T> items, Predictate<? super T> where, Comparator<? super T> orderby) { return transform(items, where, orderby, null, null); }
public static <T> List<T> transform(Collection<T> items, Predictate<? super T> where, Comparator<? super T> orderby, Integer limit) { return transform(items, where, orderby, limit, null); }
public static <T> List<T> transform(Collection<T> items, Predictate<? super T> where, Integer limit) { return transform(items, where, null, limit, null); }
public static <T> List<T> transform(Collection<T> items, Predictate<? super T> where, Integer limit, Integer offset) { return transform(items, where, null, limit, offset); }
public static <T> List<T> transform(Collection<T> items, Comparator<? super T> orderby) { return transform(items, null, orderby, null, null); }
public static <T> List<T> transform(Collection<T> items, Comparator<? super T> orderby, Integer limit) { return transform(items, null, orderby, limit, null); }
public static <T> List<T> transform(Collection<T> items, Comparator<? super T> orderby, Integer limit, Integer offset) { return transform(items, null, orderby, limit, offset); }
public static <T> List<T> transform(Collection<T> items, Integer limit) { return transform(items, null, null, limit, null); }
public static <T> List<T> transform(Collection<T> items, Integer limit, Integer offset) { return transform(items, null, null, limit, offset); }
// -------------------------------------------- //
// SIMPLE CONSTRUCTORS
// -------------------------------------------- //