package org.jruby.util.collections;
import java.util.HashSet;
import java.util.Set;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBoolean;
import org.jruby.RubyString;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
public class StringArraySet extends RubyArray {
private final Set<String> set;
public StringArraySet(Ruby runtime) {
super(runtime, 16);
this.set = new HashSet<>(20);
}
public final void appendString(Ruby runtime, String element) {
final RubyString item = runtime.newString(element);
synchronized (this) {
super.append(item);
set.add(element);
}
}
@Override
public synchronized RubyArray append(IRubyObject item) {
RubyArray result = super.append(item);
set.add(convertToString(item));
return result;
}
@Override
public synchronized IRubyObject rb_clear() {
IRubyObject res = super.rb_clear();
set.clear();
return res;
}
public final void deleteString(ThreadContext context, String element) {
final RubyString item = context.runtime.newString(element);
synchronized (this) {
super.delete(context, item, Block.NULL_BLOCK);
set.remove(element);
}
}
@Override
public synchronized IRubyObject delete(ThreadContext context, IRubyObject item, Block block) {
IRubyObject result = super.delete(context, item, block);
if ( ! includes(context, item) ) set.remove(convertToString(item));
return result;
}
@Override
public synchronized IRubyObject delete_if(ThreadContext context, Block block) {
IRubyObject result = super.delete_if(context, block);
return result;
}
@Override
public final RubyBoolean include_p(ThreadContext context, IRubyObject item) {
return context.runtime.newBoolean(containsString(convertToString(item)));
}
@Override
public synchronized IRubyObject replace(IRubyObject orig) {
IRubyObject result = super.replace(orig);
rehash();
return result;
}
@Override
public synchronized IRubyObject aset(IRubyObject arg0, IRubyObject arg1) {
IRubyObject result = super.aset(arg0, arg1);
rehash();
return result;
}
@Override
public synchronized IRubyObject aset(IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
IRubyObject result = super.aset(arg0, arg1, arg2);
rehash();
return result;
}
@Override
public synchronized RubyArray collectBang(ThreadContext context, Block block) {
RubyArray result = super.collectBang(context, block);
rehash();
return result;
}
@Override
public synchronized IRubyObject compact_bang() {
IRubyObject result = super.compact_bang();
rehash();
return result;
}
@Override
public synchronized IRubyObject drop(ThreadContext context, IRubyObject n) {
IRubyObject result = super.drop(context, n);
rehash();
return result;
}
@Override
public synchronized IRubyObject drop_while(ThreadContext context, Block block) {
IRubyObject result = super.drop_while(context, block);
rehash();
return result;
}
@Override
public synchronized IRubyObject flatten_bang(ThreadContext context) {
IRubyObject result = super.flatten_bang(context);
rehash();
return result;
}
@Override
public synchronized IRubyObject flatten_bang(ThreadContext context, IRubyObject arg) {
IRubyObject result = super.flatten_bang(context, arg);
rehash();
return result;
}
@Override
public synchronized IRubyObject insert() {
IRubyObject result = super.insert();
rehash();
return result;
}
@Override
public synchronized IRubyObject insert(IRubyObject arg) {
IRubyObject result = super.insert(arg);
rehash();
return result;
}
@Override
public synchronized IRubyObject insert(IRubyObject arg1, IRubyObject arg2) {
IRubyObject result = super.insert(arg1, arg2);
rehash();
return result;
}
@Override
public synchronized IRubyObject insert(IRubyObject[] args) {
IRubyObject result = super.insert(args);
rehash();
return result;
}
@Override
public synchronized IRubyObject pop(ThreadContext context) {
IRubyObject result = super.pop(context);
rehash();
return result;
}
@Override
public synchronized IRubyObject pop(ThreadContext context, IRubyObject num) {
IRubyObject result = super.pop(context, num);
rehash();
return result;
}
@Override
public synchronized RubyArray push(IRubyObject item) {
RubyArray result = super.push(item);
add(item);
return result;
}
@Override
public synchronized RubyArray push(IRubyObject[] items) {
RubyArray result = super.push(items);
addAll(items);
return result;
}
@Override
public synchronized IRubyObject rejectBang(ThreadContext context, Block block) {
IRubyObject result = super.rejectBang(context, block);
rehash();
return result;
}
@Override
public synchronized IRubyObject select_bang(ThreadContext context, Block block) {
IRubyObject result = super.select_bang(context, block);
rehash();
return result;
}
@Override
public synchronized IRubyObject shift(ThreadContext context) {
IRubyObject result = super.shift(context);
if ( result != context.nil ) rehash();
return result;
}
@Override
public synchronized IRubyObject shift(ThreadContext context, IRubyObject num) {
IRubyObject result = super.shift(context, num);
rehash();
return result;
}
@Override
public synchronized IRubyObject slice_bang(IRubyObject arg0) {
IRubyObject result = super.slice_bang(arg0);
rehash();
return result;
}
@Override
public synchronized IRubyObject slice_bang(IRubyObject arg0, IRubyObject arg1) {
IRubyObject result = super.slice_bang(arg0, arg1);
rehash();
return result;
}
@Override
public synchronized IRubyObject unshift() {
IRubyObject result = super.unshift();
return result;
}
@Override
public synchronized IRubyObject unshift(IRubyObject item) {
IRubyObject result = super.unshift(item);
add(item);
return result;
}
@Override
public synchronized IRubyObject unshift(IRubyObject[] items) {
IRubyObject result = super.unshift(items);
addAll(items);
return result;
}
public final boolean containsString(String element) {
synchronized (this) { return set.contains(element); }
}
private static String convertToString(IRubyObject item) {
return item.convertToString().asJavaString();
}
private void rehash() {
set.clear();
addAll(toJavaArrayMaybeUnsafe());
}
private void add(IRubyObject item) {
set.add(convertToString(item));
}
private void addAll(IRubyObject[] items) {
for (IRubyObject item : items) {
set.add(convertToString(item));
}
}
}