How to delete value from jsarray in GWT? - gwt

i have two questions:
1) is it possible to delete any value in jsarray or only the last on with the pop method?
2) how can i remove or delete a value from jsarray? does somebody can post an example.
something like this here
public JsArray<MyObject> myObjects = JavaScriptObject.createArray().cast();
myObjects.push(new MyObject("Good"));
myObjects.push(new MyObject("morning"));
myObjects.push(new MyObject("people"));
myObjects.delete(1);
thx a lot!

Arrays in JavaScript are sparse, so you cannot, for example, remove an object from it and have all the following be moved up to lower indices (like you'd have in Java with a List for instance); at least not with some remove method.
Using only GWT Java, you can set the value at a specific index to null, but that's it.
Using JSNI, you can delete it (almost equivalent to setting it to undefined: delete myObjects[1]) or you can remove it:
public static native remove(JsArray<?> arr, int index, int count) /*-{
arr.splice(index, count);
}-*/;

Related

Are lists ensured to be processed in order by a for loop?

In a Flutter application, is the order of a list always ensured?
List<MyClass> myObjects;
// ... many add operations
int i = 0;
for(MyClass myObject in myObjects) {
assert(myObject == myObjects[i]); // will it always go through?
i++;
}
If not, what is the most efficient way to ensure a list is maintained and processed in the right order?
Yes. for-in in Dart works on Iterable objects. (It is not like for-in in JavaScript which iterates over object properties, which could be in some indeterminate order.)
From the List documentation:
Lists are Iterable. Iteration occurs over values in index order.

How to copy a bsoncxx::builder::basic::document to another?

Is there any way to safely copy a bsoncxx document to another.
In following code I am not able to do that
class DocClass
{
private:
bsoncxx::builder::basic::document m_doc;
public:
bsoncxx::builder::basic::document& copy(bsoncxx::builder::basic::document& obj)
{
obj = m_doc; //Not allowed
//Error C2280 attempting to reference a deleted function
}
};
There should not be any harm to the object even after copy.
Please help.
Thanks,
Shibin
If you want to copy a bsoncxx::document::value, you can construct a new one from its view:
bsoncxx::document::value foo = ...;
bsoncxx::document::value bar{foo.view()};
bsoncxx::builder::basic::document is only movable, not copyable. However, you can get view to the underlying document from the builder with the view() method, which might be able to help you depending on your use cases. You'll still only be able to extract from the builder once though, so you'll have to rely on constructing a second document::value if you need more than one.

How to create GWT JsArray?

I need to convert values of type T into JsArray.
For example, I have String1, String2 .... Stringn. I need to convert these Strings into JsArray string.
How can I implement this?
You don't have much choices: creating a JsArrayString and adding to it, or using JSNI.
JsArrayString arr = JavaScriptObject.createArray().cast();
arr.push(str1);
arr.push(str2);
arr.push(str3);
or
static native JsArrayString asJsArray(String str1, String str2, String str3) /*-{
return [str1, str2, str3];
}-*/;
Obviously, the latter doesn't scale, while being faster.
It really depends what exactly you need to do.
Use JsArrayUtils like this:
JsArray<String> arr = JsArrayUtils.readOnlyJsArray(new String[] { string1, string2 });
Take a look at the javadoc:
com.google.gwt.core.client.JsArrayUtils
Utility class for manipulating JS arrays. These methods are not on
other JavaScriptObject subclasses, such as JsArray, because adding new
methods might break existing subtypes.
Using generics, could do it like this:
public <T extends JavaScriptObject> JsArray<T> createGenericArray(T... objects) {
JsArray<T> array = JavaScriptObject.createArray().cast();
for (T object : objects) {
array.push(object);
}
return array;
}
Obviously, String doesn't extend JavaScriptObject. You'd need to have overloads to account for primitive types. (Or, less safely, you could remove the bounds of T to allow for arbitrary types. You'd need to be much more careful if you were to do so.)

Richfaces 4 dynamic select options when user type

I am using rich faces select component.
I want dynamic values when user manually type some thing in the select component.
<rich:select enableManualInput="true" defaultLabel="start typing for select" value="#{supplierSearchBean.userInput}">
<a4j:ajax event="keyup" execute="#this" listener="#{supplierSearchBean.userInputChange}"/>
<f:selectItems value="#{supplierSearchBean.selectOptions}" />
</rich:select>
Java code as follows
public void userInputChange(ActionEvent ae){
Map map = ae.getComponent().getAttributes();
System.out.println(map.toString());
}
public void setUserInput(String userInput) {
System.out.println("userINput = " + userInput);
this.userInput = userInput;
}
Here i found 2 issues
1st: setUserINput always print empty string when user type value
2nd: listener method never get call.
any help ?
The problem is most probably that there is no selected value while the user types, and this component restricts the allowed values to the specified select items. A partial input is thus not valid and cannot be bound to your bean.
I think you could get the expected behavior if you use a rich:autocomplete instead. However, if you want to restrict the allowed values, maybe you can keep your rich:select and listen for the selectitem event.
Override getItems function in richfaces-utils.js file in richfaces-core-impl-4.0.0.Final.jar under richfaces-core-impl-4.0.0.Final\META-INF\resources folder.
Change the condition of pushing items to be
if(p != -1)
instead of
if(p == 0)
This should fix the issue.

Is this C# casting useless?

I have two methods like so:
Foo[] GetFoos(Type t) { //do some stuff and return an array of things of type T }
T[] GetFoos<T>()
where T : Foo
{
return GetFoos(typeof(T)) as T[];
}
However, this always seems to return null. Am I doing things wrong or is this just a shortfall of C#?
Nb:
I know I could solve this problem with:
GetFoos(typeof(T)).Cast<T>().ToArray();
However, I would prefer to do this wothout any allocations (working in an environment very sensitive to garbage collections).
Nb++:
Bonus points if you suggest an alternative non allocating solution
Edit:
This raises an interesting question. The MSDN docs here: http://msdn.microsoft.com/en-us/library/aa664572%28v=vs.71%29.aspx say that the cast will succeed if there is an implicit or explicit cast. In this case there is an explicit cast, and so the cast should succeed. Are the MSDN docs wrong?
No, C# casting isn't useless - you simply can't cast a Foo[] to a T[] where T is a more derived type, as the Foo[] could contain other elements different to T. Why don't you adjust your GetFoos method to GetFoos<T>()? A method only taking a Type object can easily be converted into a generic method, where you could create the array directly via new T[].
If this is not possible: Do you need the abilities an array offers (ie. indexing and things like Count)? If not, you can work with an IEnumerable<T> without having much of a problem. If not: you won't get around going the Cast<T>.ToArray() way.
Edit:
There is no possible cast from Foo[] to T[], the description in your link is the other way round - you could cast a T[] to a Foo[] as all T are Foo, but not all Foo are T.
If you can arrange for GetFoos to create the return array using new T[], then you win. If you used new Foo[], then the array's type is fixed at that, regardless of the types of the objects it actually holds.
I haven't tried this, but it should work:
T[] array = Array.ConvertAll<Foo, T>(input,
delegate(Foo obj)
{
return (T)obj;
});
You can find more at http://msdn.microsoft.com/en-us/library/exc45z53(v=VS.85).aspx
I think this converts in-place, so it won't be doing any re-allocations.
From what I understand from your situation, using System.Array in place of a more specific array can help you. Remember, Array is the base class for all strongly typed arrays so an Array reference can essentially store any array. You should make your (generic?) dictionary map Type -> Array so you may store any strongly typed array also while not having to worry about needing to convert one array to another, now it's just type casting.
i.e.,
Dictionary<Type, Array> myDict = ...;
Array GetFoos(Type t)
{
// do checks, blah blah blah
return myDict[t];
}
// and a generic helper
T[] GetFoos<T>() where T: Foo
{
return (T[])GetFoos(typeof(T));
}
// then accesses all need casts to the specific type
Foo[] f = (Foo[])GetFoos(typeof(Foo));
DerivedFoo[] df = (DerivedFoo[])GetFoos(typeof(DerivedFoo));
// or with the generic helper
AnotherDerivedFoo[] adf = GetFoos<AnotherDerivedFoo>();
// etc...
p.s., The MSDN link that you provide shows how arrays are covariant. That is, you may store an array of a more derived type in a reference to an array of a base type. What you're trying to achieve here is contravariance (i.e., using an array of a base type in place of an array of a more derived type) which is the other way around and what arrays can't do without doing a conversion.