Randomly Intermittent Thoughts

    Thoughts I have pondered, but may not be wholely original…

    Browsing Posts published by nblom

    Yesterday I was digging around the interwebs looking to find out how I can hibernate a computer from the command line. My first thought was “Oh, the shutdown command might work”, but nope. It doesn’t have a hibernate option. Then I went to Google and did a search for it. A forum thread lead me to Wizmo. This is a freeware utility that can hibernate a computer plus a lot more. I think this is getting added to my toolbox of tools!

    Thanks Steve!

    As I was reading other blogs, I found an interesting article on Parenting Science about preschoolers and math board games.
    Direct link is here: Preschool math games.

    What I liked was the fact they discuss a game that can be created by both the parent and preschooler (fun for the whole family). But it also teaches basic math skills. Good times!

    Update 17JAN2010 — Removing full instructions at the request of the original author.

    What does this code output?
    No fair, running it!

    JAVA:
    1. public class Hmm {
    2.     public static class X{
    3.         int x = -1;
    4.         public X(){}
    5.         public X(int x ){setX(x);}
    6.         public int getX(){return x;}
    7.         public void setX(int x){this.x = x;}
    8.     }
    9.    
    10.     // the default object has a default value of 0.
    11.     public final static X DEFAULT_X = new X(0);
    12.    
    13.     private X m_X = new X();
    14.    
    15.     // A Hmm object uses the default value
    16.     public Hmm(){
    17.         m_X = DEFAULT_X;
    18.     }
    19.    
    20.     public X getX(){
    21.         return m_X;
    22.     }
    23.    
    24.     public void setX(X x){
    25.         this.m_X.setX(x.getX());
    26.     }
    27.    
    28.     public static void main(String[] args){
    29.         Hmm hmm1 = new Hmm();
    30.         hmm1.getX().setX(15);
    31.        
    32.         Hmm hmm2 = new Hmm();
    33.        
    34.         System.out.println("Hmm1 X = " +
    35.                 hmm1.getX().getX());
    36.         System.out.println("Hmm2 X = " +
    37.                 hmm2.getX().getX());
    38.         System.out.println("DEFAULT_X X = " +
    39.                 DEFAULT_X.getX());
    40.     }
    41. }

    Hint: It outputs exactly what the code tells it to! Not much of a hint... It doesn't do what the person wrote it expects it to do though.

    Answer: 15 for all three.

    Why: Cause the Hmm constructor sets its X reference to the static default object. Thereby the call to set the hmm1 object's X value to 15 actually sets the default value to 15!

    Hence, Final Static Objects are the devil!

    More tools that I think I are vital. Was just reading an article about what things to backup during an OS reinstallation, so I thought I would post some more of the utilities I found useful or needed.

    I think that is enough for now! More to come when I think of them.

    XOR in C++

    Final Recommendation:

    C:
    1. // 'a' and 'b' are some sort of integer type.
    2. // the double NOTs coerce to a standard boolean type
    3. // (compiler dependent)
    4. !!a ^ !!b

    Thankfully Java is easier
    http://www.sap-img.com/java/java-boolean-logical-operators.htm

    JAVA:
    1. // 'a' and 'b' are of boolean type
    2. // yay, no coercion necessary!
    3. a ^ b

    I didn’t know that the ^ operator was overloaded to handle Booleans, until I needed it a few weeks ago.

    At work I have been tasked with some Java serialization issues and I needed a quick way to get the serialversionUid from a class.

    I tried the serialver command that comes with the JDK, but I didn't like that I had to setup the full classpath (which for us can be quite large, lots of jars and directories). Instead I wanted it to work in my Eclipse environment which already has this classpath set and is always updated.

    I found this site that had a simple way of doing it:
    http://www.jguru.com/faq/view.jsp?EID=42982
    Thanks Tim!

    I wrote up a little wrapper main function:

    JAVA:
    1. import java.io.ObjectStreamClass;
    2. public class SerialVer {
    3.     public static void main(String[] args)
    4.         throws ClassNotFoundException {
    5.         if(args.length != 1) {
    6.             System.err.println("Must pass a class name!");
    7.             System.exit(-1);
    8.         }
    9.         Class cclass = Class.forName(args[0]);
    10.  
    11.         long uid =
    12.             ObjectStreamClass.lookup(cclass).getSerialVersionUID();
    13.        
    14.         StringBuffer sb = new StringBuffer();
    15.         sb.append(cclass.getName());
    16.         sb.append(":\t");
    17.         sb.append("static final long serialVersionUID = ");
    18.         sb.append(uid);
    19.         sb.append("L");
    20.         System.out.println(sb.toString());
    21.     }
    22. }

    I was just looking through some code. I noticed that an array needed to be reversed and that the original order was never needed again. This was a perfect time to reverse the array in place. This was Java so I looked in the Arrays class. But alas there wasn't a reverse for primitive arrays! Java Lists get a reverse method in the Collections class. But primitive arrays get no such love.

    Also, I thought of using the asList method from Arrays, but that just seems like a ton of overhead.

    So instead I wrote a quick little method for ints and any other primitive type.

    Here is the int example:

    JAVA:
    1. public static void reverseInPlace(int[] iar) {
    2.   final int length = iar.length;
    3.   final int halfLength = length / 2;
    4.   final int lengthMinus1 = length - 1;
    5.   for (int i = 0; i <halfLength; ++i) {
    6.     final int index2 = lengthMinus1 - i;
    7.  
    8.     //swap
    9.     int tmp = iar[i];
    10.     iar[i] = iar[index2];
    11.     iar[index2] = tmp;
    12.  }
    13. }

    Basically, this method just swaps the items from the front to back until reaching the middle of the array. If the loop continued on, it would just switch everything back and the original order would be preserved!

    The other implementations for the rest of the primitive types can be found here.

    This is a bit different from my last post about Process Explorer. Process Explorer is so powerful that I accidentally killed a different process than I wanted. This wasn't through some fancy schmancy search with regular expressions and kill accident. I just highlighted a process and clicked the Process Explorer Red X.

    I then proceeded to click 'Yes' on the simple dialog: pe_are_you_sure_want_to_kill_xxxx.PNG.

    Notice the blackened out word at the end of the Dialog's text? Well that is essentially what my brain did when I read that dialog.

    So, long story short I killed the wrong process. Nothing seemed to break for a few hours. Then when I went to debug a couple of things, I wasn't able to connect to a service that was supposed to be running. I restarted somethings on my end, then remembered, "Oh yeah, I killed a random process earlier." I restarted the killed service and lo and behold everything started working again.

    Lesson learned here kids: Don't kill random processes and then wonder why stuff breaks!

    Process Explorer is part of the Microsoft Sysinternals set of utilities.

    Was just having a problem getting the IE7 onbeforeunload event to be canceled. Found this blog about it: OnBeforeUnload, IE7, Assigning event handlers to null and the problems that arise.

    Pretty simple, just wrap the return with a boolean:

    JAVASCRIPT:
    1. var goodExit = false;
    2. window.onbeforeunload = confirmExit;
    3. function confirmExit()
    4. {
    5.   // normally this if-check wouldn't be here,
    6.   // cause the event handler was nulled elsewhere.
    7.   // But IE7 doesn't respect the null, and
    8.   // wants the handler to not return anything
    9.   // (not even null).
    10.   if (!goodExit)
    11.   {
    12.     return "Leaving this page will cause all unplaced orders to be discarded."+
    13.     "  Are you sure you want to leave the page?";
    14.   }
    15.   // notice no return statement here!
    16. }
    17. </script>
    18.  
    19.     . . . . .
    20.  
    21. <input type="image" src="<%=AppService.BASEDIR%>/images/lgrequest.gif" onclick="javascript: goodExit = true; window.onbeforeunload = null; document.forms[0].submit();" />

    This example was stolen from Brandon Himes and his blog, The Busted Mug. Thank you Brandon!