...

View Full Version : Please tell me how to compare two JSObjects.



sur_7887
01-16-2012, 09:48 AM
I want a sample program which tells me comparision between two JSObjects. Any help would be really appreciated.


Thanks,
Suraj G

felgall
01-16-2012, 08:36 PM
If you just want to check if both names point to the same object then:


if (obj1 === obj2)

If you want to check if the two objects are complety identical with the same properties and methods attached in the same order and containing the same values but not necessarily being the same object then


if (JSON.stringify(obj1) === JSON.stringify(obj2))

If the order in which the properties and methods are attached can be different and you just want to confirm that matching properties and methods containing the same values exist then you'd need to loop through all the properties and methods of both objects and check that a corresponding entry exists for the other. Just looping through all the properties and methods of one wouldn't check that there are no extra properties and methods attached to the other.

sur_7887
01-17-2012, 05:48 AM
import java.io.*;
import java.lang.ref.*;
import java.lang.reflect.*;
import java.rmi.server.ObjID;
import java.security.*;
import java.util.*;

// Imports for getJSProtectionDomain
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.SocketPermission;
import java.net.UnknownHostException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.text.NumberFormat;
import java.text.ParseException;
import sun.plugin2.applet.*;
import sun.plugin2.liveconnect.*;
import sun.plugin2.main.client.LiveConnectSupport;
import sun.plugin2.main.server.AppletID;
import sun.plugin2.main.server.JVMManager;
import sun.plugin2.main.server.Plugin;
import sun.plugin2.main.server.ResultID;
import sun.plugin2.message.*;
import sun.plugin2.util.SystemUtil;
import netscape.javascript.*;

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import netscape.javascript.*;
import sun.plugin.liveconnect.*;
import sun.plugin2.message.*;
import sun.plugin2.util.SystemUtil;

public class df extends Applet{
public static JSObject object;
private static int appletID;

public df(JSObject object,int appletID) {
this.object = object;
this.appletID = appletID;
}


public JSObject getObject() { return object; }
public int getAppletID() {return appletID; }



public boolean equals(Object arg) {
if (arg == null || arg.getClass() != getClass())
return false;
df other = (df) arg;
return (object.equals(other.object) && appletID == other.appletID);
}

public static void main(String[] args) {
try{



JSObject g1 = new JSObject() {

@Override
public void setSlot(int arg0, Object arg1) throws JSException {
// TODO Auto-generated method stub

}

@Override
public void setMember(String arg0, Object arg1) throws JSException {
// TODO Auto-generated method stub

}

@Override
public void removeMember(String arg0) throws JSException {
// TODO Auto-generated method stub

}

@Override
public Object getSlot(int arg0) throws JSException {
// TODO Auto-generated method stub
return null;
}

@Override
public Object getMember(String arg0) throws JSException {
// TODO Auto-generated method stub
return null;
}

@Override
public Object eval(String arg0) throws JSException {
// TODO Auto-generated method stub
return null;
}

@Override
public Object call(String arg0, Object[] arg1) throws JSException {
// TODO Auto-generated method stub
return null;
}
};;;
JSObject g2=new JSObject() {

@Override
public void setSlot(int arg0, Object arg1) throws JSException {
// TODO Auto-generated method stub

}

@Override
public void setMember(String arg0, Object arg1) throws JSException {
// TODO Auto-generated method stub

}

@Override
public void removeMember(String arg0) throws JSException {
// TODO Auto-generated method stub

}

@Override
public Object getSlot(int arg0) throws JSException {
// TODO Auto-generated method stub
return null;
}

@Override
public Object getMember(String arg0) throws JSException {
// TODO Auto-generated method stub
return null;
}

@Override
public Object eval(String arg0) throws JSException {
// TODO Auto-generated method stub
return null;
}

@Override
public Object call(String arg0, Object[] arg1) throws JSException {
// TODO Auto-generated method stub
return null;
}
};;;
System.out.println(g1.equals(g2));
}catch(Exception e){e.printStackTrace();}

}


}
In above code its returning me false na.. similarly i want it to return true by setting values of those two JSO's. Basically i want to compare between two JSO.. By your approach how can i be able to compare it can you please modify existing code and send me back . that would be great!

VIPStephan
02-22-2012, 12:28 PM
Sorry, the last post was hidden in the moderation queue. However, this is JAVA, not JavaScript. Keep that in mind for the next time, sur_7887. I’m moving it to the correct forum.

Fou-Lu
02-22-2012, 01:36 PM
Since JSObject is not a built in interface, you'll need to modify the JSObject interface and add an extension of type Comparable<JSObject> to it.
This will force you to implement CompareTo so you can compare two objects of the same type and return 0 if its equal, < 0 if "other" is < "this", and > 0 if "this" is > "other". You need to write what it compares of course.
This can then be used in any collection type to sort, or used manually with jsobject1.compareTo(jsobject2). Not sure exactly what it will do if you use it in anonymous inner classes with a collection, but I presume it will work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum