/* --------------------------------------------------------- *
* __________ D E L T A S C R I P T *
* (_________() *
* / === / - A fast, dynamic scripting language *
* | == | - Version 4.13.11.0 *
* / === / - Developed by Adam R. Nelson *
* | = = | - 2011-2013 *
* / === / - Distributed under GNU LGPL v3 *
* (________() - http://github.com/ar-nelson/deltascript *
* *
* --------------------------------------------------------- */
package com.sector91.delta.script.objects;
import java.io.Serializable;
import java.util.*;
import com.sector91.delta.script.DScriptContext;
import com.sector91.delta.script.DScriptErr;
import com.sector91.delta.script.DeltaScript;
import com.sector91.delta.script.Operator;
import com.sector91.delta.script.annotations.DSDynamicField;
import com.sector91.delta.script.annotations.DSInaccessible;
import com.sector91.delta.script.annotations.DSName;
import com.sector91.delta.script.annotations.DSType;
import com.sector91.delta.script.objects.reflect.DS_JavaClass;
import com.sector91.util.StringUtil;
/**
* <p>A mutable collection of objects in no particular order, containing no
* duplicates.</p>
*
* @author Adam R. Nelson
* @version 4.13.11.0
*/
@DSType("Set")
public class DS_Set extends DS_AbstractObject
implements Set<DS_Object>,
DS_MutableCollection,
Serializable
{
private static final long serialVersionUID =
DeltaScript.VERSION.majorVersion();
public static final String TYPE_NAME = "Set";
private static final DS_JavaClass DSCLASS = DS_JavaClass.fromClass(
DS_Set.class);
private final Set<DS_Object> set;
public DS_Set()
{this.set = new HashSet<DS_Object>();}
@DSInaccessible
public DS_Set(Collection<? extends DS_Object> objs)
{this.set = new HashSet<DS_Object>(objs);}
public DS_Set(DS_Object... objs)
{this(Arrays.asList(objs));}
// Sequence Methods
// ----------------------------------------------------
@DSName("addAll")
public void addAll(DS_Object... items)
{for (DS_Object item : items) set.add(item);}
@DSName("removeAll")
public void removeAll(DS_Object... items)
{for (DS_Object item : items) set.remove(item);}
@DSName("retainAll")
public void retainAll(DS_Object... items)
{
Set<DS_Object> oldSet = new HashSet<DS_Object>(set);
set.clear();
for (DS_Object item : items) if (oldSet.contains(item)) set.add(item);
}
@DSName("removeWhere")
public void removeWhere(DS_Callable callable)
throws DScriptErr
{
final Iterator<DS_Object> iter = set.iterator();
while (iter.hasNext())
if (callable.call(iter.next()).booleanValue())
iter.remove();
}
@DSName("retainWhere")
public void retainWhere(DS_Callable callable)
throws DScriptErr
{
final Iterator<DS_Object> iter = set.iterator();
while (iter.hasNext())
if (!callable.call(iter.next()).booleanValue())
iter.remove();
}
// Collection and Iterable Methods
// ----------------------------------------------------
@DSName("size") @DSDynamicField
public int size()
{return set.size();}
@DSName("empty") @DSDynamicField
public boolean isEmpty()
{return set.isEmpty();}
@DSName("contains") @DSDynamicField
public boolean contains(Object o)
{return set.contains(o);}
@DSInaccessible
public Iterator<DS_Object> iterator()
{return set.iterator();}
@DSName("each")
public void each(DS_Callable func) throws DScriptErr
{
for (DS_Object obj : set)
func.call(obj);
}
@DSInaccessible
public Object[] toArray()
{return set.toArray();}
@DSInaccessible
public <T> T[] toArray(T[] a)
{return set.toArray(a);}
@DSInaccessible
public DS_Object[] dumpToArray()
{return set.toArray(new DS_Object[size()]);}
@DSName("add")
public boolean add(DS_Object e)
{return set.add(e);}
@DSName("remove")
public boolean remove(Object o)
{return set.remove(o);}
@DSName("containsAll")
public boolean containsAll(DS_Object... items)
{return set.containsAll(Arrays.asList(items));}
@DSInaccessible
public boolean containsAll(Collection<?> c)
{return set.containsAll(c);}
@DSInaccessible
public boolean addAll(Collection<? extends DS_Object> c)
{return set.addAll(c);}
@DSInaccessible
public boolean removeAll(Collection<?> c)
{return set.removeAll(c);}
@DSInaccessible
public boolean retainAll(Collection<?> c)
{return set.retainAll(c);}
@DSName("clear")
public void clear()
{set.clear();}
// DS_Object Methods
// ----------------------------------------------------
public Object unbox()
{return set;}
public String getTypeName()
{return TYPE_NAME;}
@Override protected DS_JavaClass getDeltaScriptClass()
{return DSCLASS;}
@SuppressWarnings("incomplete-switch")
@Override public DS_Object operator(Operator op, DS_Object other)
throws DScriptErr
{
switch (op)
{
case ABSOLUTE:
return ScalarFactory.fromInt(size());
case RANDOM:
final int n = DScriptContext.getContextRandom().nextInt(size());
Iterator<DS_Object> iter = set.iterator();
for (int i=0; i<n-1; i++) iter.next();
return iter.next();
case IN:
return DS_Boolean.box(contains(other));
}
if (other instanceof DS_Iterable)
{
DS_Iterable seq = (DS_Iterable)other;
DS_Set newSet;
switch (op)
{
case BIT_OR:
newSet = new DS_Set(this.dumpToArray());
newSet.addAll(seq.dumpToArray());
return newSet;
case SUBTRACT:
newSet = new DS_Set(this.dumpToArray());
newSet.removeAll(seq.dumpToArray());
return newSet;
case BIT_AND:
newSet = new DS_Set(this.dumpToArray());
newSet.retainAll(seq.dumpToArray());
return newSet;
}
}
return super.operator(op, other);
}
public boolean equals(DS_Object other)
{
if (other instanceof DS_Set)
return this.unbox().equals(other.unbox());
return false;
}
@Override public int hashCode()
{return set.hashCode();}
@Override public String toString()
{return standardToString(StringUtil.wrapBracketedList(set, 73));}
}